From c68d27a6d3f7899aa7c7c2bfae15819f7bfc18ed Mon Sep 17 00:00:00 2001 From: moonrailgun Date: Sat, 16 Dec 2023 14:43:17 +0800 Subject: [PATCH] feat: add status page basic info change --- .../monitor/StatusPage/EditForm.tsx | 28 ++++- .../components/monitor/StatusPage/index.tsx | 116 +++++++++++++----- src/client/pages/Monitor/PageAdd.tsx | 1 + src/server/trpc/routers/monitor.ts | 3 + 4 files changed, 114 insertions(+), 34 deletions(-) diff --git a/src/client/components/monitor/StatusPage/EditForm.tsx b/src/client/components/monitor/StatusPage/EditForm.tsx index c6ef945..1ff014c 100644 --- a/src/client/components/monitor/StatusPage/EditForm.tsx +++ b/src/client/components/monitor/StatusPage/EditForm.tsx @@ -5,21 +5,28 @@ import { z } from 'zod'; const { Text } = Typography; -interface Values { +export interface MonitorStatusPageEditFormValues { title: string; slug: string; } interface MonitorStatusPageEditFormProps { isLoading?: boolean; - onFinish: (values: Values) => void; + initialValues?: Partial; + onFinish: (values: MonitorStatusPageEditFormValues) => void; + onCancel?: () => void; + saveButtonLabel?: string; } export const MonitorStatusPageEditForm: React.FC = React.memo((props) => { return (
- layout="vertical" onFinish={props.onFinish}> + + layout="vertical" + initialValues={props.initialValues} + onFinish={props.onFinish} + > > - + +
+ + + {props.onCancel && ( + + )} +
); diff --git a/src/client/components/monitor/StatusPage/index.tsx b/src/client/components/monitor/StatusPage/index.tsx index df05237..70494f7 100644 --- a/src/client/components/monitor/StatusPage/index.tsx +++ b/src/client/components/monitor/StatusPage/index.tsx @@ -1,8 +1,15 @@ import { Button, Empty } from 'antd'; -import React from 'react'; +import React, { useState } from 'react'; import { trpc } from '../../../api/trpc'; import { MonitorHealthBar } from '../MonitorHealthBar'; import { useAllowEdit } from './useAllowEdit'; +import { + MonitorStatusPageEditForm, + MonitorStatusPageEditFormValues, +} from './EditForm'; +import clsx from 'clsx'; +import { useRequest } from '../../../hooks/useRequest'; +import { useNavigate } from 'react-router'; interface MonitorStatusPageProps { slug: string; @@ -15,41 +22,94 @@ export const MonitorStatusPage: React.FC = React.memo( const { data: info } = trpc.monitor.getPageInfo.useQuery({ slug, }); + const editPageMutation = trpc.monitor.editPage.useMutation(); + const trpcUtils = trpc.useContext(); + const navigate = useNavigate(); const allowEdit = useAllowEdit(info?.workspaceId); + const [editMode, setEditMode] = useState(false); const monitorList = info?.monitorList ?? []; + const [{ loading }, handleSave] = useRequest( + async (values: MonitorStatusPageEditFormValues) => { + if (!info) { + return; + } + + const newPageInfo = await editPageMutation.mutateAsync({ + id: info.id, + workspaceId: info.workspaceId, + ...values, + }); + + trpcUtils.monitor.getPageInfo.setData( + { + slug, + }, + newPageInfo + ); + setEditMode(false); + + if (info.slug !== newPageInfo.slug) { + // if slug is changed, should to navigate to new url + navigate(`/status/${newPageInfo.slug}`); + } + } + ); + return ( -
-
{info?.title}
- -
{allowEdit && }
- -
Services
- - {info && ( -
- {monitorList.length > 0 ? ( - monitorList.map((item) => ( -
- -
- )) - ) : ( - - )} +
+ {editMode && ( +
+
)} +
+
{info?.title}
+ +
+ {allowEdit && !editMode && ( + + )} +
+ +
Services
+ + {info && ( +
+ {monitorList.length > 0 ? ( + monitorList.map((item) => ( +
+ +
+ )) + ) : ( + + )} +
+ )} +
); } diff --git a/src/client/pages/Monitor/PageAdd.tsx b/src/client/pages/Monitor/PageAdd.tsx index 16b582c..ad95bb9 100644 --- a/src/client/pages/Monitor/PageAdd.tsx +++ b/src/client/pages/Monitor/PageAdd.tsx @@ -32,6 +32,7 @@ export const MonitorPageAdd: React.FC = React.memo(() => { return (
diff --git a/src/server/trpc/routers/monitor.ts b/src/server/trpc/routers/monitor.ts index ad3184d..e5822d7 100644 --- a/src/server/trpc/routers/monitor.ts +++ b/src/server/trpc/routers/monitor.ts @@ -523,6 +523,9 @@ export const monitorRouter = router({ const existSlugCount = await prisma.monitorStatusPage.count({ where: { slug, + id: { + not: id, + }, }, });