diff --git a/src/client/api/model/website.ts b/src/client/api/model/website.ts index 0df7a11..b6d23e2 100644 --- a/src/client/api/model/website.ts +++ b/src/client/api/model/website.ts @@ -41,20 +41,6 @@ export async function getWorkspaceWebsiteInfo( return data.website; } -export async function updateWorkspaceWebsiteInfo( - workspaceId: string, - websiteId: string, - info: { name: string; domain: string } -) { - await request.post(`/api/workspace/website/${websiteId}`, { - workspaceId, - name: info.name, - domain: info.domain, - }); - - queryClient.resetQueries(['websites', workspaceId]); -} - export async function deleteWorkspaceWebsite( workspaceId: string, websiteId: string diff --git a/src/client/components/WebsiteInfo.tsx b/src/client/components/WebsiteInfo.tsx index 25a2a88..616e7f4 100644 --- a/src/client/components/WebsiteInfo.tsx +++ b/src/client/components/WebsiteInfo.tsx @@ -3,7 +3,6 @@ import React from 'react'; import { useNavigate, useParams } from 'react-router'; import { deleteWorkspaceWebsite, - updateWorkspaceWebsiteInfo, useWorkspaceWebsiteInfo, } from '../api/model/website'; import { useRequest } from '../hooks/useRequest'; @@ -11,38 +10,50 @@ import { useCurrentWorkspaceId } from '../store/user'; import { ErrorTip } from './ErrorTip'; import { Loading } from './Loading'; import { NoWorkspaceTip } from './NoWorkspaceTip'; +import { defaultErrorHandler, defaultSuccessHandler, trpc } from '../api/trpc'; +import { useQueryClient } from '@tanstack/react-query'; +import { useEvent } from '../hooks/useEvent'; export const WebsiteInfo: React.FC = React.memo(() => { + const workspaceId = useCurrentWorkspaceId(); const { websiteId } = useParams<{ websiteId: string; }>(); - const currentWorkspaceId = useCurrentWorkspaceId(); const { website, isLoading } = useWorkspaceWebsiteInfo( - currentWorkspaceId!, + workspaceId, websiteId! ); const navigate = useNavigate(); + const queryClient = useQueryClient(); - const [, handleSave] = useRequest( + const updateMutation = trpc.website.updateInfo.useMutation({ + onSuccess: () => { + queryClient.resetQueries(['websites', workspaceId]); // TODO: translation to trpc + defaultSuccessHandler(); + }, + onError: defaultErrorHandler, + }); + + const handleSave = useEvent( async (values: { name: string; domain: string }) => { - await updateWorkspaceWebsiteInfo(currentWorkspaceId!, websiteId!, { + await updateMutation.mutateAsync({ + workspaceId, + websiteId: websiteId!, name: values.name, domain: values.domain, }); - - message.success('Save Success'); } ); const [, handleDeleteWebsite] = useRequest(async () => { - await deleteWorkspaceWebsite(currentWorkspaceId!, websiteId!); + await deleteWorkspaceWebsite(workspaceId, websiteId!); message.success('Delete Success'); navigate('/settings/websites'); }); - if (!currentWorkspaceId) { + if (!workspaceId) { return ; } diff --git a/src/server/model/workspace.ts b/src/server/model/workspace.ts index 9ac1cf7..06460d3 100644 --- a/src/server/model/workspace.ts +++ b/src/server/model/workspace.ts @@ -66,26 +66,6 @@ export async function getWorkspaceWebsiteInfo( return websiteInfo; } -export async function updateWorkspaceWebsiteInfo( - workspaceId: string, - websiteId: string, - name: string, - domain: string -) { - const websiteInfo = await prisma.website.update({ - where: { - id: websiteId, - workspaceId, - }, - data: { - name, - domain, - }, - }); - - return websiteInfo; -} - export async function addWorkspaceWebsite( workspaceId: string, name: string, diff --git a/src/server/router/workspace.ts b/src/server/router/workspace.ts index 2a5bad4..61496a8 100644 --- a/src/server/router/workspace.ts +++ b/src/server/router/workspace.ts @@ -76,43 +76,6 @@ workspaceRouter.get( } ); -workspaceRouter.post( - '/website/:websiteId', - validate( - body('workspaceId').isString(), - param('websiteId') - .isString() - .isUUID() - .withMessage('workspaceId should be UUID'), - body('name') - .isString() - .withMessage('name should be string') - .isLength({ max: 100 }) - .withMessage('length should be under 100'), - body('domain') - .isURL() - .withMessage('domain should be URL') - .isLength({ max: 500 }) - .withMessage('length should be under 500') - ), - auth(), - workspacePermission(), - async (req, res) => { - const workspaceId = req.query.workspaceId as string; - const websiteId = req.params.websiteId; - const { name, domain } = req.body; - - const website = await updateWorkspaceWebsiteInfo( - workspaceId, - websiteId, - name, - domain - ); - - res.json({ website }); - } -); - workspaceRouter.delete( '/:workspaceId/website/:websiteId', validate(param('workspaceId').isString(), param('websiteId').isString()), diff --git a/src/server/trpc/routers/website.ts b/src/server/trpc/routers/website.ts index 873e1c5..1ff9f34 100644 --- a/src/server/trpc/routers/website.ts +++ b/src/server/trpc/routers/website.ts @@ -1,4 +1,4 @@ -import { router, workspaceProcedure } from '../trpc'; +import { router, workspaceOwnerProcedure, workspaceProcedure } from '../trpc'; import { z } from 'zod'; import { getWebsiteOnlineUserCount } from '../../model/website'; import { prisma } from '../../model/_client'; @@ -154,4 +154,28 @@ export const websiteRouter = router({ return []; }), + updateInfo: workspaceOwnerProcedure + .input( + z.object({ + websiteId: z.string().cuid(), + name: z.string().max(100), + domain: z.union([z.string().max(500).url(), z.string().max(500).ip()]), + }) + ) + .mutation(async ({ input }) => { + const { workspaceId, websiteId, name, domain } = input; + + const websiteInfo = await prisma.website.update({ + where: { + id: websiteId, + workspaceId, + }, + data: { + name, + domain, + }, + }); + + return websiteInfo; + }), });