index.tsx 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. import { Button, Form, FormInstance, Input, Table } from "antd";
  2. import { useCallback, useEffect, useMemo, useRef, useState } from "react";
  3. import { debounce } from "lodash";
  4. import { DageTableActions } from "@dage/pc-components";
  5. import { messageApi } from "@/api";
  6. const DEFAULT_PARAMS = {
  7. pageNum: 1,
  8. pageSize: 20,
  9. searchKey: "",
  10. };
  11. export default function MessagePage() {
  12. const formRef = useRef<FormInstance>(null);
  13. const [loading, setLoading] = useState(false);
  14. const [list, setList] = useState<[]>([]);
  15. const [params, setParams] = useState({
  16. ...DEFAULT_PARAMS,
  17. });
  18. const [total, setTotal] = useState(0);
  19. const getList = useCallback(async () => {
  20. setLoading(true);
  21. try {
  22. const data = await messageApi.getList(params);
  23. setList(data.records);
  24. setTotal(data.total);
  25. } finally {
  26. setLoading(false);
  27. }
  28. }, [params]);
  29. useEffect(() => {
  30. getList();
  31. }, [getList]);
  32. const handleDelete = useCallback(
  33. async (id: number) => {
  34. await messageApi.delete(id);
  35. getList();
  36. },
  37. [getList]
  38. );
  39. const COLUMNS = useMemo(() => {
  40. return [
  41. {
  42. title: "留言时间",
  43. dataIndex: "updateTime",
  44. },
  45. {
  46. title: "称号",
  47. dataIndex: "name",
  48. },
  49. {
  50. title: "联系方式",
  51. dataIndex: "phone",
  52. },
  53. {
  54. title: "正文",
  55. dataIndex: "content",
  56. },
  57. {
  58. title: "操作",
  59. render: (item: any) => {
  60. return (
  61. <DageTableActions
  62. showEdit={false}
  63. onDelete={handleDelete.bind(undefined, item.id)}
  64. />
  65. );
  66. },
  67. },
  68. ];
  69. }, [handleDelete]);
  70. const handleReset = useCallback(() => {
  71. setParams({ ...DEFAULT_PARAMS });
  72. setTimeout(() => {
  73. formRef.current?.resetFields();
  74. });
  75. }, [formRef]);
  76. const debounceSearch = useMemo(
  77. () =>
  78. debounce((changedVal: unknown, vals: any) => {
  79. setParams({ ...params, ...vals });
  80. }, 500),
  81. [params]
  82. );
  83. const paginationChange = useCallback(
  84. () => (pageNum: number, pageSize: number) => {
  85. setParams({ ...params, pageNum, pageSize });
  86. },
  87. [params]
  88. );
  89. return (
  90. <div className="information">
  91. <Form ref={formRef} layout="inline" onValuesChange={debounceSearch}>
  92. <Form.Item label="搜索项" name="searchKey">
  93. <Input placeholder="请输入" maxLength={10} showCount allowClear />
  94. </Form.Item>
  95. <Form.Item>
  96. <Button onClick={handleReset}>重置</Button>
  97. </Form.Item>
  98. </Form>
  99. <Table
  100. loading={loading}
  101. className="page-table"
  102. dataSource={list}
  103. columns={COLUMNS}
  104. rowKey="id"
  105. pagination={{
  106. showQuickJumper: true,
  107. position: ["bottomCenter"],
  108. showSizeChanger: true,
  109. current: params.pageNum,
  110. pageSize: params.pageSize,
  111. total,
  112. onChange: paginationChange(),
  113. }}
  114. />
  115. </div>
  116. );
  117. }