index.tsx 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. import { FormPageFooter, MemoSpinLoding } from "@/components";
  2. import {
  3. DageFileCheckbox,
  4. DageFileCheckboxMethods,
  5. DageFileResponseType,
  6. DageUpload,
  7. DageUploadConsumer,
  8. DageUploadProvider,
  9. } from "@dage/pc-components";
  10. import { DatePicker, Form, FormInstance, Input, Select } from "antd";
  11. import { useCallback, useEffect, useRef, useState } from "react";
  12. import { useNavigate, useParams } from "react-router-dom";
  13. import { CATEGORY_TYPE, LEVEL_TYPE } from "../constants";
  14. import { collectionApi } from "@/api";
  15. import { dayjs, formatDate } from "@dage/utils";
  16. export default function CollectionCreateOrEditPage() {
  17. const dageFileCheckboxRef = useRef<DageFileCheckboxMethods>(null);
  18. const fileList = useRef<DageFileResponseType[]>([]);
  19. const formRef = useRef<FormInstance>(null);
  20. const navigate = useNavigate();
  21. const params = useParams();
  22. const [loading, setLoading] = useState(false);
  23. const getDetail = useCallback(async () => {
  24. setLoading(true);
  25. try {
  26. const {
  27. entity: { thumb, publishDate, ...rest },
  28. file,
  29. } = await collectionApi.getDetail(params.id as string);
  30. if (thumb) {
  31. rest.thumb = [
  32. {
  33. uid: thumb,
  34. url: `${process.env.REACT_APP_API_URL}${process.env.REACT_APP_IMG_PUBLIC}${thumb}`,
  35. name: thumb,
  36. status: "done",
  37. },
  38. ];
  39. }
  40. formRef.current?.setFieldsValue({
  41. publishDate: dayjs(publishDate),
  42. ...rest,
  43. });
  44. dageFileCheckboxRef.current?.setFileList(
  45. file.map((i: any) => ({
  46. uid: i.id + "",
  47. url: `${process.env.REACT_APP_API_URL}${process.env.REACT_APP_IMG_PUBLIC}${i.filePath}`,
  48. thumbUrl: `${process.env.REACT_APP_API_URL}${process.env.REACT_APP_IMG_PUBLIC}${i.filePath}`,
  49. name: i.fileName,
  50. dType: i.type,
  51. status: "done",
  52. }))
  53. );
  54. } finally {
  55. setLoading(false);
  56. }
  57. }, [params.id]);
  58. useEffect(() => {
  59. !!params.id && getDetail();
  60. }, [getDetail, params.id]);
  61. const handleCancel = useCallback(() => {
  62. navigate(-1);
  63. }, [navigate]);
  64. const handleSubmit = useCallback(async () => {
  65. if (!(await formRef.current?.validateFields())) return;
  66. const {
  67. thumb = [],
  68. publishDate,
  69. ...rest
  70. } = formRef.current?.getFieldsValue();
  71. if (params.id) {
  72. rest.id = params.id;
  73. }
  74. await collectionApi.save({
  75. ...rest,
  76. publishDate: formatDate(publishDate),
  77. thumb: thumb[0].response ? thumb[0].response.filePath : thumb[0].name,
  78. fileIds: fileList.current
  79. .map((i) => (i.response ? i.response.id : i.uid))
  80. .join(),
  81. });
  82. handleCancel();
  83. }, [handleCancel, params]);
  84. const handleFileChange = useCallback((list: DageFileResponseType[]) => {
  85. fileList.current = list;
  86. }, []);
  87. return (
  88. <div style={{ position: "relative" }}>
  89. {loading && <MemoSpinLoding />}
  90. <DageUploadProvider>
  91. <DageUploadConsumer>
  92. {(data) => (
  93. <>
  94. <Form
  95. ref={formRef}
  96. labelCol={{ span: 2 }}
  97. // initialValues={{
  98. // dictLevel: LEVEL_TYPE[0].value,
  99. // }}
  100. >
  101. <Form.Item
  102. label="标题"
  103. name="name"
  104. rules={[{ required: true, message: "请输入内容" }]}
  105. >
  106. <Input
  107. className="w220"
  108. placeholder="请输入内容"
  109. maxLength={20}
  110. showCount
  111. />
  112. </Form.Item>
  113. <Form.Item label="类别" name="dictType">
  114. <Select
  115. style={{ width: 220 }}
  116. options={CATEGORY_TYPE}
  117. placeholder="请选择"
  118. />
  119. </Form.Item>
  120. <Form.Item label="时代" name="dictAge">
  121. <Input
  122. className="w220"
  123. placeholder="请输入内容"
  124. maxLength={10}
  125. showCount
  126. />
  127. </Form.Item>
  128. <Form.Item label="质地" name="dictTexture">
  129. <Input
  130. className="w220"
  131. placeholder="请输入内容"
  132. maxLength={10}
  133. showCount
  134. />
  135. </Form.Item>
  136. <Form.Item label="级别" name="dictLevel">
  137. <Select
  138. style={{ width: 220 }}
  139. options={LEVEL_TYPE}
  140. placeholder="请选择"
  141. />
  142. </Form.Item>
  143. <Form.Item label="来源" name="dictSource">
  144. <Input
  145. className="w220"
  146. placeholder="请输入内容"
  147. maxLength={10}
  148. showCount
  149. />
  150. </Form.Item>
  151. <Form.Item label="简介" name="description">
  152. <Input.TextArea
  153. className="w450"
  154. placeholder="请输入内容,最多200字"
  155. maxLength={200}
  156. showCount
  157. rows={6}
  158. />
  159. </Form.Item>
  160. <Form.Item
  161. label="封面图"
  162. name="thumb"
  163. rules={[{ required: true, message: "请上传封面图" }]}
  164. >
  165. <DageUpload
  166. tips="支持png、jpg和jpeg格式;最多1张,最大20M"
  167. action={
  168. process.env.REACT_APP_API_URL + "/api/cms/goods/upload"
  169. }
  170. maxSize={20}
  171. maxCount={1}
  172. />
  173. </Form.Item>
  174. <Form.Item
  175. label="文件类型"
  176. name="fileType"
  177. rules={[
  178. { required: true, message: "" },
  179. {
  180. validator: (...args) => {
  181. dageFileCheckboxRef.current!.validate(...args);
  182. },
  183. },
  184. ]}
  185. validateTrigger="onSubmit"
  186. >
  187. <DageFileCheckbox
  188. ref={dageFileCheckboxRef}
  189. action={
  190. process.env.REACT_APP_API_URL + "/api/cms/goods/upload"
  191. }
  192. onFileChange={handleFileChange}
  193. />
  194. </Form.Item>
  195. <Form.Item
  196. label="发布日期"
  197. name="publishDate"
  198. rules={[{ required: true, message: "请选择发布日期" }]}
  199. >
  200. <DatePicker className="w220" format="YYYY-MM-DD" />
  201. </Form.Item>
  202. </Form>
  203. {!loading && (
  204. <FormPageFooter
  205. disabled={data?.uploading}
  206. onSubmit={handleSubmit}
  207. onCancel={handleCancel}
  208. />
  209. )}
  210. </>
  211. )}
  212. </DageUploadConsumer>
  213. </DageUploadProvider>
  214. </div>
  215. );
  216. }