2023-09-27 12:45:19 +00:00
|
|
|
import { DeleteOutlined, EditOutlined, PlusOutlined } from '@ant-design/icons';
|
|
|
|
import { Button, List, Popconfirm } from 'antd';
|
2023-09-20 16:19:37 +00:00
|
|
|
import React, { useState } from 'react';
|
2023-09-27 12:27:46 +00:00
|
|
|
import { trpc } from '../../api/trpc';
|
2023-09-27 08:57:46 +00:00
|
|
|
import {
|
|
|
|
NotificationFormValues,
|
|
|
|
NotificationInfoModal,
|
|
|
|
} from '../../components/modals/NotificationInfo';
|
2023-09-27 12:27:46 +00:00
|
|
|
import { NoWorkspaceTip } from '../../components/NoWorkspaceTip';
|
2023-09-20 16:19:37 +00:00
|
|
|
import { PageHeader } from '../../components/PageHeader';
|
|
|
|
import { useEvent } from '../../hooks/useEvent';
|
2023-09-27 12:27:46 +00:00
|
|
|
import { useCurrentWorkspaceId } from '../../store/user';
|
2023-09-20 16:19:37 +00:00
|
|
|
|
|
|
|
export const NotificationList: React.FC = React.memo(() => {
|
|
|
|
const [open, setOpen] = useState(false);
|
2023-09-27 12:27:46 +00:00
|
|
|
const currentWorkspaceId = useCurrentWorkspaceId();
|
2023-10-19 16:28:46 +00:00
|
|
|
const { data: list = [], refetch } = trpc.notification.all.useQuery({
|
2023-09-27 12:27:46 +00:00
|
|
|
workspaceId: currentWorkspaceId!,
|
|
|
|
});
|
|
|
|
const [editingFormData, setEditingFormData] = useState<
|
|
|
|
NotificationFormValues | undefined
|
|
|
|
>(undefined);
|
|
|
|
|
2023-09-27 12:45:19 +00:00
|
|
|
const upsertMutation = trpc.notification.upsert.useMutation();
|
|
|
|
const deleteMutation = trpc.notification.delete.useMutation();
|
2023-09-27 12:27:46 +00:00
|
|
|
|
|
|
|
const handleOpenModal = useEvent((initValues?: NotificationFormValues) => {
|
|
|
|
setEditingFormData(initValues);
|
|
|
|
setOpen(true);
|
|
|
|
});
|
2023-09-20 16:19:37 +00:00
|
|
|
|
2023-09-27 12:27:46 +00:00
|
|
|
const handleCloseModal = useEvent(() => {
|
|
|
|
setEditingFormData(undefined);
|
2023-09-20 16:19:37 +00:00
|
|
|
setOpen(false);
|
|
|
|
});
|
|
|
|
|
2023-09-27 12:27:46 +00:00
|
|
|
const handleSubmit = useEvent(async (values: NotificationFormValues) => {
|
2023-09-27 12:45:19 +00:00
|
|
|
await upsertMutation.mutateAsync({
|
2023-09-27 12:27:46 +00:00
|
|
|
workspaceId: currentWorkspaceId!,
|
|
|
|
...values,
|
|
|
|
});
|
|
|
|
handleCloseModal();
|
|
|
|
refetch();
|
|
|
|
});
|
|
|
|
|
2023-09-27 12:45:19 +00:00
|
|
|
const handleDelete = useEvent(async (notificationId: string) => {
|
|
|
|
await deleteMutation.mutateAsync({
|
|
|
|
workspaceId: currentWorkspaceId!,
|
|
|
|
id: notificationId,
|
|
|
|
});
|
|
|
|
refetch();
|
|
|
|
});
|
|
|
|
|
2023-09-27 12:27:46 +00:00
|
|
|
if (!currentWorkspaceId) {
|
|
|
|
return <NoWorkspaceTip />;
|
|
|
|
}
|
|
|
|
|
2023-09-20 16:19:37 +00:00
|
|
|
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()}
|
2023-09-20 16:19:37 +00:00
|
|
|
>
|
|
|
|
New
|
|
|
|
</Button>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<List
|
|
|
|
bordered={true}
|
2023-09-27 12:27:46 +00:00
|
|
|
dataSource={list}
|
2023-09-20 16:19:37 +00:00
|
|
|
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>,
|
2023-09-27 12:45:19 +00:00
|
|
|
<Popconfirm
|
|
|
|
title="Is delete this item?"
|
|
|
|
okButtonProps={{
|
|
|
|
danger: true,
|
|
|
|
}}
|
|
|
|
onConfirm={() => {
|
|
|
|
handleDelete(item.id);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<Button danger={true} icon={<DeleteOutlined />} />
|
|
|
|
</Popconfirm>,
|
2023-09-27 12:27:46 +00:00
|
|
|
]}
|
|
|
|
>
|
2023-09-20 16:19:37 +00:00
|
|
|
<List.Item.Meta title={item.name} />
|
|
|
|
</List.Item>
|
|
|
|
)}
|
|
|
|
/>
|
|
|
|
|
|
|
|
<NotificationInfoModal
|
|
|
|
open={open}
|
2023-09-27 12:27:46 +00:00
|
|
|
initialValues={editingFormData}
|
2023-09-27 08:57:46 +00:00
|
|
|
onSubmit={handleSubmit}
|
2023-09-27 12:27:46 +00:00
|
|
|
onCancel={() => handleCloseModal()}
|
2023-09-20 16:19:37 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
NotificationList.displayName = 'NotificationList';
|