From 9716681f2b025b81219fdc25a6cda5c8743a2578 Mon Sep 17 00:00:00 2001 From: dlicheva Date: Sun, 28 Jul 2024 19:00:08 +0100 Subject: [PATCH] fix loading indicator on explorer --- .../frontend/src/pages/Explorer/explorer.tsx | 8 +++++- .../frontend/src/providers/DataProvider.tsx | 25 ++++++++----------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/application/frontend/src/pages/Explorer/explorer.tsx b/application/frontend/src/pages/Explorer/explorer.tsx index 4836e2df..9aa2f7d6 100644 --- a/application/frontend/src/pages/Explorer/explorer.tsx +++ b/application/frontend/src/pages/Explorer/explorer.tsx @@ -14,6 +14,7 @@ import { getInternalUrl } from '../../utils/document'; export const Explorer = () => { const { dataLoading, dataTree } = useDataStore(); + const [loading, setLoading] = useState(false); const [filter, setFilter] = useState(''); const [filteredTree, setFilteredTree] = useState(); const applyHighlight = (text, term) => { @@ -78,6 +79,10 @@ export const Explorer = () => { } }, [filter, dataTree, setFilteredTree]); + useEffect(() => { + setLoading(dataLoading); + }, [dataLoading]); + function processNode(item) { if (!item) { return <>; @@ -121,6 +126,7 @@ export const Explorer = () => { ); } + function update(event) { setFilter(event.target.value.toLowerCase()); } @@ -163,7 +169,7 @@ export const Explorer = () => { */} - + {filteredTree?.map((item) => { return processNode(item); diff --git a/application/frontend/src/providers/DataProvider.tsx b/application/frontend/src/providers/DataProvider.tsx index d389c6cb..2f7b4d6b 100644 --- a/application/frontend/src/providers/DataProvider.tsx +++ b/application/frontend/src/providers/DataProvider.tsx @@ -24,7 +24,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { const { apiUrl } = useEnvironment(); const [dataLoading, setDataLoading] = useState(false); const [dataStore, setDataStore] = useState>( - getLocalStorageObject(DATA_STORE_KEY) || {} + getLocalStorageObject(DATA_STORE_KEY) || {}, ); const [dataTree, setDataTree] = useState(getLocalStorageObject(DATA_TREE_KEY) || []); @@ -42,7 +42,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { const initialLinks = storedDoc.links; let creLinks = initialLinks.filter( - (x) => x.document && !keyPath.includes(getStoreKey(x.document)) && getStoreKey(x.document) in dataStore + (x) => x.document && !keyPath.includes(getStoreKey(x.document)) && getStoreKey(x.document) in dataStore, ); if (!creLinks.length) { @@ -59,7 +59,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { //attach Standards to the CREs const standards = initialLinks.filter( (link) => - link.document && link.document.doctype === 'Standard' && !keyPath.includes(getStoreKey(link.document)) + link.document && link.document.doctype === 'Standard' && !keyPath.includes(getStoreKey(link.document)), ); storedDoc.links = [...creLinks, ...standards]; @@ -70,7 +70,6 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { 'root_cres', async () => { if (!dataTree.length && Object.keys(dataStore).length) { - setDataLoading(true); try { const result = await axios.get(`${apiUrl}/root_cres`); const treeData = result.data.data.map((x) => buildTree(x)); @@ -84,10 +83,7 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { { retry: false, enabled: false, - onSettled: () => { - setDataLoading(false); - }, - } + }, ); const getStoreQuery = useQuery( @@ -95,7 +91,6 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { async () => { if (!Object.keys(dataStore).length) { try { - setDataLoading(true); const result = await axios.get(`${apiUrl}/all_cres`); let data = result.data.data; const page = result.data.page; @@ -128,18 +123,18 @@ export const DataProvider = ({ children }: { children: React.ReactNode }) => { { retry: false, enabled: false, - onSettled: () => { - setDataLoading(false); - }, - } + }, ); + + useEffect(() => { + setDataLoading(getStoreQuery.isLoading || getTreeQuery.isLoading); + }, [getStoreQuery.isLoading, getTreeQuery.isLoading]); + useEffect(() => { - setDataLoading(true); getStoreQuery.refetch(); }, [dataTree]); useEffect(() => { - setDataLoading(true); getTreeQuery.refetch(); }, [dataStore, setDataStore]);