index.tsx 4.7 KB

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