From c00f0f159b49723f996bfb9f23d5beaca2054ea1 Mon Sep 17 00:00:00 2001 From: palash-signoz Date: Mon, 21 Mar 2022 21:04:32 +0530 Subject: [PATCH] fix: save layout bug is resolved (#840) * fix: save layout bug is resolved * chore: onClick is also added in the component slider * chore: dashboard Id is added * chore: non dashboard widget is filtered out * chore: panel layout stack issue is resolved --- .../container/GridGraphLayout/Graph/index.tsx | 4 +- .../src/container/GridGraphLayout/index.tsx | 116 ++++++++++++------ .../src/container/GridGraphLayout/utils.ts | 66 ++++++++++ .../NewDashboard/ComponentsSlider/index.tsx | 54 ++++++-- .../NewWidget/LeftContainer/index.tsx | 2 +- frontend/src/container/NewWidget/index.tsx | 23 ++-- .../store/actions/dashboard/saveDashboard.ts | 2 + frontend/src/types/api/dashboard/getAll.ts | 1 + 8 files changed, 211 insertions(+), 57 deletions(-) create mode 100644 frontend/src/container/GridGraphLayout/utils.ts diff --git a/frontend/src/container/GridGraphLayout/Graph/index.tsx b/frontend/src/container/GridGraphLayout/Graph/index.tsx index f6386e09f636..40a4db6ea7c6 100644 --- a/frontend/src/container/GridGraphLayout/Graph/index.tsx +++ b/frontend/src/container/GridGraphLayout/Graph/index.tsx @@ -48,7 +48,7 @@ const GridCardGraph = ({ (async (): Promise => { try { const getMaxMinTime = GetMaxMinTime({ - graphType: widget.panelTypes, + graphType: widget?.panelTypes, maxTime, minTime, }); @@ -125,7 +125,7 @@ const GridCardGraph = ({ [], ); - const getModals = () => { + const getModals = (): JSX.Element => { return ( <> { - const { push } = useHistory(); - const { pathname } = useLocation(); - const { dashboards, loading } = useSelector( (state) => state.dashboards, ); @@ -50,6 +48,7 @@ const GridGraph = (): JSX.Element => { return []; } + // when the layout is not present if (data.layout === undefined) { return widgets.map((e, index) => { return { @@ -69,17 +68,24 @@ const GridGraph = (): JSX.Element => { }; }); } else { - return data.layout.map((e, index) => ({ - ...e, - y: 0, - Component: (): JSX.Element => ( - - ), - })); + return data.layout + .filter((_, index) => widgets[index]) + .map((e, index) => ({ + ...e, + Component: (): JSX.Element => { + if (widgets[index]) { + return ( + + ); + } + return <>; + }, + })); } }, [widgets, data.layout]); @@ -89,21 +95,34 @@ const GridGraph = (): JSX.Element => { (isMounted.current === true || isDeleted.current === true) ) { const preLayouts = getPreLayouts(); - setLayout(() => [ - ...preLayouts, - { - i: (preLayouts.length + 1).toString(), - x: (preLayouts.length % 2) * 6, - y: Infinity, - w: 6, - h: 2, - Component: AddWidgetWrapper, - maxW: 6, - isDraggable: false, - isResizable: false, - isBounded: true, - }, - ]); + setLayout(() => { + const getX = (): number => { + if (preLayouts && preLayouts?.length > 0) { + const last = preLayouts[preLayouts?.length - 1]; + + return (last.w + last.x) % 12; + } + return 0; + }; + + console.log({ x: getX() }); + + return [ + ...preLayouts, + { + i: (preLayouts.length + 1).toString(), + x: getX(), + y: Infinity, + w: 6, + h: 2, + Component: AddWidgetWrapper, + maxW: 6, + isDraggable: false, + isResizable: false, + isBounded: true, + }, + ]; + }); } return (): void => { @@ -112,18 +131,39 @@ const GridGraph = (): JSX.Element => { }, [widgets, layouts.length, AddWidgetWrapper, loading, getPreLayouts]); const onDropHandler = useCallback( - (allLayouts: Layout[], currectLayout: Layout, event: DragEvent) => { + async (allLayouts: Layout[], currentLayout: Layout, event: DragEvent) => { event.preventDefault(); if (event.dataTransfer) { - const graphType = event.dataTransfer.getData('text') as GRAPH_TYPES; - const generateWidgetId = v4(); - push(`${pathname}/new?graphType=${graphType}&widgetId=${generateWidgetId}`); + try { + const graphType = event.dataTransfer.getData('text') as GRAPH_TYPES; + const generateWidgetId = v4(); + + await updateDashboard({ + data, + generateWidgetId, + graphType, + selectedDashboard: selectedDashboard, + layout: allLayouts + .map((e, index) => ({ + ...e, + i: index.toString(), + // when a new element drops + w: e.i === '__dropping-elem__' ? 6 : e.w, + h: e.i === '__dropping-elem__' ? 2 : e.h, + })) + .filter((e) => e.maxW === undefined), + }); + } catch (error) { + notification.error({ + message: error.toString() || 'Something went wrong', + }); + } } }, - [pathname, push], + [data, selectedDashboard], ); - const onLayoutSaveHanlder = async (): Promise => { + const onLayoutSaveHandler = async (): Promise => { setSaveLayoutState((state) => ({ ...state, error: false, @@ -175,7 +215,7 @@ const GridGraph = (): JSX.Element => {