Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 

273 linhas
10 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid, Typography, Stack, Box
  5. } from '@mui/material';
  6. import { useEffect, useState } from "react";
  7. import * as React from "react";
  8. import axios from "axios";
  9. import { apiPath } from "../../auth/utils";
  10. import { useParams } from "react-router-dom";
  11. import {
  12. GeneralConfirmWindow,
  13. getDeletedRecordWithRefList,
  14. getIdList,
  15. notifyDeleteSuccess,
  16. // notifyDeleteSuccess,
  17. notifySaveSuccess
  18. } from "../../utils/CommonFunction";
  19. import { POST_AND_UPDATE_USER_GROUP, GET_GROUP_LIST_PATH } from "../../utils/ApiPathConst";
  20. import Loadable from 'components/Loadable';
  21. import { lazy } from 'react';
  22. const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));
  23. const GroupAuthCard = Loadable(lazy(() => import('./GroupAuthCard')));
  24. const UserGroupInfoCard = Loadable(lazy(() => import('./UserGroupInfoCard')));
  25. const UserAddCard = Loadable(lazy(() => import('./UserAddCard')));
  26. import { useNavigate } from "react-router";
  27. import ForwardIcon from '@mui/icons-material/Forward';
  28. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  29. const BackgroundHead = {
  30. backgroundImage: `url(${titleBackgroundImg})`,
  31. width: '100%',
  32. height: '100%',
  33. backgroundSize: 'contain',
  34. backgroundRepeat: 'no-repeat',
  35. backgroundColor: '#0C489E',
  36. backgroundPosition: 'right'
  37. }
  38. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  39. const UserMaintainPage = () => {
  40. const params = useParams();
  41. const navigate = useNavigate();
  42. const [onReady, setOnReady] = useState(false);
  43. const [isCollectData, setIsCollectData] = useState(false);
  44. const [editedGroupData, setEditedGroupData] = useState({});
  45. const [userGroupData, setUserGroupData] = useState([]);
  46. const [userAuthData, setUserAuthData] = useState([]);
  47. const [userConfirm, setUserConfirm] = useState(false);
  48. const [groupMember, setGroupMember] = useState([]);
  49. const [isNewRecord, setIsNewRecord] = useState(false);
  50. const [deletedUserList, setDeletedUserList] = React.useState([]);
  51. const [deletedAuthList, setDeletedAuthList] = React.useState([]);
  52. const [isWindowOpen, setIsWindowOpen] = React.useState(false);
  53. const handleClose = () => {
  54. setIsWindowOpen(false);
  55. };
  56. const handleDeleteClick = () => {
  57. setIsWindowOpen(true);
  58. };
  59. function deleteData() {
  60. axios.delete(`${apiPath}${GET_GROUP_LIST_PATH}/${params.id}`,
  61. )
  62. .then((response) => {
  63. if (response.status === 204) {
  64. notifyDeleteSuccess()
  65. setIsWindowOpen(false);
  66. navigate('/usergroupSearchview');
  67. }
  68. })
  69. .catch(error => {
  70. console.log(error);
  71. return false;
  72. });
  73. }
  74. function updateGroupObject(groupData) {
  75. setEditedGroupData(groupData);
  76. }
  77. function updateGroupMember(groupMember) {
  78. setGroupMember(groupMember.currentList);
  79. setDeletedUserList(groupMember.deletedList);
  80. }
  81. function updateUserAuthList(userAuthData) {
  82. setUserAuthData(userAuthData.currentList);
  83. setDeletedAuthList(userAuthData.deletedList);
  84. }
  85. const submitData = () => {
  86. setUserConfirm(true);
  87. setIsCollectData(!isCollectData);
  88. }
  89. useEffect(() => {
  90. if (params.id > 0) {
  91. axios.get(`${apiPath}${GET_GROUP_LIST_PATH}/${params.id}`)
  92. .then((response) => {
  93. if (response.status === 200) {
  94. setUserGroupData(response.data);
  95. }
  96. })
  97. .catch(error => {
  98. console.log(error);
  99. return false;
  100. });
  101. }
  102. else {
  103. //new record case
  104. setUserGroupData(
  105. {
  106. "authIds": [],
  107. "data": {},
  108. "userIds": []
  109. }
  110. );
  111. setIsNewRecord(true);
  112. }
  113. }, []);
  114. useEffect(() => {
  115. if (Object.keys(userGroupData).length > 0 && userGroupData !== undefined) {
  116. setOnReady(true);
  117. }
  118. else if (isNewRecord) {
  119. setOnReady(true);
  120. }
  121. }, [userGroupData]);
  122. useEffect(() => {
  123. if (userConfirm && onReady) {
  124. //avoid delete and add user at the same time
  125. let finalDeletedUserList = getDeletedRecordWithRefList(deletedUserList, getIdList(groupMember));
  126. console.log(finalDeletedUserList)
  127. axios.post(POST_AND_UPDATE_USER_GROUP,
  128. {
  129. "id": parseInt(params.id) !== -1 ? parseInt(params.id) : null,
  130. "name": editedGroupData.userGroupName,
  131. "description": editedGroupData.description,
  132. "addUserIds": getIdList(groupMember),
  133. "removeUserIds": finalDeletedUserList,
  134. "addAuthIds": userAuthData,
  135. "removeAuthIds": deletedAuthList,
  136. },
  137. )
  138. .then((response) => {
  139. if (response.status === 200) {
  140. navigate('/usergroupSearchview');
  141. notifySaveSuccess()
  142. }
  143. })
  144. .catch(error => {
  145. console.log(error);
  146. return false;
  147. });
  148. }
  149. setUserConfirm(false);
  150. }, [editedGroupData, userGroupData, userAuthData]);
  151. return (
  152. !onReady ?
  153. <Grid container sx={{ minHeight: '87vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
  154. <Grid item>
  155. <LoadingComponent />
  156. </Grid>
  157. </Grid>
  158. :
  159. <Grid container sx={{ backgroundColor: "backgroundColor.default" }}>
  160. <Grid item xs={12}>
  161. <div style={BackgroundHead}>
  162. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  163. <Typography ml={15} color='#FFF' variant="h4" sx={{ "textShadow": "0px 0px 25px #0C489E" }}>{isNewRecord ? "Create User Group" : "Maintain User Group"}</Typography>
  164. </Stack>
  165. </div>
  166. </Grid>
  167. <Grid item xs={12}>
  168. <Button title="Back" sx={{ ml: 3.5, mt: 2 }} style={{ border: '2px solid' }} variant="outlined" onClick={() => { navigate("/usergroupSearchview") }}>
  169. <ForwardIcon style={{ height: 30, width: 50, transform: "rotate(180deg)" }} />
  170. </Button>
  171. </Grid>
  172. {/*col 1*/}
  173. <Grid item xs={12} md={5} lg={5}>
  174. <Grid container>
  175. <Grid item xs={12} md={12} lg={12}>
  176. <Box xs={12} ml={0} mt={-1} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  177. <UserGroupInfoCard
  178. updateGroupObject={updateGroupObject}
  179. userGroupData={userGroupData}
  180. isCollectData={isCollectData}
  181. isNewRecord={isNewRecord}
  182. />
  183. </Box>
  184. </Grid>
  185. <Grid item xs={12} md={12} lg={12} sx={{ mt: 3 }}>
  186. <Box xs={12} ml={0} mt={-5} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  187. <UserAddCard
  188. updateGroupMember={updateGroupMember}
  189. userGroupData={userGroupData}
  190. isCollectData={isCollectData}
  191. isNewRecord={isNewRecord}
  192. />
  193. </Box>
  194. </Grid>
  195. </Grid>
  196. </Grid>
  197. {/*col 2*/}
  198. <Grid item xs={12} md={7} lg={7}>
  199. <Box xs={12} ml={-2} mt={-1} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  200. <GroupAuthCard
  201. updateUserAuthList={updateUserAuthList}
  202. userGroupData={userGroupData}
  203. isCollectData={isCollectData}
  204. isNewRecord={isNewRecord}
  205. />
  206. </Box>
  207. </Grid>
  208. {/*bottom button*/}
  209. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"end"} justifyContent="center">
  210. <Grid container maxWidth justifyContent="flex-end">
  211. <Grid item sx={{ ml: 3, mr: 3, mb: 3 }}>
  212. <Button
  213. size="large"
  214. variant="contained"
  215. sx={{
  216. textTransform: 'capitalize',
  217. alignItems: 'end'
  218. }}
  219. disabled={isNewRecord}
  220. onClick={handleDeleteClick}
  221. >
  222. <Typography variant="h5">Delete User Group</Typography>
  223. </Button>
  224. <GeneralConfirmWindow
  225. isWindowOpen={isWindowOpen}
  226. title={"Attention"}
  227. content={`Confirm to delete User Group "${userGroupData.data.name}" ?`}
  228. onNormalClose={handleClose}
  229. onConfirmClose={deleteData}
  230. />
  231. </Grid>
  232. <Grid item sx={{ ml: 3, mr: 3 }}>
  233. <Button
  234. size="large"
  235. variant="contained"
  236. type="submit"
  237. sx={{
  238. textTransform: 'capitalize',
  239. alignItems: 'end'
  240. }}
  241. onClick={submitData}
  242. >
  243. <Typography variant="h5">Save</Typography>
  244. </Button>
  245. </Grid>
  246. </Grid>
  247. </Grid>
  248. </Grid>
  249. );
  250. };
  251. export default UserMaintainPage;