Files
ThriveX-Admin/src/pages/Comment/index.tsx

166 lines
6.0 KiB
TypeScript
Raw Normal View History

2024-08-07 17:07:14 +08:00
import { useState, useEffect } from 'react';
2024-08-07 23:30:01 +08:00
import { Spin, Card, message, Table, Popconfirm, Button, Tag, Modal } from 'antd';
2024-08-06 23:21:42 +08:00
import { getCommentListAPI } from '@/api/Comment';
2024-08-07 17:07:14 +08:00
import { auditCommentDataAPI, delCommentDataAPI } from '@/api/Comment';
import { ColumnsType } from 'antd/es/table';
import { titleSty } from '@/styles/sty';
import Title from '@/components/Title';
2024-08-15 15:28:58 +08:00
import { Comment } from '@/types/app/comment'
2024-08-17 15:34:39 +08:00
import dayjs from 'dayjs';
2024-08-06 23:21:42 +08:00
2024-08-07 18:43:07 +08:00
const CommentPage = () => {
2024-08-07 17:07:14 +08:00
const [loading, setLoading] = useState(false);
const [comment, setComment] = useState<Comment>();
const [list, setList] = useState<Comment[]>([]);
2024-08-06 23:21:42 +08:00
2024-08-07 18:43:07 +08:00
const [isModalOpen, setIsModalOpen] = useState(false);
2024-08-07 17:07:14 +08:00
const getCommentList = async () => {
const { data } = await getCommentListAPI();
2024-08-14 13:16:14 +08:00
// 根据时间排序:最新时间在前
const sortedData = (data as Comment[]).sort((a, b) => +b.createTime - +a.createTime);
setList(sortedData as Comment[])
2024-08-07 17:07:14 +08:00
setLoading(false)
}
2024-08-06 23:21:42 +08:00
2024-08-07 17:07:14 +08:00
const auditCommentData = async (id: number) => {
setLoading(true)
await auditCommentDataAPI(id);
2024-08-06 23:21:42 +08:00
getCommentList();
2024-08-07 17:07:14 +08:00
message.success('🎉 审核评论成功');
};
2024-08-06 23:21:42 +08:00
2024-08-07 17:07:14 +08:00
const delCommentData = async (id: number) => {
setLoading(true)
await delCommentDataAPI(id);
2024-08-06 23:21:42 +08:00
getCommentList();
2024-08-07 17:07:14 +08:00
message.success('🎉 删除评论成功');
2024-08-06 23:21:42 +08:00
};
2024-08-07 17:07:14 +08:00
useEffect(() => {
setLoading(true)
getCommentList();
}, []);
const columns: ColumnsType = [
{
title: 'ID',
dataIndex: 'id',
key: 'id',
align: "center"
},
{
title: '状态',
dataIndex: 'auditStatus',
key: 'auditStatus',
fixed: 'left',
render: (status: number) => status ?
<Tag bordered={false} color="processing"></Tag>
: <Tag bordered={false} color="error"></Tag>
},
{
title: '名称',
dataIndex: 'name',
key: 'name',
fixed: 'left',
},
{
title: '邮箱',
dataIndex: 'email',
key: 'email',
},
{
title: '内容',
dataIndex: 'content',
key: 'content',
2024-08-07 18:43:07 +08:00
render: (text: string, record) => <span className="hover:text-primary cursor-pointer" onClick={() => {
setComment(record)
setIsModalOpen(true)
}}>{text}</span>
2024-08-07 17:07:14 +08:00
},
{
title: '网站',
dataIndex: 'url',
key: 'url',
render: (url: string) => url ? <a href={url} className="hover:text-primary">{url}</a> : '无网站',
},
{
title: '所属文章',
dataIndex: 'articleTitle',
key: 'articleTitle',
},
{
title: '评论时间',
dataIndex: 'createTime',
key: 'createTime',
render: (date: string) => dayjs(date).format('YYYY-MM-DD HH:mm:ss'),
},
{
title: '操作',
key: 'action',
fixed: 'right',
align: 'center',
2024-08-07 18:43:07 +08:00
render: (text: string, record: Comment) => (
2024-08-07 23:30:01 +08:00
<div className='flex justify-center space-x-2'>
{
!record.auditStatus
? <Button type='primary' onClick={() => {
setComment(record)
setIsModalOpen(true)
}}></Button>
: <Button onClick={() => {
setComment(record)
setIsModalOpen(true)
}}></Button>
}
2024-08-07 18:43:07 +08:00
2024-08-07 17:07:14 +08:00
<Popconfirm title="警告" description="你确定要删除吗" okText="确定" cancelText="取消" onConfirm={() => delCommentData(record.id)}>
<Button type="primary" danger></Button>
</Popconfirm>
</div>
),
},
];
2024-08-07 13:02:59 +08:00
2024-08-06 23:21:42 +08:00
return (
2024-08-07 17:07:14 +08:00
<>
<Title value='评论管理' />
<Card className={`${titleSty} mt-2`}>
<Spin spinning={loading} indicator={<svg />}>
2024-08-13 21:04:46 +08:00
{list.length && <Table
2024-08-07 17:07:14 +08:00
rowKey="id"
dataSource={list}
columns={columns}
2024-08-13 20:53:49 +08:00
expandable={{ defaultExpandAllRows: true }}
2024-08-07 17:07:14 +08:00
scroll={{ x: 'max-content' }}
pagination={{
position: ['bottomCenter'],
pageSize: 8
}}
2024-08-13 21:04:46 +08:00
/>}
2024-08-07 17:07:14 +08:00
</Spin>
</Card>
2024-08-07 18:43:07 +08:00
<Modal title='评论详情' open={isModalOpen} onCancel={() => setIsModalOpen(false)} footer={null}>
<div className='pt-2 space-y-2'>
<div><b></b> {comment?.articleTitle}</div>
<div><b></b> {dayjs(comment?.createTime).format("YYYY-MM-DD HH:mm:ss")}</div>
<div><b></b> {comment?.name}</div>
<div><b></b> {comment?.email}</div>
<div><b></b> {comment?.url ? <a href={comment?.url} className="hover:text-primary">{comment?.url}</a> : '无网站'}</div>
<div><b></b> {comment?.content}</div>
<div><b></b> {comment?.auditStatus
? <Tag bordered={false} color="processing"></Tag>
: <Tag bordered={false} color="error"></Tag>}
</div>
{!comment?.auditStatus ? <Button type="primary" className='w-full !mt-4' onClick={() => auditCommentData(1)}></Button> : null}
</div>
</Modal>
2024-08-07 17:07:14 +08:00
</>
2024-08-06 23:21:42 +08:00
);
};
2024-08-07 18:43:07 +08:00
export default CommentPage;