index.tsx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import React, { useCallback, useEffect, useState } from "react";
  2. import styles from "./index.module.scss";
  3. import { Button, Checkbox, Empty, Modal } from "antd";
  4. import { A1_APIdownBtnOk, A1_APIgetDownList } from "@/store/action/A1Project";
  5. import { A1getStorage, A1setStorage } from "@/utils/storage";
  6. import { MessageFu } from "@/utils/message";
  7. import { urlChangeFu } from "@/utils/authFilesLook";
  8. type SelectType = {
  9. attrId: number;
  10. name: string;
  11. hasUpload: boolean;
  12. done: boolean;
  13. };
  14. type Props = {
  15. downId: { id: number; txt: string };
  16. closeFu: () => void;
  17. };
  18. function A1Down({ downId, closeFu }: Props) {
  19. const getInfoFu = useCallback(async (id: number) => {
  20. const res = await A1_APIgetDownList(id);
  21. if (res.code === 0) {
  22. let arr: SelectType[] = res.data.filter((v: SelectType) => v.hasUpload);
  23. const storeIds = A1getStorage();
  24. if (storeIds) {
  25. arr = arr.map((v) => ({
  26. ...v,
  27. done: storeIds.includes(v.attrId) ? true : false,
  28. }));
  29. }
  30. setCheckedList(arr);
  31. }
  32. }, []);
  33. useEffect(() => {
  34. getInfoFu(downId.id);
  35. }, [getInfoFu, downId.id]);
  36. const [checkedList, setCheckedList] = useState([] as SelectType[]);
  37. // 全选的状态
  38. const [indeterminate, setIndeterminate] = useState(false);
  39. const [checkAll, setCheckAll] = useState(false);
  40. const onCheckAllChange = useCallback(
  41. (val: boolean) => {
  42. const newArr = checkedList.map((v) => ({
  43. ...v,
  44. done: val,
  45. }));
  46. setCheckedList(newArr);
  47. setCheckAll(val);
  48. },
  49. [checkedList]
  50. );
  51. const onChange = useCallback(
  52. (val: boolean, id: number) => {
  53. setCheckedList(
  54. checkedList.map((v) => ({
  55. ...v,
  56. done: v.attrId === id ? val : v.done,
  57. }))
  58. );
  59. },
  60. [checkedList]
  61. );
  62. useEffect(() => {
  63. const newArr = checkedList.filter((v) => v.done);
  64. setIndeterminate(!!newArr.length && newArr.length < checkedList.length);
  65. setCheckAll(newArr.length === checkedList.length);
  66. }, [checkedList]);
  67. // 点击提交
  68. const btnOkFu = useCallback(async () => {
  69. const obj = {
  70. projectId: downId.id,
  71. attrId: checkedList.map((v) => v.attrId),
  72. };
  73. const res = await A1_APIdownBtnOk(obj);
  74. if (res.code === 0) {
  75. urlChangeFu(res.data, true, undefined, downId.txt);
  76. // 清空所有选中状态
  77. setCheckedList(
  78. checkedList.map((v) => ({
  79. ...v,
  80. done: false,
  81. }))
  82. );
  83. }
  84. }, [checkedList, downId.id, downId.txt]);
  85. // 点击保存设置 到 本地
  86. const clickStoreFu = useCallback(() => {
  87. const arr = checkedList.filter((v) => v.done).map((v) => v.attrId);
  88. A1setStorage(arr);
  89. MessageFu.success("保存设置到本地成功!");
  90. }, [checkedList]);
  91. return (
  92. <Modal
  93. wrapClassName={styles.A1Down}
  94. destroyOnClose
  95. open={true}
  96. title={"下载:" + downId.txt}
  97. footer={
  98. [] // 设置footer为空,去掉 取消 确定默认按钮
  99. }
  100. >
  101. <div className="A1DownMain">
  102. {checkedList.length ? (
  103. <>
  104. <h3>请选择下载的项目文件类型</h3>
  105. <Checkbox
  106. indeterminate={indeterminate}
  107. onChange={(e) => onCheckAllChange(e.target.checked)}
  108. checked={checkAll}
  109. >
  110. 全选
  111. </Checkbox>
  112. </>
  113. ) : null}
  114. {checkedList.length ? (
  115. <div className="A1DownMainC">
  116. {checkedList.map((v) => (
  117. <div className="A1DownMainCRow" key={v.attrId} title={v.name}>
  118. <Checkbox
  119. onChange={(e) => onChange(e.target.checked, v.attrId)}
  120. checked={v.done}
  121. >
  122. {v.name}
  123. </Checkbox>
  124. </div>
  125. ))}
  126. </div>
  127. ) : (
  128. <>
  129. <br />
  130. <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />
  131. </>
  132. )}
  133. <div className="A1DownBtn">
  134. {checkedList.length ? (
  135. <div className="A1DownBtn1" onClick={clickStoreFu}>
  136. <Button disabled={checkedList.filter((v) => v.done).length < 1}>
  137. 保存设置
  138. </Button>
  139. </div>
  140. ) : (
  141. <span></span>
  142. )}
  143. <div className="A1DownBtn2">
  144. <Button onClick={closeFu}>取消</Button>
  145. &emsp;
  146. <Button
  147. type="primary"
  148. onClick={btnOkFu}
  149. disabled={checkedList.filter((v) => v.done).length < 1}
  150. >
  151. 提交
  152. </Button>
  153. </div>
  154. </div>
  155. </div>
  156. </Modal>
  157. );
  158. }
  159. const MemoA1Down = React.memo(A1Down);
  160. export default MemoA1Down;