|
- // material-ui
- import {
- Button,
- Grid,
- Typography,
- Stack,
- Box,
- } from '@mui/material';
- import { useEffect, useState } from "react";
- import * as React from "react";
- import axios from "axios";
- import { useNavigate, useParams } from "react-router-dom";
- import { GLD_USER_PATH, DELETE_USER, POST_ADMIN_USER_REGISTER } from "../../../utils/ApiPathConst";
-
- import Loadable from 'components/Loadable';
- import { lazy } from 'react';
- const UserInformationCard = Loadable(lazy(() => import('./UserInformationCard')));
- const LoadingComponent = Loadable(lazy(() => import('../../extra-pages/LoadingComponent')));
- const UserGroupCard = Loadable(lazy(() => import('./UserGroupCard')));
- const UserAuthorityCard = Loadable(lazy(() => import('./UserAuthorityCard')));
- import {
- GeneralConfirmWindow,
- getDeletedRecordWithRefList,
- notifyDeleteSuccess,
- notifySaveSuccess,
- } from "../../../utils/CommonFunction";
- import KeyboardBackspaceOutlinedIcon from '@mui/icons-material/KeyboardBackspaceOutlined';
- import titleBackgroundImg from 'assets/images/dashboard/gazette-bar.png'
-
- const BackgroundHead = {
- backgroundImage: `url(${titleBackgroundImg})`,
- width: '100%',
- height: '100%',
- backgroundSize: 'contain',
- backgroundRepeat: 'no-repeat',
- backgroundColor: '#0C489E',
- backgroundPosition: 'right'
- }
- // ==============================|| DASHBOARD - DEFAULT ||============================== //
-
-
- const UserMaintainPage = () => {
- const params = useParams();
- const navigate = useNavigate();
- const [userData, setUserData] = React.useState({});
- const [onReady, setOnReady] = useState(false);
- const [isCollectData, setIsCollectData] = useState(false);
- const [editedCustomerData, setEditedCustomerData] = useState({});
- const [userGroupData, setUserGroupData] = useState([]);
- const [userAuthData, setUserAuthData] = useState([]);
- const [userConfirm, setUserConfirm] = useState(false);
- const [isNewRecord, setIsNewRecord] = useState(false);
- const [refUserData, setRefUserData] = React.useState({});
-
-
- function updateUserObject(userData) {
- setEditedCustomerData(userData);
- }
-
- function updateUserGroupList(userGroupData) {
- setUserGroupData(userGroupData);
- }
-
- function updateUserAuthList(userAuthData) {
- setUserAuthData(userAuthData);
- }
-
- const submitData = () => {
- setUserConfirm(true);
- setIsCollectData(!isCollectData);
- }
-
- // ==============================|| DELETE WINDOW RELATED ||============================== //
- const [isWindowOpen, setIsWindowOpen] = useState(false);
- const handleClose = () => {
- setIsWindowOpen(false);
- };
-
- const handleDeleteClick = () => {
- setIsWindowOpen(true);
- };
-
- function deleteData() {
- axios.delete(`${DELETE_USER}/${params.id}`,
- )
- .then((response) => {
- if (response.status === 204) {
- notifyDeleteSuccess();
- setIsWindowOpen(false);
- navigate('/userSearchview');
- }
- })
- .catch(error => {
- console.log(error);
- return false;
- });
- }
- // ==============================|| DELETE WINDOW RELATED ||============================== //
-
- useEffect(() => {
- if (params.id > 0) {
- axios.get(`${GLD_USER_PATH}/${params.id}`)
- .then((response) => {
- if (response.status === 200) {
- setRefUserData(response.data);
- setUserData(response.data);
- }
- })
- .catch(error => {
- console.log(error);
- return false;
- });
- }
- else {
- setUserData(
- {
- "authIds": [],
- "data": {},
- "groupIds": []
- }
- );
- setRefUserData(
- {
- "authIds": [],
- "data": {},
- "groupIds": []
- }
- );
- setIsNewRecord(true);
- }
-
- }, []);
-
- useEffect(() => {
- if (Object.keys(userData).length > 0 && userData !== undefined) {
- setOnReady(true);
- }
- else if (isNewRecord) {
- setOnReady(true);
- }
- }, [userData]);
-
- useEffect(() => {
- if (userConfirm && onReady) {
- const deletedUserAuth = getDeletedRecordWithRefList(refUserData.authIds, userAuthData);
- const deletedUserGroup = getDeletedRecordWithRefList(refUserData.groupIds, userGroupData);
- // console.log(userGroupData)
- // console.log(userAuthData)
- if (parseInt(params.id) >= -1) {
- axios.post(`${GLD_USER_PATH}/${params.id}`,
- {
- "enName": editedCustomerData.enName,
- "locked": editedCustomerData.locked,
- // "password": editedCustomerData.password,
- // "phone": editedCustomerData.phone,
- "post": editedCustomerData.post,
- "emailAddress": editedCustomerData.emailAddress,
- "addGroupIds": userGroupData,
- "removeGroupIds": deletedUserGroup,
- "addAuthIds": userAuthData,
- "removeAuthIds": deletedUserAuth,
- },
- ).then((response) => {
- if (response.status === 204) {
- notifySaveSuccess();
- navigate('/userSearchview');
- }
- })
- .catch(error => {
- console.log(error);
- return false;
- });
- } else {
- axios.post(POST_ADMIN_USER_REGISTER,
- {
- "username": editedCustomerData.username,
- "name": editedCustomerData.username,
- "password": editedCustomerData.password,
- "enName": editedCustomerData.enName,
- "locked": editedCustomerData.locked,
- "emailAddress": editedCustomerData.emailAddress,
- "post": editedCustomerData.post,
- "addGroupIds": userGroupData,
- "removeGroupIds": [],
- "addAuthIds": userAuthData,
- "removeAuthIds": [],
- },
- ).then((response) => {
- if (response.status === 200) {
- notifySaveSuccess();
- navigate('/userSearchview');
- }
- })
- .catch(error => {
- console.log(error);
- return false;
- });
- }
- }
- setUserConfirm(false);
- }, [editedCustomerData, userGroupData, userAuthData]);
-
- return (
- !onReady ?
- <LoadingComponent />
- :
- <Grid container sx={{ minHeight: '90vh' }}>
- <Grid item xs={12}>
- <div style={BackgroundHead}>
- <Stack direction="row" height='70px' justifyContent="flex-start" alignItems="center">
- <Typography ml={15} color='#FFF' variant="h4">{isNewRecord ? "Create GLD User" : "Maintain GLD User"}</Typography>
- </Stack>
- </div>
- </Grid>
- <Grid item xs={12}>
- <Button sx={{ ml: 3.5, mt: 2 }} style={{ border: '2px solid' }} variant="outlined" onClick={() => { navigate("/userSearchview") }}>
- <KeyboardBackspaceOutlinedIcon />
- <Typography variant="h4">Back</Typography>
- </Button>
- </Grid>
- {/*col 1*/}
- <Grid item xs={12} md={5} lg={5}>
- <Grid container>
- <Grid item xs={12} md={12} lg={12}>
- <Box xs={12} ml={4} mt={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
- <UserInformationCard
- updateUserObject={updateUserObject}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </Box>
- </Grid>
-
- <Grid item xs={12} md={12} lg={12} sx={{ mt: 3 }}>
- <Box xs={12} ml={4} mt={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
- <UserGroupCard
- updateUserGroupList={updateUserGroupList}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </Box>
- </Grid>
- </Grid>
- </Grid>
- {/*col 2*/}
- <Grid item xs={12} md={7} lg={7}>
- <Box xs={12} ml={4} mt={3} mr={3} sx={{ p: 1, border: '3px groove grey', borderRadius: '10px' }}>
- <UserAuthorityCard
- updateUserAuthList={updateUserAuthList}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </Box>
- </Grid>
-
- {/*bottom button*/}
- <Grid item s={12} md={12} lg={12} sx={{ mb: 3 }} alignItems={"end"} justifyContent="center">
- <Grid container maxWidth justifyContent="flex-end">
- <Grid item sx={{ ml: 3, mr: 3 }}>
- <Button
- size="large"
- variant="contained"
- sx={{
- textTransform: 'capitalize',
- alignItems: 'end'
- }}
- disabled={isNewRecord}
- onClick={handleDeleteClick}
- >
- Delete User
- </Button>
- <GeneralConfirmWindow
- isWindowOpen={isWindowOpen}
- title={"Attention"}
- content={`Confirm to delete User "${userData.data.username}" ?`}
- onNormalClose={handleClose}
- onConfirmClose={deleteData}
- />
- </Grid>
-
- <Grid item sx={{ ml: 3, mr: 3 }}>
- <Button
- size="large"
- variant="contained"
- type="submit"
- sx={{
- textTransform: 'capitalize',
- alignItems: 'end'
- }}
- onClick={submitData}
- >
- Save
- </Button>
- </Grid>
- </Grid>
- </Grid>
- </Grid>
- );
- };
-
- export default UserMaintainPage;
|