Skip to content

Commit

Permalink
CORE-4993: fix search
Browse files Browse the repository at this point in the history
  • Loading branch information
ekachxaidze98 committed Nov 5, 2024
1 parent 1744554 commit 5fce815
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
4 changes: 2 additions & 2 deletions store/data-provider/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
38 changes: 20 additions & 18 deletions templates/sdg/table/sdgTable.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
Expand Down Expand Up @@ -121,27 +125,25 @@ 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) ||
record.authors.some((author) =>
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) => {
Expand Down

0 comments on commit 5fce815

Please sign in to comment.