index.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. import React, {
  2. useCallback,
  3. useEffect,
  4. useMemo,
  5. useRef,
  6. useState,
  7. } from "react";
  8. import styles from "./index.module.scss";
  9. import { Button, Input, Popconfirm, Table } from "antd";
  10. import { A1_APIUdel, A1_APIUgeiList } from "@/store/action/A1Project";
  11. import { A1UtableType } from "@/types";
  12. import A1UserAdd from "./A1UserAdd";
  13. import { useSelector } from "react-redux";
  14. import { RootState } from "@/store";
  15. import { MessageFu } from "@/utils/message";
  16. import AuthCom from "@/components/AuthCom";
  17. type Props = {
  18. projectId: number;
  19. };
  20. function A1User({ projectId }: Props) {
  21. const [searchKey, setSearchKey] = useState("");
  22. const getListFu = useCallback(
  23. async (val: string) => {
  24. const res = await A1_APIUgeiList(projectId, val);
  25. if (res.code === 0) setData(res.data);
  26. },
  27. [projectId]
  28. );
  29. useEffect(() => {
  30. getListFu(searchKey);
  31. }, [getListFu, searchKey]);
  32. const [data, setData] = useState<A1UtableType[]>([]);
  33. // 搜索项的输入
  34. const searchKeyTime = useRef(-1);
  35. const searchKeyChange = useCallback(
  36. (e: React.ChangeEvent<HTMLInputElement>) => {
  37. clearTimeout(searchKeyTime.current);
  38. searchKeyTime.current = window.setTimeout(() => {
  39. setSearchKey(e.target.value);
  40. }, 500);
  41. },
  42. []
  43. );
  44. // 新增 和 编辑 成功
  45. const addSuFu = useCallback(() => {
  46. if (searchKey.trim() === "") getListFu("");
  47. else setSearchKey("");
  48. }, [getListFu, searchKey]);
  49. // 点击删除
  50. const delById = useCallback(
  51. async (id: number) => {
  52. const res = await A1_APIUdel(id);
  53. if (res.code === 0) {
  54. MessageFu.success("删除成功!");
  55. getListFu(searchKey);
  56. }
  57. },
  58. [getListFu, searchKey]
  59. );
  60. // 权限
  61. const authArr = useSelector((state: RootState) => state.A4Role.A4RoleAll);
  62. const columns = useMemo(() => {
  63. const arr: any = [
  64. {
  65. title: "项目职能",
  66. dataIndex: "jobName",
  67. },
  68. {
  69. title: "姓名",
  70. dataIndex: "name",
  71. },
  72. ];
  73. if (authArr.includes("1112") || authArr.includes("1113"))
  74. arr.push({
  75. title: "操作",
  76. render: (item: A1UtableType) => (
  77. <>
  78. <AuthCom aId="1112">
  79. <Button size="small" type="text" onClick={() => setAddInfo(item)}>
  80. 编辑
  81. </Button>
  82. </AuthCom>
  83. <AuthCom aId="1113">
  84. <Popconfirm
  85. title="删除后无法恢复,是否删除?"
  86. okText="删除"
  87. cancelText="取消"
  88. onConfirm={() => delById(item.id)}
  89. okButtonProps={{ loading: false }}
  90. >
  91. <Button size="small" type="text" danger>
  92. 删除
  93. </Button>
  94. </Popconfirm>
  95. </AuthCom>
  96. </>
  97. ),
  98. });
  99. return arr;
  100. }, [authArr, delById]);
  101. // 编辑 新增 的数据
  102. const [addInfo, setAddInfo] = useState({} as A1UtableType);
  103. return (
  104. <div className={styles.A1User}>
  105. {/* 顶部 */}
  106. <div className="A1Utop">
  107. <div className="A1Utop1">
  108. <span>搜索项:</span>
  109. <Input
  110. maxLength={30}
  111. style={{ width: 300 }}
  112. placeholder="请输入成员姓名"
  113. allowClear
  114. onChange={(e) => searchKeyChange(e)}
  115. />
  116. </div>
  117. <div className="A1Utop2">
  118. <AuthCom aId="1115">
  119. <Button
  120. type="primary"
  121. onClick={() => setAddInfo({ id: -1, projectId } as A1UtableType)}
  122. >
  123. 新增
  124. </Button>
  125. </AuthCom>
  126. </div>
  127. </div>
  128. {/* 表格主体 */}
  129. <div className="A1UTableBox">
  130. <Table
  131. scroll={{ y: 610 }}
  132. dataSource={data}
  133. columns={columns}
  134. rowKey="id"
  135. pagination={false}
  136. />
  137. </div>
  138. {/* 编辑和新增出来的弹窗 */}
  139. {addInfo.id ? (
  140. <A1UserAdd
  141. info={addInfo}
  142. closeFu={() => setAddInfo({ id: 0 } as A1UtableType)}
  143. editFu={() => getListFu(searchKey)}
  144. addFu={() => addSuFu()}
  145. />
  146. ) : null}
  147. </div>
  148. );
  149. }
  150. const MemoA1User = React.memo(A1User);
  151. export default MemoA1User;