From 752a193ec6afff97838999f645405de0c5d49ef0 Mon Sep 17 00:00:00 2001 From: mahad Date: Wed, 29 Jul 2020 16:22:55 +0200 Subject: [PATCH] feat: trigger loading after mount --- src/components/autoComplete.js | 10 +++++++--- src/components/checkbox.js | 10 +++++++--- src/components/dataList.js | 13 +++++++++---- src/components/dataTable.js | 10 +++++++--- src/components/form.js | 10 +++++++--- src/components/radio.js | 10 +++++++--- src/components/select.js | 10 +++++++--- 7 files changed, 51 insertions(+), 22 deletions(-) diff --git a/src/components/autoComplete.js b/src/components/autoComplete.js index eb9da3c37..ecfc2ec3d 100644 --- a/src/components/autoComplete.js +++ b/src/components/autoComplete.js @@ -136,9 +136,13 @@ }; }, [searchParam]); - if (loading) { - B.triggerEvent('onLoad', loading); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); if (err && !displayError) { B.triggerEvent('onError', err.message); diff --git a/src/components/checkbox.js b/src/components/checkbox.js index c991792f7..9dea2f00a 100644 --- a/src/components/checkbox.js +++ b/src/components/checkbox.js @@ -57,9 +57,13 @@ const { loading, error: err, data, refetch } = model && useGetAll(model, { filter, skip: 0, take: 50 }); - if (loading) { - B.triggerEvent('onLoad', loading); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); if (err && !displayError) { B.triggerEvent('onError', err.message); diff --git a/src/components/dataList.js b/src/components/dataList.js index 7b679b1cd..2ebe71edc 100644 --- a/src/components/dataList.js +++ b/src/components/dataList.js @@ -96,15 +96,20 @@ }); }, []); + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); + const canvasLayout = () => { if (!model) { return builderLayout(); } - if (loading) { - B.triggerEvent('onLoad', loading); - return 'loading...'; - } + if (loading) return 'loading...'; if (error && !displayError) { B.triggerEvent('onError', error.message); diff --git a/src/components/dataTable.js b/src/components/dataTable.js index 48230a797..a08de6c2b 100644 --- a/src/components/dataTable.js +++ b/src/components/dataTable.js @@ -134,9 +134,13 @@ repeat(); }); - if (loading) { - B.triggerEvent('onLoad', loading); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); if (error && !displayError) { B.triggerEvent('onError', error.message); diff --git a/src/components/form.js b/src/components/form.js index a0a3c9c52..9a5ecb53b 100644 --- a/src/components/form.js +++ b/src/components/form.js @@ -65,9 +65,13 @@ B.triggerEvent('onLoad', loading); } - if (isFetching) { - B.triggerEvent('onLoad', isFetching); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && isFetching) { + B.triggerEvent('onLoad', isFetching); + } + mounted.current = false; + }, [isFetching]); if (err && !displayError) { B.triggerEvent('onError', formErrorMessage || err.message); diff --git a/src/components/radio.js b/src/components/radio.js index f5e3721c3..8bba41f81 100644 --- a/src/components/radio.js +++ b/src/components/radio.js @@ -65,9 +65,13 @@ const { loading, error: err, data, refetch } = model && useGetAll(model, { filter, skip: 0, take: 50 }); - if (loading) { - B.triggerEvent('onLoad', loading); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); if (err && !displayError) { B.triggerEvent('onError', err.message); diff --git a/src/components/select.js b/src/components/select.js index 4b8e5d70d..5d4f44c1d 100644 --- a/src/components/select.js +++ b/src/components/select.js @@ -48,9 +48,13 @@ const { loading, error, data, refetch } = model && useGetAll(model, { filter, skip: 0, take: 50 }); - if (loading) { - B.triggerEvent('onLoad', loading); - } + const mounted = useRef(true); + useEffect(() => { + if (!mounted.current && loading) { + B.triggerEvent('onLoad', loading); + } + mounted.current = false; + }, [loading]); if (error && !displayError) { B.triggerEvent('onError', error.message);