tianji/src/client/pages/Settings/NotificationList.tsx

104 lines
2.8 KiB
TypeScript
Raw Normal View History

2023-09-27 12:27:46 +00:00
import { EditOutlined, PlusOutlined } from '@ant-design/icons';
import { Button, List } from 'antd';
import React, { useState } from 'react';
2023-09-27 12:27:46 +00:00
import { trpc } from '../../api/trpc';
import {
NotificationFormValues,
NotificationInfoModal,
} from '../../components/modals/NotificationInfo';
2023-09-27 12:27:46 +00:00
import { NoWorkspaceTip } from '../../components/NoWorkspaceTip';
import { PageHeader } from '../../components/PageHeader';
import { useEvent } from '../../hooks/useEvent';
2023-09-27 12:27:46 +00:00
import { useCurrentWorkspaceId } from '../../store/user';
export const NotificationList: React.FC = React.memo(() => {
const [open, setOpen] = useState(false);
2023-09-27 12:27:46 +00:00
const currentWorkspaceId = useCurrentWorkspaceId();
const { data: list = [], refetch } = trpc.notification.getAll.useQuery({
workspaceId: currentWorkspaceId!,
});
const [editingFormData, setEditingFormData] = useState<
NotificationFormValues | undefined
>(undefined);
const mutation = trpc.notification.upsert.useMutation();
const handleOpenModal = useEvent((initValues?: NotificationFormValues) => {
console.log('initValues', initValues);
setEditingFormData(initValues);
setOpen(true);
});
2023-09-27 12:27:46 +00:00
const handleCloseModal = useEvent(() => {
setEditingFormData(undefined);
setOpen(false);
});
2023-09-27 12:27:46 +00:00
const handleSubmit = useEvent(async (values: NotificationFormValues) => {
await mutation.mutateAsync({
workspaceId: currentWorkspaceId!,
...values,
});
handleCloseModal();
refetch();
});
if (!currentWorkspaceId) {
return <NoWorkspaceTip />;
}
return (
<div>
<PageHeader
title="Notification List"
action={
<div>
<Button
type="primary"
icon={<PlusOutlined />}
size="large"
2023-09-27 12:27:46 +00:00
onClick={() => handleOpenModal()}
>
New
</Button>
</div>
}
/>
<List
bordered={true}
2023-09-27 12:27:46 +00:00
dataSource={list}
renderItem={(item) => (
2023-09-27 12:27:46 +00:00
<List.Item
actions={[
<Button
icon={<EditOutlined />}
onClick={() => {
handleOpenModal({
id: item.id,
name: item.name,
type: item.type,
payload: item.payload as Record<string, any>,
});
}}
>
Edit
</Button>,
]}
>
<List.Item.Meta title={item.name} />
</List.Item>
)}
/>
<NotificationInfoModal
open={open}
2023-09-27 12:27:46 +00:00
initialValues={editingFormData}
onSubmit={handleSubmit}
2023-09-27 12:27:46 +00:00
onCancel={() => handleCloseModal()}
/>
</div>
);
});
NotificationList.displayName = 'NotificationList';