From bb1cec351f072dd6bfc24f7b44d405280d4379f1 Mon Sep 17 00:00:00 2001 From: Tim Pechersky Date: Wed, 24 Nov 2021 13:41:41 +0000 Subject: [PATCH] init update dashboard name --- frontend/pages/dashboard/[dashboardId].js | 49 ++++++++++++++----- frontend/src/components/SubscriptionsList.js | 2 +- frontend/src/core/hooks/useDashboard.js | 13 +++++ .../src/core/services/dashboard.service.js | 13 +++++ 4 files changed, 64 insertions(+), 13 deletions(-) diff --git a/frontend/pages/dashboard/[dashboardId].js b/frontend/pages/dashboard/[dashboardId].js index c55fa132..0d29002e 100644 --- a/frontend/pages/dashboard/[dashboardId].js +++ b/frontend/pages/dashboard/[dashboardId].js @@ -8,6 +8,9 @@ import { Text, Spacer, IconButton, + Editable, + EditableInput, + EditablePreview, } from "@chakra-ui/react"; import Scrollable from "../../src/components/Scrollable"; import RangeSelector from "../../src/components/RangeSelector"; @@ -94,8 +97,12 @@ const Analytics = () => { const [timeRange, setTimeRange] = useState(timeMap[MINUTE_KEY]); const router = useRouter(); const { dashboardId } = router.params; - const { dashboardCache, dashboardLinksCache, deleteDashboard } = - useDashboard(dashboardId); + const { + dashboardCache, + dashboardLinksCache, + deleteDashboard, + updateDashboard, + } = useDashboard(dashboardId); const { subscriptionsCache } = useSubscriptions(); @@ -145,6 +152,12 @@ const Analytics = () => { // } // }, [nodesReady, windowSize]); + const updateCallback = ({ name }) => { + const dashboard = { ...dashboardCache.data.data.resource_data }; + dashboard.name = name; + updateDashboard.mutate({ id: dashboardCache.data.data.id, dashboard }); + }; + if ( dashboardCache.isLoading || dashboardLinksCache.isLoading || @@ -166,16 +179,28 @@ const Analytics = () => { minH="100vh" > - - {dashboardCache.data.data.resource_data.name} - } - variant="ghost" - colorScheme="red" - size="sm" - onClick={() => toggleAlert(() => deleteDashboard.mutate())} - /> - + + updateCallback({ + name: nextValue, + }) + } + > + + + + + } + variant="ghost" + colorScheme="red" + size="sm" + onClick={() => toggleAlert(() => deleteDashboard.mutate())} + /> { }, }); + const updateDashboard = useMutation(DashboardService.updateDashboard, { + onSuccess: () => { + toast("Updated new dashboard", "success"); + }, + onError: (error) => { + toast(error.error, "error", "Fail"); + }, + onSettled: () => { + dashboardsListCache.refetch(); + }, + }); + const deleteDashboard = useMutation( () => DashboardService.deleteDashboard(dashboardId), { @@ -80,6 +92,7 @@ const useDashboard = (dashboardId) => { dashboardCache, deleteDashboard, dashboardLinksCache, + updateDashboard, }; }; diff --git a/frontend/src/core/services/dashboard.service.js b/frontend/src/core/services/dashboard.service.js index fed37f1e..3b50795b 100644 --- a/frontend/src/core/services/dashboard.service.js +++ b/frontend/src/core/services/dashboard.service.js @@ -1,3 +1,4 @@ +import { identify } from "mixpanel-browser"; import { http } from "../utils"; const API_URL = process.env.NEXT_PUBLIC_MOONSTREAM_API_URL; @@ -10,6 +11,18 @@ export const createDashboard = (data) => { }); }; +export const updateDashboard = ({ dashboard, id }) => { + return http({ + method: "PUT", + url: `${API_URL}/dashboards/${id}`, + data: { + ...dashboard, + dashboard_id: id, + subscriptions: dashboard.dashboard_subscriptions, + }, + }); +}; + export const getDashboardsList = () => { return http({ method: "GET",