tianji/src/client/pages/Website.tsx

121 lines
3.1 KiB
TypeScript
Raw Normal View History

import {
BarChartOutlined,
EditOutlined,
PlusOutlined,
} from '@ant-design/icons';
import { Button, Form, Input, Modal, Table } from 'antd';
import { ColumnsType } from 'antd/es/table';
import React, { useMemo, useState } from 'react';
2023-09-04 17:18:43 +00:00
import {
addWorkspaceWebsite,
refreshWorkspaceWebsites,
useWorspaceWebsites,
WebsiteInfo,
} from '../api/model/website';
import { Loading } from '../components/Loading';
import { NoWorkspaceTip } from '../components/NoWorkspaceTip';
import { useRequest } from '../hooks/useRequest';
import { useUserStore } from '../store/user';
2023-08-31 16:11:47 +00:00
export const Website: React.FC = React.memo(() => {
const [isModalOpen, setIsModalOpen] = useState(false);
2023-09-04 17:18:43 +00:00
const currentWorkspace = useUserStore(
(state) => state.info?.currentWorkspace
);
const [form] = Form.useForm();
2023-09-04 17:18:43 +00:00
const [{ loading }, handleAddWebsite] = useRequest(async () => {
await form.validateFields();
const values = form.getFieldsValue();
await addWorkspaceWebsite(currentWorkspace!.id, values.name, values.domain);
refreshWorkspaceWebsites(currentWorkspace!.id);
setIsModalOpen(false);
2023-09-04 17:18:43 +00:00
form.resetFields();
});
if (!currentWorkspace) {
return <NoWorkspaceTip />;
}
return (
<div>
<div className="h-24 flex items-center">
2023-09-04 17:18:43 +00:00
<div className="text-2xl flex-1">Websites</div>
<div>
<Button
type="primary"
icon={<PlusOutlined />}
size="large"
onClick={() => setIsModalOpen(true)}
>
Add Website
</Button>
</div>
</div>
2023-09-04 17:18:43 +00:00
<WebsiteList workspaceId={currentWorkspace.id} />
<Modal
title="Add Server"
open={isModalOpen}
2023-09-04 17:18:43 +00:00
okButtonProps={{
loading,
}}
onOk={() => handleAddWebsite()}
onCancel={() => setIsModalOpen(false)}
>
2023-09-04 17:18:43 +00:00
<Form layout="vertical" form={form}>
<Form.Item
label="Server Name"
name="name"
rules={[{ required: true }]}
>
<Input />
</Form.Item>
2023-09-04 17:18:43 +00:00
<Form.Item label="Domain" name="domain" rules={[{ required: true }]}>
<Input />
</Form.Item>
</Form>
</Modal>
</div>
);
2023-08-31 16:11:47 +00:00
});
Website.displayName = 'Website';
2023-09-04 17:18:43 +00:00
const WebsiteList: React.FC<{ workspaceId: string }> = React.memo((props) => {
const { websites, isLoading } = useWorspaceWebsites(props.workspaceId);
2023-09-04 17:18:43 +00:00
const columns = useMemo((): ColumnsType<WebsiteInfo> => {
return [
{
dataIndex: 'name',
title: 'Name',
},
{
dataIndex: 'domain',
title: 'Domain',
},
{
key: 'action',
render: () => {
return (
<div className="flex gap-2 justify-end">
<Button icon={<EditOutlined />}>Edit</Button>
<Button icon={<BarChartOutlined />}>View</Button>
</div>
);
},
},
];
}, []);
2023-09-04 17:18:43 +00:00
if (isLoading) {
return <Loading />;
}
return <Table columns={columns} dataSource={websites} pagination={false} />;
});
WebsiteList.displayName = 'WebsiteList';