Skip to content

Commit

Permalink
fix: reduce redundant fetch request
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Nov 20, 2024
1 parent 899c68b commit ae0bc8e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
1 change: 1 addition & 0 deletions frontend/src/dashboard/useDashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ function getDashboardResource(name) {
return createDocumentResource({
doctype: 'Insights Dashboard',
name: name,
auto: false,
whitelistedMethods: {
fetch_chart_data: 'fetch_chart_data',
clear_charts_cache: 'clear_charts_cache',
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/widgets/useChartData.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ export default function useChartData(options = {}) {
error: null,
})

let currentLoadPromise = null
function load(query) {
if (!query) return
state.loading = true
return options
.resultsFetcher()
if (!currentLoadPromise) {
currentLoadPromise = options.resultsFetcher(query)
}
return currentLoadPromise
.then((results) => {
state.loading = false
state.rawData = getFormattedResult(results)
Expand All @@ -41,6 +44,9 @@ export default function useChartData(options = {}) {
state.loading = false
state.error = error
})
.finally(() => {
currentLoadPromise = null
})
}

if (options.query) {
Expand Down

0 comments on commit ae0bc8e

Please sign in to comment.