|
- // material-ui
- import {
- Button,
- Grid,
- Typography,
- Stack
- } 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} 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,
- } from "../../utils/CommonFunction";
-
- // ==============================|| 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);
- 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 === 200) {
- // notifySaveSuccess();
- navigate('/userSearchview');
- }
- })
- .catch(error => {
- console.log(error);
- return false;
- });
- }
- setUserConfirm(false);
- }, [editedCustomerData,userGroupData,userAuthData]);
-
- return (
- !onReady ?
- <LoadingComponent/>
- :
- <Grid container sx={{minHeight: '90vh', paddingTop: '53px'}}>
- <Grid item xs={12} height='60px'>
- <Stack direction="row" height='100%' justifyContent="flex-start" alignItems="center">
- <Typography variant="h5">Maintain User</Typography>
- </Stack>
- </Grid>
- {/*col 1*/}
- <Grid item xs={12} md={5} lg={5}>
- <Grid container>
- <Grid item xs={12} md={12} lg={12}>
- <UserInformationCard
- updateUserObject={updateUserObject}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </Grid>
-
- <Grid item xs={12} md={12} lg={12} sx={{mt: 3}}>
- <UserGroupCard
- updateUserGroupList={updateUserGroupList}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </Grid>
- </Grid>
- </Grid>
- {/*col 2*/}
- <Grid item xs={12} md={7} lg={7}>
- <UserAuthorityCard
- updateUserAuthList={updateUserAuthList}
- userData={userData}
- isCollectData={isCollectData}
- isNewRecord={isNewRecord}
- />
- </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, mb: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 User
- </Button>
- </Grid>
- </Grid>
- </Grid>
- </Grid>
- );
- };
-
- export default UserMaintainPage;
|