index.tsx 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. import { Button, Form, FormInstance, Input, Select, Table, Image } 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 { CATEGORY_TYPE } from "./constants";
  7. import { collectionApi } from "@/api";
  8. const ALL_CATEGORY_TYPE = [
  9. {
  10. label: "全部",
  11. value: "",
  12. },
  13. ...CATEGORY_TYPE,
  14. ];
  15. const DEFAULT_PARAMS = {
  16. pageNum: 1,
  17. pageSize: 20,
  18. searchKey: "",
  19. dictType: ALL_CATEGORY_TYPE[0].value,
  20. };
  21. export default function CollectionPage() {
  22. const navigate = useNavigate();
  23. const [searchParams, setSearchParams] = useSearchParams();
  24. const formRef = useRef<FormInstance>(null);
  25. const [loading, setLoading] = useState(false);
  26. const [list, setList] = useState<[]>([]);
  27. const [params, setParams] = useState({
  28. ...DEFAULT_PARAMS,
  29. searchKey: searchParams.get("searchKey") || "",
  30. dictType: searchParams.get("dictType") || ALL_CATEGORY_TYPE[0].value,
  31. });
  32. const [total, setTotal] = useState(0);
  33. const getList = useCallback(async () => {
  34. setLoading(true);
  35. try {
  36. const data = await collectionApi.getList(params);
  37. setList(data.records);
  38. setTotal(data.total);
  39. } finally {
  40. setLoading(false);
  41. }
  42. }, [params]);
  43. const handleDelete = useCallback(
  44. async (id: number) => {
  45. await collectionApi.delete(id);
  46. getList();
  47. },
  48. [getList]
  49. );
  50. const COLUMNS = useMemo(() => {
  51. return [
  52. {
  53. title: "标题",
  54. dataIndex: "name",
  55. },
  56. {
  57. title: "年代",
  58. dataIndex: "dictAge",
  59. },
  60. {
  61. title: "级别",
  62. dataIndex: "dictLevel",
  63. },
  64. {
  65. title: "来源",
  66. dataIndex: "dictSource",
  67. },
  68. // {
  69. // title: "封面",
  70. // render: (item: any) => {
  71. // return (
  72. // <Image
  73. // height={60}
  74. // src={`${process.env.REACT_APP_API_URL}${process.env.REACT_APP_IMG_PUBLIC}${item.thumb}`}
  75. // />
  76. // );
  77. // },
  78. // },
  79. {
  80. title: "简介",
  81. width: 250,
  82. render: (item: any) => {
  83. return <p className="limit-line line-2">{item.description}</p>;
  84. },
  85. },
  86. {
  87. title: "发布日期",
  88. dataIndex: "publishDate",
  89. },
  90. {
  91. title: "操作",
  92. render: (item: any) => {
  93. return (
  94. <DageTableActions
  95. onEdit={() => navigate(`/collection/edit/${item.id}`)}
  96. onDelete={handleDelete.bind(undefined, item.id)}
  97. />
  98. );
  99. },
  100. },
  101. ];
  102. }, [navigate, handleDelete]);
  103. useEffect(() => {
  104. setSearchParams({
  105. searchKey: params.searchKey,
  106. dictType: params.dictType,
  107. });
  108. }, [params.searchKey, params.dictType, setSearchParams]);
  109. useEffect(() => {
  110. getList();
  111. }, [getList]);
  112. const handleReset = useCallback(() => {
  113. setParams({ ...DEFAULT_PARAMS });
  114. setTimeout(() => {
  115. formRef.current?.resetFields();
  116. });
  117. }, [formRef]);
  118. const debounceSearch = useMemo(
  119. () =>
  120. debounce((changedVal: unknown, vals: any) => {
  121. setParams({ ...params, ...vals });
  122. }, 500),
  123. [params]
  124. );
  125. const paginationChange = useCallback(
  126. () => (pageNum: number, pageSize: number) => {
  127. setParams({ ...params, pageNum, pageSize });
  128. },
  129. [params]
  130. );
  131. return (
  132. <div className="information">
  133. <Form
  134. ref={formRef}
  135. layout="inline"
  136. initialValues={params}
  137. onValuesChange={debounceSearch}
  138. >
  139. <Form.Item label="搜索项" name="searchKey">
  140. <Input placeholder="请输入" maxLength={10} showCount allowClear />
  141. </Form.Item>
  142. <Form.Item label="类别" name="dictType">
  143. <Select style={{ width: 220 }} options={ALL_CATEGORY_TYPE} />
  144. </Form.Item>
  145. <Form.Item>
  146. <Button type="primary" onClick={() => navigate("/collection/create")}>
  147. 新增
  148. </Button>
  149. </Form.Item>
  150. <Form.Item>
  151. <Button onClick={handleReset}>重置</Button>
  152. </Form.Item>
  153. </Form>
  154. <Table
  155. loading={loading}
  156. className="page-table"
  157. dataSource={list}
  158. columns={COLUMNS}
  159. rowKey="id"
  160. pagination={{
  161. showQuickJumper: true,
  162. position: ["bottomCenter"],
  163. showSizeChanger: true,
  164. current: params.pageNum,
  165. pageSize: params.pageSize,
  166. total,
  167. onChange: paginationChange(),
  168. }}
  169. />
  170. </div>
  171. );
  172. }