-
Notifications
You must be signed in to change notification settings - Fork 4.5k
Enhance dashboard parameter handling: persist updated values and apply saved parameters #7570
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 6 commits
9de81de
fd3eb53
35b8970
d25c4ee
e67a83a
d4ec1d3
54b27e3
13d5084
6bff5be
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| import { useState, useEffect, useMemo, useCallback, useRef } from "react"; | ||
| import { isEmpty, includes, compact, map, has, pick, keys, extend, every, get } from "lodash"; | ||
| import { isEmpty, includes, compact, map, has, pick, keys, extend, every, get, isEqual } from "lodash"; | ||
| import notification from "@/services/notification"; | ||
| import location from "@/services/location"; | ||
| import url from "@/services/url"; | ||
|
|
@@ -144,16 +144,62 @@ function useDashboard(dashboardData) { | |
| [loadWidget] | ||
| ); | ||
|
|
||
| const persistParameterValues = useCallback( | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| parameterValues => { | ||
|
||
| if (!canEditDashboard || !parameterValues || isEmpty(parameterValues)) { | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| const currentDashboard = dashboardRef.current; | ||
| const currentValues = get(currentDashboard, "options.parameterValues", {}); | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| const nextValues = extend({}, currentValues); | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| let hasChanges = false; | ||
|
|
||
| Object.entries(parameterValues).forEach(([name, value]) => { | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!isEqual(nextValues[name], value)) { | ||
| nextValues[name] = value; | ||
| hasChanges = true; | ||
| } | ||
| }); | ||
|
|
||
| if (!hasChanges) { | ||
| return Promise.resolve(); | ||
| } | ||
|
|
||
| return updateDashboard({ | ||
| options: extend({}, get(currentDashboard, "options", {}), { parameterValues: nextValues }), | ||
| }); | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| [canEditDashboard, updateDashboard] | ||
| ); | ||
|
|
||
| const refreshDashboard = useCallback( | ||
| updatedParameters => { | ||
| if (!refreshing) { | ||
| setRefreshing(true); | ||
| loadDashboard(true, updatedParameters).finally(() => setRefreshing(false)); | ||
| if (refreshing) { | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return; | ||
| } | ||
|
|
||
| setRefreshing(true); | ||
| loadDashboard(true, updatedParameters).finally(() => setRefreshing(false)); | ||
| }, | ||
| [refreshing, loadDashboard] | ||
| ); | ||
|
|
||
| const saveDashboardParameters = useCallback(() => { | ||
| const latestValues = (globalParameters || []).reduce((acc, param) => { | ||
| if (!param) return acc; | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| acc[param.name] = param.value === undefined ? null : param.value; | ||
denisov-vlad marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return acc; | ||
| }, {}); | ||
| if (isEmpty(latestValues)) return Promise.resolve(); | ||
|
|
||
| return persistParameterValues(latestValues).catch(error => { | ||
| console.error("Failed to persist parameter values:", error); | ||
| notification.error("Parameter values could not be saved. Your changes may not be persisted."); | ||
| throw error; | ||
| }); | ||
| }, [globalParameters, persistParameterValues]); | ||
|
|
||
| const archiveDashboard = useCallback(() => { | ||
| recordEvent("archive", "dashboard", dashboard.id); | ||
| Dashboard.delete(dashboard).then(updatedDashboard => | ||
|
|
@@ -238,6 +284,7 @@ function useDashboard(dashboardData) { | |
| setRefreshRate, | ||
| disableRefreshRate, | ||
| ...editModeHandler, | ||
| saveDashboardParameters, | ||
| gridDisabled, | ||
| setGridDisabled, | ||
| fullscreen, | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.