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.
 
 

305 line
12 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid,
  5. Typography,
  6. Stack,
  7. Box,
  8. } from '@mui/material';
  9. import { useEffect, useState } from "react";
  10. import * as React from "react";
  11. import axios from "axios";
  12. import { useNavigate, useParams } from "react-router-dom";
  13. import { GLD_USER_PATH, DELETE_USER, POST_ADMIN_USER_REGISTER } from "../../../utils/ApiPathConst";
  14. import Loadable from 'components/Loadable';
  15. import { lazy } from 'react';
  16. const UserInformationCard = Loadable(lazy(() => import('./UserInformationCard')));
  17. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  18. const UserGroupCard = Loadable(lazy(() => import('./UserGroupCard')));
  19. const UserAuthorityCard = Loadable(lazy(() => import('./UserAuthorityCard')));
  20. import {
  21. GeneralConfirmWindow,
  22. getDeletedRecordWithRefList,
  23. notifyDeleteSuccess,
  24. notifySaveSuccess,
  25. } from "../../../utils/CommonFunction";
  26. import KeyboardBackspaceOutlinedIcon from '@mui/icons-material/KeyboardBackspaceOutlined';
  27. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  28. const BackgroundHead = {
  29. backgroundImage: `url(${titleBackgroundImg})`,
  30. width: '100%',
  31. height: '100%',
  32. backgroundSize: 'contain',
  33. backgroundRepeat: 'no-repeat',
  34. backgroundColor: '#0C489E',
  35. backgroundPosition: 'right'
  36. }
  37. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  38. const UserMaintainPage = () => {
  39. const params = useParams();
  40. const navigate = useNavigate();
  41. const [userData, setUserData] = React.useState({});
  42. const [onReady, setOnReady] = useState(false);
  43. const [isCollectData, setIsCollectData] = useState(false);
  44. const [editedCustomerData, setEditedCustomerData] = useState({});
  45. const [userGroupData, setUserGroupData] = useState([]);
  46. const [userAuthData, setUserAuthData] = useState([]);
  47. const [userConfirm, setUserConfirm] = useState(false);
  48. const [isNewRecord, setIsNewRecord] = useState(false);
  49. const [refUserData, setRefUserData] = React.useState({});
  50. function updateUserObject(userData) {
  51. setEditedCustomerData(userData);
  52. }
  53. function updateUserGroupList(userGroupData) {
  54. setUserGroupData(userGroupData);
  55. }
  56. function updateUserAuthList(userAuthData) {
  57. setUserAuthData(userAuthData);
  58. }
  59. const submitData = () => {
  60. setUserConfirm(true);
  61. setIsCollectData(!isCollectData);
  62. }
  63. // ==============================|| DELETE WINDOW RELATED ||============================== //
  64. const [isWindowOpen, setIsWindowOpen] = useState(false);
  65. const handleClose = () => {
  66. setIsWindowOpen(false);
  67. };
  68. const handleDeleteClick = () => {
  69. setIsWindowOpen(true);
  70. };
  71. function deleteData() {
  72. axios.delete(`${DELETE_USER}/${params.id}`,
  73. )
  74. .then((response) => {
  75. if (response.status === 204) {
  76. notifyDeleteSuccess();
  77. setIsWindowOpen(false);
  78. navigate('/userSearchview');
  79. }
  80. })
  81. .catch(error => {
  82. console.log(error);
  83. return false;
  84. });
  85. }
  86. // ==============================|| DELETE WINDOW RELATED ||============================== //
  87. useEffect(() => {
  88. if (params.id > 0) {
  89. axios.get(`${GLD_USER_PATH}/${params.id}`)
  90. .then((response) => {
  91. if (response.status === 200) {
  92. setRefUserData(response.data);
  93. setUserData(response.data);
  94. }
  95. })
  96. .catch(error => {
  97. console.log(error);
  98. return false;
  99. });
  100. }
  101. else {
  102. setUserData(
  103. {
  104. "authIds": [],
  105. "data": {},
  106. "groupIds": []
  107. }
  108. );
  109. setRefUserData(
  110. {
  111. "authIds": [],
  112. "data": {},
  113. "groupIds": []
  114. }
  115. );
  116. setIsNewRecord(true);
  117. }
  118. }, []);
  119. useEffect(() => {
  120. if (Object.keys(userData).length > 0 && userData !== undefined) {
  121. setOnReady(true);
  122. }
  123. else if (isNewRecord) {
  124. setOnReady(true);
  125. }
  126. }, [userData]);
  127. useEffect(() => {
  128. if (userConfirm && onReady) {
  129. const deletedUserAuth = getDeletedRecordWithRefList(refUserData.authIds, userAuthData);
  130. const deletedUserGroup = getDeletedRecordWithRefList(refUserData.groupIds, userGroupData);
  131. // console.log(userGroupData)
  132. // console.log(userAuthData)
  133. if (parseInt(params.id) >= -1) {
  134. axios.post(`${GLD_USER_PATH}/${params.id}`,
  135. {
  136. "enName": editedCustomerData.enName,
  137. "locked": editedCustomerData.locked,
  138. // "password": editedCustomerData.password,
  139. // "phone": editedCustomerData.phone,
  140. "post": editedCustomerData.post,
  141. "emailAddress": editedCustomerData.emailAddress,
  142. "addGroupIds": userGroupData,
  143. "removeGroupIds": deletedUserGroup,
  144. "addAuthIds": userAuthData,
  145. "removeAuthIds": deletedUserAuth,
  146. },
  147. ).then((response) => {
  148. if (response.status === 204) {
  149. notifySaveSuccess();
  150. navigate('/userSearchview');
  151. }
  152. })
  153. .catch(error => {
  154. console.log(error);
  155. return false;
  156. });
  157. } else {
  158. axios.post(POST_ADMIN_USER_REGISTER,
  159. {
  160. "username": editedCustomerData.username,
  161. "name": editedCustomerData.username,
  162. "password": editedCustomerData.password,
  163. "enName": editedCustomerData.enName,
  164. "locked": editedCustomerData.locked,
  165. "emailAddress": editedCustomerData.emailAddress,
  166. "post": editedCustomerData.post,
  167. "addGroupIds": userGroupData,
  168. "removeGroupIds": [],
  169. "addAuthIds": userAuthData,
  170. "removeAuthIds": [],
  171. },
  172. ).then((response) => {
  173. if (response.status === 200) {
  174. notifySaveSuccess();
  175. navigate('/userSearchview');
  176. }
  177. })
  178. .catch(error => {
  179. console.log(error);
  180. return false;
  181. });
  182. }
  183. }
  184. setUserConfirm(false);
  185. }, [editedCustomerData, userGroupData, userAuthData]);
  186. return (
  187. !onReady ?
  188. <LoadingComponent />
  189. :
  190. <Grid container sx={{ minHeight: '90vh' }}>
  191. <Grid item xs={12}>
  192. <div style={BackgroundHead}>
  193. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  194. <Typography ml={15} color='#FFF' variant="h4">{isNewRecord ? "Create GLD User" : "Maintain GLD User"}</Typography>
  195. </Stack>
  196. </div>
  197. </Grid>
  198. <Grid item xs={12}>
  199. <Button sx={{ ml: 3.5, mt: 2 }} style={{ border: '2px solid' }} variant="outlined" onClick={() => { navigate("/userSearchview") }}>
  200. <KeyboardBackspaceOutlinedIcon />
  201. <Typography variant="h4">Back</Typography>
  202. </Button>
  203. </Grid>
  204. {/*col 1*/}
  205. <Grid item xs={12} md={5} lg={5}>
  206. <Grid container>
  207. <Grid item xs={12} md={12} lg={12}>
  208. <Box xs={12} ml={4} mt={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
  209. <UserInformationCard
  210. updateUserObject={updateUserObject}
  211. userData={userData}
  212. isCollectData={isCollectData}
  213. isNewRecord={isNewRecord}
  214. />
  215. </Box>
  216. </Grid>
  217. <Grid item xs={12} md={12} lg={12} sx={{ mt: 3 }}>
  218. <Box xs={12} ml={4} mt={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
  219. <UserGroupCard
  220. updateUserGroupList={updateUserGroupList}
  221. userData={userData}
  222. isCollectData={isCollectData}
  223. isNewRecord={isNewRecord}
  224. />
  225. </Box>
  226. </Grid>
  227. </Grid>
  228. </Grid>
  229. {/*col 2*/}
  230. <Grid item xs={12} md={7} lg={7}>
  231. <Box xs={12} ml={4} mt={3} mr={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
  232. <UserAuthorityCard
  233. updateUserAuthList={updateUserAuthList}
  234. userData={userData}
  235. isCollectData={isCollectData}
  236. isNewRecord={isNewRecord}
  237. />
  238. </Box>
  239. </Grid>
  240. {/*bottom button*/}
  241. <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"end"} justifyContent="center">
  242. <Grid container maxWidth justifyContent="flex-end">
  243. <Grid item sx={{ ml: 3, mr: 3 }}>
  244. <Button
  245. size="large"
  246. variant="contained"
  247. sx={{
  248. textTransform: 'capitalize',
  249. alignItems: 'end'
  250. }}
  251. disabled={isNewRecord}
  252. onClick={handleDeleteClick}
  253. >
  254. Delete User
  255. </Button>
  256. <GeneralConfirmWindow
  257. isWindowOpen={isWindowOpen}
  258. title={"Attention"}
  259. content={`Confirm to delete User "${userData.data.username}" ?`}
  260. onNormalClose={handleClose}
  261. onConfirmClose={deleteData}
  262. />
  263. </Grid>
  264. <Grid item sx={{ ml: 3, mr: 3 }}>
  265. <Button
  266. size="large"
  267. variant="contained"
  268. type="submit"
  269. sx={{
  270. textTransform: 'capitalize',
  271. alignItems: 'end'
  272. }}
  273. onClick={submitData}
  274. >
  275. Save
  276. </Button>
  277. </Grid>
  278. </Grid>
  279. </Grid>
  280. </Grid>
  281. );
  282. };
  283. export default UserMaintainPage;