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.
 
 

141 lines
5.2 KiB

  1. // material-ui
  2. import {
  3. Button,
  4. Grid, TextField,
  5. Typography
  6. } from '@mui/material';
  7. import MainCard from "../../components/MainCard";
  8. import * as React from "react";
  9. import {useEffect, useState} from "react";
  10. import {GET_GROUP_MEMBER_LIST_PATH, GET_USER_COMBO_LIST} from "../../utils/ApiPathConst";
  11. import axios from "axios";
  12. import {apiPath} from "../../auth/utils";
  13. import Autocomplete from "@mui/material/Autocomplete";
  14. import {getIdList} from "../../utils/CommonFunction";
  15. //import LoadingComponent from "../extra-pages/LoadingComponent";
  16. //import UserAddTable from "./UserAddTable";
  17. import Loadable from 'components/Loadable';
  18. import { lazy } from 'react';
  19. const LoadingComponent = Loadable(lazy(() => import('../extra-pages/LoadingComponent')));
  20. const UserAddTable = Loadable(lazy(() => import('./UserAddTable')));
  21. // ==============================|| DASHBOARD - DEFAULT ||============================== //
  22. const UserAddCard = ({isCollectData, updateGroupMember,userGroupData}) => {
  23. const [currentUserData, setCurrentUserData] = React.useState({});
  24. const [onReady, setOnReady] = useState(false);
  25. const [groupUserData, setGroupUserData] = useState([]);
  26. const [userComboList, setUserComboList] = useState([]);
  27. const [selectedUser, setSelectedUser] = useState(null);
  28. function updateUserList (){
  29. const idList = getIdList(groupUserData);
  30. if(!idList.includes(selectedUser.id)){
  31. const userList = [...groupUserData, selectedUser];
  32. setGroupUserData(userList);
  33. console.log(userList);
  34. }
  35. }
  36. useEffect(() => {
  37. axios.get(`${apiPath}${GET_USER_COMBO_LIST}`)
  38. .then((response) => {
  39. if (response.status === 200) {
  40. setUserComboList(response.data.records);
  41. }
  42. })
  43. .catch(error => {
  44. console.log(error);
  45. return false;
  46. });
  47. }, []);
  48. useEffect(() => {
  49. //if user data from parent are not null
  50. if (Object.keys(userGroupData).length > 0 && userGroupData !== undefined) {
  51. setCurrentUserData(userGroupData.data);
  52. }
  53. }, [userGroupData]);
  54. useEffect(() => {
  55. //if state data are ready and assign to different field
  56. if (Object.keys(userGroupData).length > 0 &&currentUserData !== undefined) {
  57. axios.get(`${apiPath}${GET_GROUP_MEMBER_LIST_PATH}?id=${userGroupData.data.id}`)
  58. .then((response) => {
  59. if (response.status === 200) {
  60. setGroupUserData(response.data.records);
  61. }
  62. setOnReady(true);
  63. })
  64. .catch(error => {
  65. console.log(error);
  66. return false;
  67. });
  68. }
  69. }, [currentUserData]);
  70. useEffect(() => {
  71. //upload latest data to parent
  72. updateGroupMember(groupUserData);
  73. }, [isCollectData]);
  74. return (
  75. !onReady ?
  76. <LoadingComponent/>
  77. :
  78. <MainCard elevation={0}
  79. border={false}
  80. content={false}
  81. >
  82. <Typography variant="h5" sx={{mt: 3, ml: 3}}>
  83. User(s)
  84. </Typography>
  85. <Grid item xs={12} s={12} md={12} lg={12} sx={{ml: 3, mr: 3, mt: 2}}>
  86. <Grid container alignItems={"center"}>
  87. <Grid item xs={3} s={3} md={2} lg={2}
  88. sx={{display: 'flex', alignItems: 'center'}}>
  89. User:
  90. </Grid>
  91. <Grid item xs={6} s={5} md={5} lg={5}>
  92. <Autocomplete
  93. disablePortal
  94. id="user-combo"
  95. value={selectedUser === null ? null : selectedUser}
  96. options={userComboList}
  97. onChange={(event, newValue) => {
  98. console.log(newValue)
  99. setSelectedUser(newValue);
  100. }}
  101. renderInput={(params) => <TextField {...params} />}
  102. />
  103. </Grid>
  104. <Grid item xs={3} s={3} md={4} lg={4} sx={{ml: 3}}>
  105. <Button
  106. size="large"
  107. variant="contained"
  108. type="submit"
  109. sx={{
  110. textTransform: 'capitalize',
  111. alignItems: 'end'
  112. }}
  113. onClick={updateUserList}
  114. >
  115. Add
  116. </Button>
  117. </Grid>
  118. </Grid>
  119. </Grid>
  120. <UserAddTable
  121. setGroupUserData={setGroupUserData}
  122. userList={groupUserData}
  123. />
  124. </MainCard>
  125. );
  126. };
  127. export default UserAddCard;