tianji/src/client/components/WebsiteInfo.tsx

128 lines
3.5 KiB
TypeScript
Raw Normal View History

2023-09-06 06:14:12 +00:00
import { Button, Form, Input, message, Popconfirm, Tabs } from 'antd';
2023-09-05 07:32:16 +00:00
import React from 'react';
2023-09-06 06:14:12 +00:00
import { useNavigate, useParams } from 'react-router';
2023-09-05 07:32:16 +00:00
import {
2023-09-06 06:14:12 +00:00
deleteWorkspaceWebsite,
2023-09-05 07:32:16 +00:00
useWorkspaceWebsiteInfo,
} from '../api/model/website';
import { useRequest } from '../hooks/useRequest';
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';
2023-09-05 07:32:16 +00:00
export const WebsiteInfo: React.FC = React.memo(() => {
const workspaceId = useCurrentWorkspaceId();
2023-09-05 07:32:16 +00:00
const { websiteId } = useParams<{
websiteId: string;
}>();
const { website, isLoading } = useWorkspaceWebsiteInfo(
workspaceId,
2023-09-05 07:32:16 +00:00
websiteId!
);
2023-09-06 06:14:12 +00:00
const navigate = useNavigate();
const queryClient = useQueryClient();
2023-09-05 07:32:16 +00:00
const updateMutation = trpc.website.updateInfo.useMutation({
onSuccess: () => {
queryClient.resetQueries(['websites', workspaceId]); // TODO: translation to trpc
defaultSuccessHandler();
},
onError: defaultErrorHandler,
});
const handleSave = useEvent(
2023-09-05 07:32:16 +00:00
async (values: { name: string; domain: string }) => {
await updateMutation.mutateAsync({
workspaceId,
websiteId: websiteId!,
2023-09-05 07:32:16 +00:00
name: values.name,
domain: values.domain,
});
}
);
2023-09-06 06:14:12 +00:00
const [, handleDeleteWebsite] = useRequest(async () => {
await deleteWorkspaceWebsite(workspaceId, websiteId!);
2023-09-06 06:14:12 +00:00
message.success('Delete Success');
navigate('/settings/websites');
});
if (!workspaceId) {
2023-09-05 07:32:16 +00:00
return <NoWorkspaceTip />;
}
if (!websiteId) {
return <ErrorTip />;
}
if (isLoading) {
return <Loading />;
}
if (!website) {
return <ErrorTip />;
}
return (
<div>
<div className="h-24 flex items-center">
<div className="text-2xl flex-1">Website Info</div>
</div>
<div>
2023-09-06 06:14:12 +00:00
<Tabs>
<Tabs.TabPane key={'detail'} tab={'Detail'}>
<Form
layout="vertical"
initialValues={{
id: website.id,
name: website.name,
domain: website.domain,
}}
onFinish={handleSave}
>
<Form.Item label="Website ID" name="id">
<Input size="large" disabled={true} />
</Form.Item>
<Form.Item label="Name" name="name" rules={[{ required: true }]}>
<Input size="large" />
</Form.Item>
<Form.Item
label="Domain"
name="domain"
rules={[{ required: true }]}
>
<Input size="large" />
</Form.Item>
<Form.Item>
<Button size="large" htmlType="submit">
Save
</Button>
</Form.Item>
</Form>
</Tabs.TabPane>
2023-09-05 07:32:16 +00:00
2023-09-06 06:14:12 +00:00
<Tabs.TabPane key={'data'} tab={'Data'}>
<Popconfirm
title="Delete Website"
onConfirm={() => handleDeleteWebsite()}
>
<Button type="primary" danger={true}>
Delete Website
</Button>
</Popconfirm>
</Tabs.TabPane>
</Tabs>
2023-09-05 07:32:16 +00:00
</div>
</div>
);
});
WebsiteInfo.displayName = 'WebsiteInfo';