refactor: migrate website update to trpc

This commit is contained in:
moonrailgun 2023-10-15 00:39:14 +08:00
parent b6f7c32171
commit df7d18c2a0
5 changed files with 45 additions and 81 deletions

View File

@ -41,20 +41,6 @@ export async function getWorkspaceWebsiteInfo(
return data.website; 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( export async function deleteWorkspaceWebsite(
workspaceId: string, workspaceId: string,
websiteId: string websiteId: string

View File

@ -3,7 +3,6 @@ import React from 'react';
import { useNavigate, useParams } from 'react-router'; import { useNavigate, useParams } from 'react-router';
import { import {
deleteWorkspaceWebsite, deleteWorkspaceWebsite,
updateWorkspaceWebsiteInfo,
useWorkspaceWebsiteInfo, useWorkspaceWebsiteInfo,
} from '../api/model/website'; } from '../api/model/website';
import { useRequest } from '../hooks/useRequest'; import { useRequest } from '../hooks/useRequest';
@ -11,38 +10,50 @@ import { useCurrentWorkspaceId } from '../store/user';
import { ErrorTip } from './ErrorTip'; import { ErrorTip } from './ErrorTip';
import { Loading } from './Loading'; import { Loading } from './Loading';
import { NoWorkspaceTip } from './NoWorkspaceTip'; 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(() => { export const WebsiteInfo: React.FC = React.memo(() => {
const workspaceId = useCurrentWorkspaceId();
const { websiteId } = useParams<{ const { websiteId } = useParams<{
websiteId: string; websiteId: string;
}>(); }>();
const currentWorkspaceId = useCurrentWorkspaceId();
const { website, isLoading } = useWorkspaceWebsiteInfo( const { website, isLoading } = useWorkspaceWebsiteInfo(
currentWorkspaceId!, workspaceId,
websiteId! websiteId!
); );
const navigate = useNavigate(); 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 }) => { async (values: { name: string; domain: string }) => {
await updateWorkspaceWebsiteInfo(currentWorkspaceId!, websiteId!, { await updateMutation.mutateAsync({
workspaceId,
websiteId: websiteId!,
name: values.name, name: values.name,
domain: values.domain, domain: values.domain,
}); });
message.success('Save Success');
} }
); );
const [, handleDeleteWebsite] = useRequest(async () => { const [, handleDeleteWebsite] = useRequest(async () => {
await deleteWorkspaceWebsite(currentWorkspaceId!, websiteId!); await deleteWorkspaceWebsite(workspaceId, websiteId!);
message.success('Delete Success'); message.success('Delete Success');
navigate('/settings/websites'); navigate('/settings/websites');
}); });
if (!currentWorkspaceId) { if (!workspaceId) {
return <NoWorkspaceTip />; return <NoWorkspaceTip />;
} }

View File

@ -66,26 +66,6 @@ export async function getWorkspaceWebsiteInfo(
return websiteInfo; 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( export async function addWorkspaceWebsite(
workspaceId: string, workspaceId: string,
name: string, name: string,

View File

@ -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( workspaceRouter.delete(
'/:workspaceId/website/:websiteId', '/:workspaceId/website/:websiteId',
validate(param('workspaceId').isString(), param('websiteId').isString()), validate(param('workspaceId').isString(), param('websiteId').isString()),

View File

@ -1,4 +1,4 @@
import { router, workspaceProcedure } from '../trpc'; import { router, workspaceOwnerProcedure, workspaceProcedure } from '../trpc';
import { z } from 'zod'; import { z } from 'zod';
import { getWebsiteOnlineUserCount } from '../../model/website'; import { getWebsiteOnlineUserCount } from '../../model/website';
import { prisma } from '../../model/_client'; import { prisma } from '../../model/_client';
@ -154,4 +154,28 @@ export const websiteRouter = router({
return []; 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;
}),
}); });