diff --git a/frontend/src/container/GridCardLayout/GridCardLayout.tsx b/frontend/src/container/GridCardLayout/GridCardLayout.tsx index 3ee11b4008..ed93df3a07 100644 --- a/frontend/src/container/GridCardLayout/GridCardLayout.tsx +++ b/frontend/src/container/GridCardLayout/GridCardLayout.tsx @@ -1,6 +1,6 @@ import './GridCardLayout.styles.scss'; -import { PlusOutlined, SaveFilled } from '@ant-design/icons'; +import { PlusOutlined } from '@ant-design/icons'; import { SOMETHING_WENT_WRONG } from 'constants/api'; import { PANEL_TYPES } from 'constants/queryBuilder'; import { themeColors } from 'constants/theme'; @@ -8,9 +8,12 @@ import { useUpdateDashboard } from 'hooks/dashboard/useUpdateDashboard'; import useComponentPermission from 'hooks/useComponentPermission'; import { useIsDarkMode } from 'hooks/useDarkMode'; import { useNotifications } from 'hooks/useNotifications'; +import isEqual from 'lodash-es/isEqual'; import { FullscreenIcon } from 'lucide-react'; import { useDashboard } from 'providers/Dashboard/Dashboard'; +import { useEffect, useState } from 'react'; import { FullScreen, useFullScreenHandle } from 'react-full-screen'; +import { Layout } from 'react-grid-layout'; import { useTranslation } from 'react-i18next'; import { useSelector } from 'react-redux'; import { AppState } from 'store/reducers'; @@ -29,6 +32,7 @@ import { ReactGridLayout, } from './styles'; import { GraphLayoutProps } from './types'; +import { removeUndefinedValuesFromLayout } from './utils'; function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { const { @@ -51,6 +55,8 @@ function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { const isDarkMode = useIsDarkMode(); + const [dashboardLayout, setDashboardLayout] = useState(layouts); + const updateDashboardMutation = useUpdateDashboard(); const { notifications } = useNotifications(); @@ -78,7 +84,7 @@ function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { ...selectedDashboard, data: { ...selectedDashboard.data, - layout: layouts.filter((e) => e.i !== PANEL_TYPES.EMPTY_WIDGET), + layout: dashboardLayout.filter((e) => e.i !== PANEL_TYPES.EMPTY_WIDGET), }, uuid: selectedDashboard.uuid, }; @@ -90,9 +96,6 @@ function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { setLayouts(updatedDashboard.payload.data.layout); setSelectedDashboard(updatedDashboard.payload); } - notifications.success({ - message: t('dashboard:layout_saved_successfully'), - }); featureResponse.refetch(); }, @@ -108,6 +111,32 @@ function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { ? [...ViewMenuAction, ...EditMenuAction] : [...ViewMenuAction]; + const handleLayoutChange = (layout: Layout[]): void => { + const filterLayout = removeUndefinedValuesFromLayout(layout); + const filterDashboardLayout = removeUndefinedValuesFromLayout( + dashboardLayout, + ); + if (!isEqual(filterLayout, filterDashboardLayout)) { + setDashboardLayout(layout); + } + }; + + useEffect(() => { + if ( + dashboardLayout && + Array.isArray(dashboardLayout) && + dashboardLayout.length > 0 && + !isEqual(layouts, dashboardLayout) && + !isDashboardLocked && + saveLayoutPermission && + !updateDashboardMutation.isLoading + ) { + onSaveHandler(); + } + + // eslint-disable-next-line react-hooks/exhaustive-deps + }, [dashboardLayout]); + return ( <> @@ -120,17 +149,6 @@ function GraphLayout({ onAddPanelHandler }: GraphLayoutProps): JSX.Element { {t('dashboard:full_view')} - {!isDashboardLocked && saveLayoutPermission && ( - - )} - {!isDashboardLocked && addPanelPermission && (