From 5fce815d61363f6d970ee611eddc068eb5db3054 Mon Sep 17 00:00:00 2001 From: ekachxaidze98 Date: Tue, 5 Nov 2024 15:32:20 +0400 Subject: [PATCH] CORE-4993: fix search --- store/data-provider/index.js | 4 ++-- templates/sdg/table/sdgTable.jsx | 38 +++++++++++++++++--------------- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/store/data-provider/index.js b/store/data-provider/index.js index 34ce5d73..ce5c829f 100644 --- a/store/data-provider/index.js +++ b/store/data-provider/index.js @@ -535,9 +535,9 @@ class DataProvider extends Resource { if (response.ok && response.status === 200) { const data = await response.json() this.setSdgTableList([...this.sdgTableList, ...data]) - } else throw new Error('Failed to fetch sdg data') + } else throw new Error('Failed to fetch SDG data') } catch (error) { - console.error('Error fetching sdg data:', error) + console.error('Error fetching SDG data:', error) this.setSdgTableList([]) } finally { this.sdgTableDataLoading = false diff --git a/templates/sdg/table/sdgTable.jsx b/templates/sdg/table/sdgTable.jsx index b1a04f55..e5542c91 100644 --- a/templates/sdg/table/sdgTable.jsx +++ b/templates/sdg/table/sdgTable.jsx @@ -54,18 +54,22 @@ const SdgTable = observer( (a, b) => b.sdg[0].score - a.sdg[0].score ) setSearchResults(sortedData) - setTableData(sortedData) + setTableData(sortedData.slice(0, 10)) } }, [sdgTableList]) const fetchData = async () => { const from = (page + 1) * 500 - await getSdgTableData(providerId, from, 500) - const newData = searchResults.slice(from, from + 500) + const endIndex = Math.min(tableData.length + 10, sdgTableList.length) + + if (tableData.length >= sdgTableList.length) { + await getSdgTableData(providerId, from, 500) + setPage(page + 1) + } + + const newData = searchResults.slice(tableData.length, endIndex) setTableData((prevData) => [...prevData, ...newData]) - setPage(page + 1) } - const getSdgIcon = (type, score) => { const sdg = sdgTypes.find((sdgItem) => sdgItem.id === type) return sdg ? ( @@ -121,8 +125,10 @@ const SdgTable = observer( useEffect(() => { const lowerSearchTerm = localSearchTerm.toLowerCase() + let filteredData + if (lowerSearchTerm) { - const filteredData = sdgTableList.filter( + filteredData = sdgTableList.filter( (record) => record.oai.toLowerCase().includes(lowerSearchTerm) || record.title.toLowerCase().includes(lowerSearchTerm) || @@ -130,18 +136,14 @@ const SdgTable = observer( author.name.toLowerCase().includes(lowerSearchTerm) ) ) - const sortedFilteredData = filteredData.sort( - (a, b) => b.sdg[0].score - a.sdg[0].score - ) - setSearchResults(sortedFilteredData) - setTableData(sortedFilteredData) - } else { - const sortedData = [...sdgTableList].sort( - (a, b) => b.sdg[0].score - a.sdg[0].score - ) - setSearchResults(sortedData) - setTableData(sortedData) - } + } else filteredData = [...sdgTableList] + + const sortedFilteredData = filteredData.sort( + (a, b) => b.sdg[0].score - a.sdg[0].score + ) + + setSearchResults(sortedFilteredData) + setTableData(sortedFilteredData.slice(0, tableData.length + 10 || 10)) }, [localSearchTerm, sdgTableList]) const searchChange = (event) => {