Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

423 lignes
18 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid,
  5. Typography,
  6. Stack,
  7. Box,
  8. Tab
  9. } from '@mui/material';
  10. import { TabPanel, TabContext, TabList } from '@mui/lab';
  11. import MainCard from "components/MainCard";
  12. import { useEffect, useState, lazy } from "react";
  13. import axios from "axios";
  14. import { useNavigate, useParams } from "react-router-dom";
  15. import { GLD_USER_PATH, DELETE_USER, POST_ADMIN_USER_REGISTER, GET_LOGIN_LOG_LIST } from "utils/ApiPathConst";
  16. import * as HttpUtils from "utils/HttpUtils";
  17. import {isGrantedAny} from "auth/utils";
  18. import Loadable from 'components/Loadable';
  19. const UserInformationCard = Loadable(lazy(() => import('./UserInformationCard')));
  20. const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
  21. const UserGroupCard = Loadable(lazy(() => import('./UserGroupCard')));
  22. const UserAuthorityCard = Loadable(lazy(() => import('./UserAuthorityCard')));
  23. const LoginGrid = Loadable(lazy(() => import('./LoginGrid')));
  24. import { ThemeProvider } from "@emotion/react";
  25. import { PNSPS_BUTTON_THEME } from "themes/buttonConst";
  26. import {
  27. GeneralConfirmWindow,
  28. getDeletedRecordWithRefList,
  29. notifyDeleteSuccess,
  30. notifySaveSuccess,
  31. } from "utils/CommonFunction";
  32. import ForwardIcon from '@mui/icons-material/Forward';
  33. import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
  34. const BackgroundHead = {
  35. backgroundImage: `url(${titleBackgroundImg})`,
  36. width: '100%',
  37. height: '100%',
  38. backgroundSize: 'contain',
  39. backgroundRepeat: 'no-repeat',
  40. backgroundColor: '#0C489E',
  41. backgroundPosition: 'right'
  42. }
  43. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  44. const UserMaintainPage = () => {
  45. const params = useParams();
  46. const navigate = useNavigate();
  47. const [userData, setUserData] = useState({});
  48. const [onReady, setOnReady] = useState(false);
  49. const [isCollectData, setIsCollectData] = useState(false);
  50. const [editedCustomerData, setEditedCustomerData] = useState({});
  51. const [userGroupData, setUserGroupData] = useState([]);
  52. const [userAuthData, setUserAuthData] = useState([]);
  53. const [userConfirm, setUserConfirm] = useState(false);
  54. const [isNewRecord, setIsNewRecord] = useState(false);
  55. const [refUserData, setRefUserData] = useState({});
  56. const [editMode, setEditMode] = useState(false);
  57. const [createMode, setCreateMode] = useState(false);
  58. const [selectedTab, setSelectedTab] = useState("1");
  59. function updateUserObject(userData) {
  60. setEditedCustomerData(userData);
  61. }
  62. function updateUserGroupList(userGroupData) {
  63. setUserGroupData(userGroupData);
  64. }
  65. function updateUserAuthList(userAuthData) {
  66. setUserAuthData(userAuthData);
  67. }
  68. const submitData = () => {
  69. setUserConfirm(true);
  70. setIsCollectData(!isCollectData);
  71. }
  72. const handleChange = (event, newValue) => {
  73. setSelectedTab(newValue);
  74. }
  75. // ==============================|| DELETE WINDOW RELATED ||============================== //
  76. const [isWindowOpen, setIsWindowOpen] = useState(false);
  77. const handleClose = () => {
  78. setIsWindowOpen(false);
  79. };
  80. const handleDeleteClick = () => {
  81. setIsWindowOpen(true);
  82. };
  83. function deleteData() {
  84. axios.delete(`${DELETE_USER}/${params.id}`,
  85. )
  86. .then((response) => {
  87. if (response.status === 204) {
  88. notifyDeleteSuccess();
  89. setIsWindowOpen(false);
  90. navigate('/userSearchview');
  91. }
  92. })
  93. .catch(error => {
  94. console.log(error);
  95. return false;
  96. });
  97. }
  98. // ==============================|| DELETE WINDOW RELATED ||============================== //
  99. useEffect(() => {
  100. if (params.id > 0) {
  101. loadData()
  102. }
  103. else {
  104. setUserData(
  105. {
  106. "authIds": [],
  107. "data": {},
  108. "groupIds": []
  109. }
  110. );
  111. setRefUserData(
  112. {
  113. "authIds": [],
  114. "data": {},
  115. "groupIds": []
  116. }
  117. );
  118. setIsNewRecord(true);
  119. setCreateMode(true)
  120. }
  121. }, []);
  122. const loadData = () => {
  123. setOnReady(false);
  124. setEditMode(false);
  125. axios.get(`${GLD_USER_PATH}/${params.id}`)
  126. .then((response) => {
  127. if (response.status === 200) {
  128. setRefUserData(response.data);
  129. setUserData(response.data);
  130. getLoginLogList()
  131. }
  132. })
  133. .catch(error => {
  134. console.log(error);
  135. return false;
  136. });
  137. }
  138. useEffect(() => {
  139. // if (Object.keys(userData).length > 0 && userData !== undefined) {
  140. if (Object.keys(userData).length > 0 && userData !== undefined) {
  141. setOnReady(true);
  142. }
  143. else if (isNewRecord) {
  144. setOnReady(true);
  145. }
  146. }, [userData]);
  147. useEffect(() => {
  148. if (userConfirm && onReady) {
  149. const deletedUserAuth = getDeletedRecordWithRefList(refUserData.authIds, userAuthData);
  150. const deletedUserGroup = getDeletedRecordWithRefList(refUserData.groupIds, userGroupData);
  151. // console.log(userGroupData)
  152. // console.log(userAuthData)
  153. if (parseInt(params.id) > -1) {
  154. axios.post(`${GLD_USER_PATH}/${params.id}`,
  155. {
  156. "enName": editedCustomerData.enName,
  157. "locked": editedCustomerData.locked,
  158. // "password": editedCustomerData.password,
  159. // "phone": editedCustomerData.phone,
  160. "post": editedCustomerData.post,
  161. "emailAddress": editedCustomerData.emailAddress,
  162. "addGroupIds": userGroupData,
  163. "removeGroupIds": deletedUserGroup,
  164. "addAuthIds": userAuthData,
  165. "removeAuthIds": deletedUserAuth,
  166. },
  167. ).then((response) => {
  168. if (response.status === 204) {
  169. notifySaveSuccess();
  170. location.reload();
  171. }
  172. })
  173. .catch(error => {
  174. console.log(error);
  175. return false;
  176. });
  177. } else {
  178. if (editedCustomerData.pwErr) return;
  179. axios.post(POST_ADMIN_USER_REGISTER,
  180. {
  181. "username": editedCustomerData.username,
  182. "name": editedCustomerData.username,
  183. "password": editedCustomerData.password,
  184. "enName": editedCustomerData.enName,
  185. "locked": editedCustomerData.locked,
  186. "emailAddress": editedCustomerData.emailAddress,
  187. "post": editedCustomerData.post,
  188. "addGroupIds": userGroupData,
  189. "removeGroupIds": [],
  190. "addAuthIds": userAuthData,
  191. "removeAuthIds": [],
  192. },
  193. ).then((response) => {
  194. if (response.status === 200) {
  195. notifySaveSuccess();
  196. navigate('/userSearchview');
  197. }
  198. })
  199. .catch(function (error){
  200. // console.log(error.response.data)
  201. if(error.response.data.error == "USERNAME_NOT_AVAILABLE"){
  202. alert("Username is already used.")
  203. }
  204. return false;
  205. });
  206. }
  207. }
  208. setUserConfirm(false);
  209. }, [editedCustomerData, userGroupData, userAuthData]);
  210. const getLoginLogList = () => {
  211. HttpUtils.get({
  212. url: `${GET_LOGIN_LOG_LIST}`,
  213. params: {
  214. u1: params.id
  215. },
  216. onSuccess: function (response) {
  217. // console.log(response)
  218. setLoginLogData(response);
  219. }
  220. });
  221. }
  222. const onEditClick = () => {
  223. setEditMode(true);
  224. };
  225. return (
  226. !onReady ?
  227. <Grid container sx={{ minHeight: '87vh', mb: 3 }} direction="column" justifyContent="center" alignItems="center">
  228. <Grid item>
  229. <LoadingComponent />
  230. </Grid>
  231. </Grid>
  232. :
  233. <Grid container sx={{ minHeight: '87vh', backgroundColor: 'backgroundColor.default' }}>
  234. <Grid item xs={12}>
  235. <div style={BackgroundHead}>
  236. <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
  237. <Typography ml={15} color='#FFF' variant="h4" sx={{ "textShadow": "0px 0px 25px #0C489E" }}>{isNewRecord ? "Create GLD User" : "Maintain GLD User"}</Typography>
  238. </Stack>
  239. </div>
  240. </Grid>
  241. <Grid item xs={12}>
  242. <Button title="Back" sx={{ ml: 3.5, mt: 2 }} style={{ border: '2px solid' }} variant="outlined" onClick={() => { navigate("/userSearchview") }}>
  243. <ForwardIcon style={{ height: 30, width: 50, transform: "rotate(180deg)" }} />
  244. </Button>
  245. </Grid>
  246. {/*col 1*/}
  247. {/*bottom button*/}
  248. {
  249. isGrantedAny(["MAINTAIN_USER"]) ?
  250. <Grid item s={12} md={12} lg={12} sx={{ mt: 2, mb: 2 }} alignItems={"start"} justifyContent="center">
  251. <Grid container maxWidth justifyContent="flex-start">
  252. {
  253. createMode?
  254. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  255. <Grid item sx={{ ml: 3, mr: 3 }}>
  256. <Button
  257. size="large"
  258. variant="contained"
  259. type="submit"
  260. sx={{
  261. textTransform: 'capitalize',
  262. alignItems: 'end'
  263. }}
  264. onClick={submitData}
  265. >
  266. Create User
  267. </Button>
  268. </Grid>
  269. </ThemeProvider>
  270. :
  271. editMode ?
  272. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  273. <Grid item sx={{ ml: 3, mr: 3 }}>
  274. <Button
  275. variant="contained"
  276. onClick={loadData}
  277. color="cancel"
  278. >
  279. Reset & Back
  280. </Button>
  281. </Grid>
  282. <Grid item sx={{ ml: 3, mr: 3 }}>
  283. <Button
  284. size="large"
  285. variant="contained"
  286. type="submit"
  287. sx={{
  288. textTransform: 'capitalize',
  289. alignItems: 'end'
  290. }}
  291. onClick={submitData}
  292. >
  293. Save
  294. </Button>
  295. </Grid>
  296. </ThemeProvider>
  297. :
  298. <ThemeProvider theme={PNSPS_BUTTON_THEME}>
  299. <Grid item sx={{ ml: 3, mr: 3 }}>
  300. <Button
  301. variant="contained"
  302. onClick={onEditClick}
  303. >
  304. Edit
  305. </Button>
  306. </Grid>
  307. <Grid item sx={{ ml: 3, mr: 3 }}>
  308. <Button
  309. size="large"
  310. variant="contained"
  311. sx={{
  312. textTransform: 'capitalize',
  313. alignItems: 'end'
  314. }}
  315. disabled={isNewRecord}
  316. onClick={handleDeleteClick}
  317. color='error'
  318. >
  319. Delete User
  320. </Button>
  321. <GeneralConfirmWindow
  322. isWindowOpen={isWindowOpen}
  323. title={"Attention"}
  324. content={`Confirm to delete User "${userData.data.username}" ?`}
  325. onNormalClose={handleClose}
  326. onConfirmClose={deleteData}
  327. />
  328. </Grid>
  329. </ThemeProvider>
  330. }
  331. </Grid>
  332. </Grid>
  333. : <></>
  334. }
  335. <Grid item xs={12} md={5} lg={5}>
  336. <Grid container>
  337. <Grid item xs={12} md={12} lg={12}>
  338. <Box xs={12} ml={4} mt={2} sx={{ borderRadius: '10px', backgroundColor: '#fff' }}>
  339. <UserInformationCard
  340. updateUserObject={updateUserObject}
  341. userData={userData}
  342. isCollectData={isCollectData}
  343. isNewRecord={isNewRecord}
  344. editMode={editMode}
  345. createMode={createMode}
  346. />
  347. </Box>
  348. </Grid>
  349. <Grid item xs={12} md={12} lg={12} sx={{ mt: 0.5 }}>
  350. <Box xs={12} ml={4} mt={2} sx={{ borderRadius: '10px', backgroundColor: '#fff' }}>
  351. <UserGroupCard
  352. updateUserGroupList={updateUserGroupList}
  353. userData={userData}
  354. isCollectData={isCollectData}
  355. isNewRecord={isNewRecord}
  356. editMode={editMode}
  357. createMode={createMode}
  358. />
  359. </Box>
  360. </Grid>
  361. </Grid>
  362. </Grid>
  363. {/*col 2*/}
  364. <Grid item xs={12} md={7} lg={7}>
  365. <Box xs={12} ml={2} mt={2} mr={3} sx={{ borderRadius: '10px', backgroundColor: '#fff' }}>
  366. <UserAuthorityCard
  367. updateUserAuthList={updateUserAuthList}
  368. userData={userData}
  369. isCollectData={isCollectData}
  370. isNewRecord={isNewRecord}
  371. editMode={editMode}
  372. createMode={createMode}
  373. />
  374. </Box>
  375. </Grid>
  376. <Grid item xs={12} md={12} lg={12}>
  377. <Box xs={12} ml={2} mt={2} mr={3} sx={{ borderRadius: '10px', backgroundColor: '#fff' }}>
  378. <MainCard elevation={0} border={false} content={false} sx={{maxWidth: '100%', mr:2, width: "-webkit-fill-available"}}>
  379. <TabContext value={selectedTab}>
  380. <Box sx={{ borderBottom: 1, borderColor: 'divider' }}>
  381. <TabList variant="scrollable" onChange={handleChange} aria-label="lab API tabs example">
  382. <Tab label="Login Log" value="1" />
  383. </TabList>
  384. </Box>
  385. <TabPanel value="1">
  386. <LoginGrid
  387. u1 = {params.id}
  388. />
  389. </TabPanel>
  390. </TabContext>
  391. </MainCard>
  392. </Box>
  393. </Grid>
  394. </Grid>
  395. );
  396. };
  397. export default UserMaintainPage;