You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

269 lines
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. <LoadingComponent />
  154. :
  155. <Grid container sx={{ backgroundColor: "backgroundColor.default" }}>
  156. <Grid item xs={12}>
  157. <div style={BackgroundHead}>
  158. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  159. <Typography ml={15} color='#FFF' variant="h4">{isNewRecord ? "Create User Group" : "Maintain User Group"}</Typography>
  160. </Stack>
  161. </div>
  162. </Grid>
  163. <Grid item xs={12}>
  164. <Button sx={{ ml: 3.5, mt: 2 }} style={{ border: '2px solid' }} variant="outlined" onClick={() => { navigate("/usergroupSearchview") }}>
  165. <ForwardIcon style={{ height: 30, width: 50, transform: "rotate(180deg)" }} />
  166. </Button>
  167. </Grid>
  168. {/*col 1*/}
  169. <Grid item xs={12} md={5} lg={5}>
  170. <Grid container>
  171. <Grid item xs={12} md={12} lg={12}>
  172. <Box xs={12} ml={0} mt={-1} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  173. <UserGroupInfoCard
  174. updateGroupObject={updateGroupObject}
  175. userGroupData={userGroupData}
  176. isCollectData={isCollectData}
  177. isNewRecord={isNewRecord}
  178. />
  179. </Box>
  180. </Grid>
  181. <Grid item xs={12} md={12} lg={12} sx={{ mt: 3 }}>
  182. <Box xs={12} ml={0} mt={-5} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  183. <UserAddCard
  184. updateGroupMember={updateGroupMember}
  185. userGroupData={userGroupData}
  186. isCollectData={isCollectData}
  187. isNewRecord={isNewRecord}
  188. />
  189. </Box>
  190. </Grid>
  191. </Grid>
  192. </Grid>
  193. {/*col 2*/}
  194. <Grid item xs={12} md={7} lg={7}>
  195. <Box xs={12} ml={-2} mt={-1} mr={0} sx={{ p: 1, borderRadius: '10px' }}>
  196. <GroupAuthCard
  197. updateUserAuthList={updateUserAuthList}
  198. userGroupData={userGroupData}
  199. isCollectData={isCollectData}
  200. isNewRecord={isNewRecord}
  201. />
  202. </Box>
  203. </Grid>
  204. {/*bottom button*/}
  205. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"end"} justifyContent="center">
  206. <Grid container maxWidth justifyContent="flex-end">
  207. <Grid item sx={{ ml: 3, mr: 3, mb: 3 }}>
  208. <Button
  209. size="large"
  210. variant="contained"
  211. sx={{
  212. textTransform: 'capitalize',
  213. alignItems: 'end'
  214. }}
  215. disabled={isNewRecord}
  216. onClick={handleDeleteClick}
  217. >
  218. <Typography variant="h5">Delete User Group</Typography>
  219. </Button>
  220. <GeneralConfirmWindow
  221. isWindowOpen={isWindowOpen}
  222. title={"Attention"}
  223. content={`Confirm to delete User Group "${userGroupData.data.name}" ?`}
  224. onNormalClose={handleClose}
  225. onConfirmClose={deleteData}
  226. />
  227. </Grid>
  228. <Grid item sx={{ ml: 3, mr: 3 }}>
  229. <Button
  230. size="large"
  231. variant="contained"
  232. type="submit"
  233. sx={{
  234. textTransform: 'capitalize',
  235. alignItems: 'end'
  236. }}
  237. onClick={submitData}
  238. >
  239. <Typography variant="h5">Save</Typography>
  240. </Button>
  241. </Grid>
  242. </Grid>
  243. </Grid>
  244. </Grid>
  245. );
  246. };
  247. export default UserMaintainPage;