Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

248 rader
8.9 KiB

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