index.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 { useNavigate, useSearchParams } from "react-router-dom";
  6. import { questionaireApi } from "@/api";
  7. const DEFAULT_PARAMS = {
  8. pageNum: 1,
  9. pageSize: 20,
  10. searchKey: "",
  11. };
  12. export default function InformationPage() {
  13. const navigate = useNavigate();
  14. const [searchParams, setSearchParams] = useSearchParams();
  15. const formRef = useRef<FormInstance>(null);
  16. const [loading, setLoading] = useState(false);
  17. const [list, setList] = useState<[]>([]);
  18. const [params, setParams] = useState({
  19. ...DEFAULT_PARAMS,
  20. searchKey: searchParams.get("searchKey") || "",
  21. });
  22. const [total, setTotal] = useState(0);
  23. useEffect(() => {
  24. setSearchParams({
  25. searchKey: params.searchKey,
  26. });
  27. }, [params.searchKey, setSearchParams]);
  28. const getList = useCallback(async () => {
  29. setLoading(true);
  30. try {
  31. const data = await questionaireApi.getList(params);
  32. setList(data.records);
  33. setTotal(data.total);
  34. } finally {
  35. setLoading(false);
  36. }
  37. }, [params]);
  38. useEffect(() => {
  39. getList();
  40. }, [getList]);
  41. const handleDelete = useCallback(
  42. async (id: number) => {
  43. await questionaireApi.delete(id);
  44. getList();
  45. },
  46. [getList]
  47. );
  48. const COLUMNS = useMemo(() => {
  49. return [
  50. {
  51. title: "标题",
  52. dataIndex: "name",
  53. },
  54. {
  55. title: "问题数量",
  56. dataIndex: "pcsQuestion",
  57. },
  58. {
  59. title: "已收集份数",
  60. dataIndex: "pcsGather",
  61. },
  62. {
  63. title: "发布日期",
  64. dataIndex: "publishDate",
  65. },
  66. {
  67. title: "展示状态",
  68. render: (item: any) => {
  69. return item.display ? "展示" : "不展示";
  70. },
  71. },
  72. {
  73. title: "操作",
  74. render: (item: any) => {
  75. return (
  76. <DageTableActions
  77. onEdit={() => navigate(`/questionnaire/edit/${item.id}`)}
  78. onDelete={handleDelete.bind(undefined, item.id)}
  79. />
  80. );
  81. },
  82. },
  83. ];
  84. }, [navigate, handleDelete]);
  85. const handleReset = useCallback(() => {
  86. formRef.current?.resetFields();
  87. }, [formRef]);
  88. const debounceSearch = useMemo(
  89. () =>
  90. debounce((changedVal: unknown, vals: any) => {
  91. setParams({ ...params, ...vals });
  92. }, 500),
  93. [params]
  94. );
  95. const paginationChange = useCallback(
  96. () => (pageNum: number, pageSize: number) => {
  97. setParams({ ...params, pageNum, pageSize });
  98. },
  99. [params]
  100. );
  101. return (
  102. <div className="information">
  103. <Form
  104. ref={formRef}
  105. layout="inline"
  106. initialValues={params}
  107. onValuesChange={debounceSearch}
  108. >
  109. <Form.Item label="搜索项" name="searchKey">
  110. <Input
  111. placeholder="请输入标题,最多10字"
  112. maxLength={10}
  113. showCount
  114. allowClear
  115. />
  116. </Form.Item>
  117. <Form.Item>
  118. <Button
  119. type="primary"
  120. onClick={() => navigate("/questionnaire/create")}
  121. >
  122. 新增
  123. </Button>
  124. </Form.Item>
  125. <Form.Item>
  126. <Button onClick={handleReset}>重置</Button>
  127. </Form.Item>
  128. </Form>
  129. <Table
  130. loading={loading}
  131. className="page-table"
  132. dataSource={list}
  133. columns={COLUMNS}
  134. rowKey="id"
  135. pagination={{
  136. showQuickJumper: true,
  137. position: ["bottomCenter"],
  138. showSizeChanger: true,
  139. current: params.pageNum,
  140. pageSize: params.pageSize,
  141. total,
  142. onChange: paginationChange(),
  143. }}
  144. />
  145. </div>
  146. );
  147. }