implement add and remove logic

This commit is contained in:
Nate Kelley 2025-03-21 13:37:28 -06:00
parent 2cf8f01aaa
commit f9dab63b67
No known key found for this signature in database
GPG Key ID: FD90372AB8D98B4F
4 changed files with 21 additions and 13 deletions

View File

@ -330,12 +330,15 @@ export const useUpdateDashboardShare = () => {
}); });
}; };
export const useSaveMetricsToDashboard = () => { /**
* Hook for adding metrics to a dashboard. This function also supports removing metrics via the addMetricToDashboardConfig
*/
export const useAddMetricsToDashboard = () => {
const queryClient = useQueryClient(); const queryClient = useQueryClient();
const prefetchDashboard = useGetDashboardAndInitializeMetrics(); const prefetchDashboard = useGetDashboardAndInitializeMetrics();
const { openErrorMessage } = useBusterNotifications(); const { openErrorMessage } = useBusterNotifications();
const saveMetricToDashboard = useMemoizedFn( const addMetricToDashboard = useMemoizedFn(
async ({ metricIds, dashboardId }: { metricIds: string[]; dashboardId: string }) => { async ({ metricIds, dashboardId }: { metricIds: string[]; dashboardId: string }) => {
const options = dashboardQueryKeys.dashboardGetDashboard(dashboardId); const options = dashboardQueryKeys.dashboardGetDashboard(dashboardId);
let dashboardResponse = queryClient.getQueryData(options.queryKey); let dashboardResponse = queryClient.getQueryData(options.queryKey);
@ -363,7 +366,7 @@ export const useSaveMetricsToDashboard = () => {
); );
return useMutation({ return useMutation({
mutationFn: saveMetricToDashboard, mutationFn: addMetricToDashboard,
onSuccess: (data, variables) => { onSuccess: (data, variables) => {
queryClient.invalidateQueries({ queryClient.invalidateQueries({
queryKey: dashboardQueryKeys.dashboardGetDashboard(variables.dashboardId).queryKey queryKey: dashboardQueryKeys.dashboardGetDashboard(variables.dashboardId).queryKey

View File

@ -6,7 +6,7 @@ import { Button } from '@/components/ui/buttons';
import { ASSET_ICONS } from '../config/assetIcons'; import { ASSET_ICONS } from '../config/assetIcons';
import { import {
useRemoveMetricsFromDashboard, useRemoveMetricsFromDashboard,
useSaveMetricsToDashboard useAddMetricsToDashboard
} from '@/api/buster_rest/dashboards'; } from '@/api/buster_rest/dashboards';
const EMPTY_SELECTED_DASHBOARDS: BusterMetric['dashboards'] = []; const EMPTY_SELECTED_DASHBOARDS: BusterMetric['dashboards'] = [];
@ -17,7 +17,7 @@ export const SaveMetricToDashboardButton: React.FC<{
selectedDashboards?: BusterMetric['dashboards']; selectedDashboards?: BusterMetric['dashboards'];
}> = React.memo( }> = React.memo(
({ metricIds, disabled = false, selectedDashboards = EMPTY_SELECTED_DASHBOARDS }) => { ({ metricIds, disabled = false, selectedDashboards = EMPTY_SELECTED_DASHBOARDS }) => {
const { mutateAsync: saveMetricsToDashboard } = useSaveMetricsToDashboard(); const { mutateAsync: saveMetricsToDashboard } = useAddMetricsToDashboard();
const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard(); const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard();
const onSaveToDashboard = useMemoizedFn(async (dashboardIds: string[]) => { const onSaveToDashboard = useMemoizedFn(async (dashboardIds: string[]) => {

View File

@ -4,8 +4,7 @@ import React, { useLayoutEffect, useMemo, useState } from 'react';
import { InputSelectModal, InputSelectModalProps } from '@/components/ui/modal/InputSelectModal'; import { InputSelectModal, InputSelectModalProps } from '@/components/ui/modal/InputSelectModal';
import { formatDate } from '@/lib'; import { formatDate } from '@/lib';
import { Button } from '@/components/ui/buttons'; import { Button } from '@/components/ui/buttons';
import { useGetDashboard } from '@/api/buster_rest/dashboards'; import { useAddMetricsToDashboard, useGetDashboard } from '@/api/buster_rest/dashboards';
import { useQueryClient } from '@tanstack/react-query';
export const AddToDashboardModal: React.FC<{ export const AddToDashboardModal: React.FC<{
open: boolean; open: boolean;
@ -14,6 +13,8 @@ export const AddToDashboardModal: React.FC<{
}> = React.memo(({ open, onClose, dashboardId }) => { }> = React.memo(({ open, onClose, dashboardId }) => {
const { data: dashboard, isFetched: isFetchedDashboard } = useGetDashboard(dashboardId); const { data: dashboard, isFetched: isFetchedDashboard } = useGetDashboard(dashboardId);
const { data: metrics, isFetched: isFetchedMetrics } = useGetMetricsList({}); const { data: metrics, isFetched: isFetchedMetrics } = useGetMetricsList({});
const { mutateAsync: addMetricsToDashboard } = useAddMetricsToDashboard();
const [selectedMetrics, setSelectedMetrics] = useState<string[]>([]); const [selectedMetrics, setSelectedMetrics] = useState<string[]>([]);
const columns: InputSelectModalProps['columns'] = [ const columns: InputSelectModalProps['columns'] = [
@ -41,8 +42,11 @@ export const AddToDashboardModal: React.FC<{
})); }));
}, [metrics.length]); }, [metrics.length]);
const handleAddMetrics = useMemoizedFn(async () => { const handleAddAndRemoveMetrics = useMemoizedFn(async () => {
// TODO: Implement the API call to add metrics to dashboard await addMetricsToDashboard({
dashboardId: dashboardId,
metricIds: selectedMetrics
});
onClose(); onClose();
}); });
@ -80,14 +84,14 @@ export const AddToDashboardModal: React.FC<{
}, },
primaryButton: { primaryButton: {
text: `Update metrics`, text: `Update metrics`,
onClick: handleAddMetrics, onClick: handleAddAndRemoveMetrics,
disabled: !isSelectedChanged, disabled: !isSelectedChanged,
tooltip: isSelectedChanged tooltip: isSelectedChanged
? `Adding ${selectedMetrics.length} metrics` ? `Adding ${selectedMetrics.length} metrics`
: 'No changes to update' : 'No changes to update'
} }
}; };
}, [selectedMetrics.length, isSelectedChanged, handleAddMetrics]); }, [selectedMetrics.length, isSelectedChanged, handleAddAndRemoveMetrics]);
useLayoutEffect(() => { useLayoutEffect(() => {
if (isFetchedDashboard) { if (isFetchedDashboard) {
@ -98,6 +102,7 @@ export const AddToDashboardModal: React.FC<{
return ( return (
<InputSelectModal <InputSelectModal
width={650}
open={open} open={open}
onClose={onClose} onClose={onClose}
columns={columns} columns={columns}

View File

@ -7,7 +7,7 @@ import {
useUpdateMetric useUpdateMetric
} from '@/api/buster_rest/metrics'; } from '@/api/buster_rest/metrics';
import { import {
useSaveMetricsToDashboard, useAddMetricsToDashboard,
useRemoveMetricsFromDashboard useRemoveMetricsFromDashboard
} from '@/api/buster_rest/dashboards'; } from '@/api/buster_rest/dashboards';
import { DropdownContent, DropdownItem, DropdownItems } from '@/components/ui/dropdown'; import { DropdownContent, DropdownItem, DropdownItems } from '@/components/ui/dropdown';
@ -125,7 +125,7 @@ export const ThreeDotMenuButton = React.memo(({ metricId }: { metricId: string }
ThreeDotMenuButton.displayName = 'ThreeDotMenuButton'; ThreeDotMenuButton.displayName = 'ThreeDotMenuButton';
const useDashboardSelectMenu = ({ metricId }: { metricId: string }) => { const useDashboardSelectMenu = ({ metricId }: { metricId: string }) => {
const { mutateAsync: saveMetricsToDashboard } = useSaveMetricsToDashboard(); const { mutateAsync: saveMetricsToDashboard } = useAddMetricsToDashboard();
const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard(); const { mutateAsync: removeMetricsFromDashboard } = useRemoveMetricsFromDashboard();
const { data: dashboards } = useGetMetric({ id: metricId }, (x) => x.dashboards); const { data: dashboards } = useGetMetric({ id: metricId }, (x) => x.dashboards);