diff --git a/frontend/src/container/GridGraphLayout/Graph/index.tsx b/frontend/src/container/GridGraphLayout/Graph/index.tsx index f6386e09f6..40a4db6ea7 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 => {