From e74c9d0793670e82a3f5e022850967fd9b631a4e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Z=C3=ADpek?= Date: Sun, 28 Aug 2022 22:52:57 +0200 Subject: [PATCH] Added dashboard migration, reworked types --- client/src/api/dashboards.ts | 2 +- .../src/pages/dashboard/NewDashboardPage.tsx | 2 +- .../components/BoxSettings/BoxSettings.tsx | 79 ++++++++----------- .../BoxSettings/components/DialSettings.tsx | 44 +++++++---- .../BoxSettings/components/GraphSettings.tsx | 42 ++++++---- .../components/BoxDialContent.tsx | 12 +-- .../components/BoxGraphContent.tsx | 10 ++- .../DashboardGrid/components/EditableBox.tsx | 6 +- client/src/pages/dashboard/types.ts | 2 +- client/src/utils/createDashboardContent.ts | 2 +- .../dashboard/createDashboardMigration.ts | 9 +++ .../utils/dashboard/getDashboardMigrations.ts | 11 +++ .../src/utils/dashboard/migrations/index.ts | 1 + .../dashboard/migrations/migrationOf10to11.ts | 17 ++++ .../utils/{ => dashboard}/parseDashboard.ts | 15 +++- client/src/utils/hooks/useForm.ts | 4 +- 16 files changed, 158 insertions(+), 100 deletions(-) create mode 100644 client/src/utils/dashboard/createDashboardMigration.ts create mode 100644 client/src/utils/dashboard/getDashboardMigrations.ts create mode 100644 client/src/utils/dashboard/migrations/index.ts create mode 100644 client/src/utils/dashboard/migrations/migrationOf10to11.ts rename client/src/utils/{ => dashboard}/parseDashboard.ts (67%) diff --git a/client/src/api/dashboards.ts b/client/src/api/dashboards.ts index 1e02267..2373afc 100644 --- a/client/src/api/dashboards.ts +++ b/client/src/api/dashboards.ts @@ -1,4 +1,4 @@ -import { DashboardContent } from '@/utils/parseDashboard' +import { DashboardContent } from '@/utils/dashboard/parseDashboard' import { request } from './request' export type DashboardInfo = { diff --git a/client/src/pages/dashboard/NewDashboardPage.tsx b/client/src/pages/dashboard/NewDashboardPage.tsx index da1c9ef..ad66487 100644 --- a/client/src/pages/dashboard/NewDashboardPage.tsx +++ b/client/src/pages/dashboard/NewDashboardPage.tsx @@ -5,7 +5,7 @@ import { } from '@/api/dashboards' import { UserLayout } from '@/layouts/UserLayout/UserLayout' import { createDashboardContent } from '@/utils/createDashboardContent' -import { parseDashboard } from '@/utils/parseDashboard' +import { parseDashboard } from '@/utils/dashboard/parseDashboard' import { useEffect, useMemo, useState } from 'preact/hooks' import { useQuery, useQueryClient } from 'react-query' import { DashboardGrid } from './components/DashboardGrid/DashboardGrid' diff --git a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx index e1ff990..d2b5b3e 100644 --- a/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx +++ b/client/src/pages/dashboard/components/BoxSettings/BoxSettings.tsx @@ -1,7 +1,11 @@ import { getSensors } from '@/api/sensors' import { Modal } from '@/components/Modal' import { useConfirmModal } from '@/contexts/ConfirmModalsContext' -import { DashboardDialData, DashboardGraphData } from '@/utils/parseDashboard' +import { + DashboardDialData, + DashboardGraphData, +} from '@/utils/dashboard/parseDashboard' +import { useForm } from '@/utils/hooks/useForm' import { useState } from 'preact/hooks' import { useQuery } from 'react-query' import { BoxDefinition } from '../../types' @@ -18,14 +22,28 @@ type Props = { export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { const sensors = useQuery(['/sensors'], getSensors) - const [formState, setFormState] = useState(() => ({ - sensor: value.sensor, - title: value.title, - type: value.data?.type ?? 'graph', - })) - const [data, setData] = useState(() => value.data) + const { + value: formState, + handleChange, + handleSubmit, + } = useForm({ + defaultValue: () => ({ + title: value.title, + type: value.data?.type ?? '', + }), + onSubmit: async (v) => { + onClose() + + onSave({ + ...value, + title: v.title, + data: data, + }) + }, + }) + const deleteConfirm = useConfirmModal({ content: 'Are you sure you want to delete the box?', onConfirm: () => { @@ -33,48 +51,10 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { }, }) - const handleSave = async (e: Event) => { - e.preventDefault() - e.stopPropagation() - - onClose() - - onSave({ - ...value, - sensor: formState.sensor, - title: formState.title, - data: data, - }) - } - - const handleChange = (e: Event) => { - const target = e.target as HTMLSelectElement | HTMLInputElement - - setFormState({ - ...formState, - [target.name]: target.value, - }) - } - return ( -
-
- - -
- - {formState.sensor && ( + + { <>
@@ -90,6 +70,7 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { name="type" value={formState.type} onChange={handleChange} + required > @@ -100,6 +81,7 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { )} @@ -107,10 +89,11 @@ export const BoxSettings = ({ value, onSave, onClose, onRemove }: Props) => { )} - )} + }