Commit f716d6d1 authored by Ermolaev Timur's avatar Ermolaev Timur

#141 Добавил функцию удаления

parent 8398b8d8
import { Box, Button, Grid, Modal } from "@mui/material";
import { useState } from "react";
import { useSelector, useDispatch } from "react-redux";
import { Typography } from "@mui/material";
import TextField from '@mui/material/TextField';
import Autocomplete from '@mui/material/Autocomplete';
import { useParams } from "react-router-dom";
import PersonAddIcon from '@mui/icons-material/PersonAdd';
import { useEffect } from "react";
import { fetchProject } from "../../store/actions/projectsActions";
const style = {
position: 'relative',
top: '50%',
left: '50%',
transform: 'translate(-50%, -50%)',
width: 400,
bgcolor: 'background.paper',
border: '2px solid #000',
boxShadow: 24,
p: 4,
};
const MemberForm = ({ onSubmit, members }) => {
const users = useSelector(state => state.users.users)
const [role, setRole] = useState([{ role: "user" }, { role: "admin" }, { role: "watcher" }])
const params = useParams()
const dispatch = useDispatch();
const [open, setOpen] = useState(false);
const handleOpen = () => setOpen(true);
const handleClose = () => setOpen(false);
const [state, setState] = useState({
roleProject: "",
user: "",
projectId: ""
});
const submitFormHandler = (e) => {
e.preventDefault();
let members = { roleProject: state.roleProject, projectId: params.id, userId: state.user.id }
console.log(members);
onSubmit(members);
setOpen(false);
};
useEffect(() => {
dispatch(fetchProject(params.id))
}, [params]);
const memberChangeHandler = (e, value) => {
setState(() => { return { ...state, user: value, projectId: params.id } });
console.log("memberChangeHandler" + value)
}
const roleChangeHandler = (e, value) => {
setState(() => { return { ...state, roleProject: value } });
console.log(value)
}
return (
<div >
<PersonAddIcon onClick={handleOpen} style={{ marginLeft: "30px", marginTop: "-3px", cursor: 'pointer' }} >Добавить участника</PersonAddIcon>
<Modal
open={open}
onClose={handleClose}
aria-labelledby="modal-modal-title"
aria-describedby="modal-modal-description"
>
<Box sx={style}>
<form onSubmit={submitFormHandler}>
<Grid container direction="column" spacing={2}>
<Typography variant="h5" style={{ margin: "5px", textAlign: "center" }} >Новый участник</Typography>
<Autocomplete
id="free-solo-demo"
freeSolo
options={users}
getOptionLabel={(item) => (members[0]?.user?.displayName !== item.displayName) ? (item.displayName || "") : ""}
onChange={memberChangeHandler}
name={"userId"}
value={state.user}
renderInput={(params) => <TextField
style={{ margin: "5px" }}
label={"Участник"}
state={state}
{...params} />}
/>
<Autocomplete
id="free-solo-demo"
freeSolo
options={role?.map((role) => role.role)}
value={state.roleProject}
onChange={roleChangeHandler}
renderInput={(params) => <TextField
style={{ margin: "5px" }}
name={"roleProject"}
label={"Роль в проекте"}
state={state}
{...params} />}
/>
<Grid item>
<Button
type="submit"
color="primary"
variant="contained"
>
Create
</Button>
</Grid>
</Grid>
</form>
</Box>
</Modal>
</div>
);
};
export default MemberForm;
\ No newline at end of file
...@@ -5,7 +5,7 @@ import CustomAutocomplete from "../../UI/CustomAutocomplete/CustomAutocomplete"; ...@@ -5,7 +5,7 @@ import CustomAutocomplete from "../../UI/CustomAutocomplete/CustomAutocomplete";
function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHandler, newMember, handleClose, createNewUser }) { function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHandler, newMember, handleClose, createNewMemberHandler }) {
return (<> return (<>
<CustomAutocomplete <CustomAutocomplete
name={'newMember'} name={'newMember'}
...@@ -27,7 +27,7 @@ function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHa ...@@ -27,7 +27,7 @@ function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHa
style={{ marginBottom: '15px' }} style={{ marginBottom: '15px' }}
/> />
<div style={{ display: 'flex', justifyContent: 'space-between', margin: '10px 0' }}> <div style={{ display: 'flex', justifyContent: 'space-between', margin: '10px 0' }}>
<Button onClick={() => { createNewUser() }} >Выбрать</Button> <Button onClick={() => { createNewMemberHandler() }} >Выбрать</Button>
<Button onClick={() => handleClose()}>Отмена</Button> <Button onClick={() => handleClose()}>Отмена</Button>
</div> </div>
......
...@@ -13,14 +13,16 @@ const styleColumn = { ...@@ -13,14 +13,16 @@ const styleColumn = {
} }
const ProjectUsersColumn = ({ role, members }) => { const ProjectUsersColumn = ({ role, members, deleteMemberHandler }) => {
return ( return (
<Box sx={styleColumn}> <Box sx={styleColumn}>
<ColumnTitle text={role.text} /> <ColumnTitle text={role.text} />
<UsersList <UsersList
role={role} role={role}
members={members}> members={members}
deleteMemberHandler={deleteMemberHandler}
>
</UsersList> </UsersList>
</Box> </Box>
); );
......
...@@ -12,12 +12,14 @@ const style = { ...@@ -12,12 +12,14 @@ const style = {
alignItems: 'center' alignItems: 'center'
} }
const ProjectMembersItem = ({ user }) => { const ProjectMembersItem = ({ user, deleteMemberHandler }) => {
return <> return <>
<Grid sx={style}> <Grid sx={style}>
{user?.displayName} {user?.displayName}
<DeleteIcon <DeleteIcon
sx={{cursor: 'pointer'}}
onClick={deleteMemberHandler}
/> />
</Grid> </Grid>
</> </>
......
...@@ -9,7 +9,7 @@ const styleList = { ...@@ -9,7 +9,7 @@ const styleList = {
alignItems: 'center', alignItems: 'center',
} }
const UsersList = ({ role, members }) => { const UsersList = ({ role, members, deleteMemberHandler }) => {
return ( return (
<Box sx={styleList}> <Box sx={styleList}>
{members.map((member) => { {members.map((member) => {
...@@ -18,6 +18,7 @@ const UsersList = ({ role, members }) => { ...@@ -18,6 +18,7 @@ const UsersList = ({ role, members }) => {
<UserItem <UserItem
key={member.id} key={member.id}
user={member?.user} user={member?.user}
deleteMemberHandler={() => { deleteMemberHandler(member?.user?.id) }}
/> />
) )
} else { } else {
......
...@@ -13,7 +13,7 @@ const style = { ...@@ -13,7 +13,7 @@ const style = {
} }
const ProjectUsersColumnsWrapper = ({ members }) => { const ProjectUsersColumnsWrapper = ({ members, deleteMemberHandler }) => {
return ( return (
<Box sx={style} justifyContent={'space-between'}> <Box sx={style} justifyContent={'space-between'}>
{projectRoles.map((role, i) => { {projectRoles.map((role, i) => {
...@@ -22,6 +22,7 @@ const ProjectUsersColumnsWrapper = ({ members }) => { ...@@ -22,6 +22,7 @@ const ProjectUsersColumnsWrapper = ({ members }) => {
key={i} key={i}
role={role} role={role}
members={members} members={members}
deleteMemberHandler={deleteMemberHandler}
/> />
) )
})} })}
......
...@@ -2,7 +2,7 @@ import { Grid } from "@mui/material"; ...@@ -2,7 +2,7 @@ import { Grid } from "@mui/material";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { useSelector, useDispatch } from "react-redux"; import { useSelector, useDispatch } from "react-redux";
import { useCallback, useEffect, useMemo, useState } from "react"; import { useCallback, useEffect, useMemo, useState } from "react";
import { createMember, fetchProject } from "../../store/actions/projectsActions"; import { createMember, deleteMember, fetchProject } from "../../store/actions/projectsActions";
import { fetchUsers, fetchUsersAllFields } from "../../store/actions/usersActions"; import { fetchUsers, fetchUsersAllFields } from "../../store/actions/usersActions";
import ProjectUsersColumnsWrapper from "../../components/ProjectComponents/ProjectUsersColumnsWrapper/ProjectUsersColumnsWrapper"; import ProjectUsersColumnsWrapper from "../../components/ProjectComponents/ProjectUsersColumnsWrapper/ProjectUsersColumnsWrapper";
import ProjectInfo from "../../components/ProjectComponents/ProjectInfo/ProjectInfo"; import ProjectInfo from "../../components/ProjectComponents/ProjectInfo/ProjectInfo";
...@@ -29,6 +29,14 @@ const FullProject = () => { ...@@ -29,6 +29,14 @@ const FullProject = () => {
return project?.project?.members || [] return project?.project?.members || []
}, [project]) }, [project])
const onChangeRoleHandler = useCallback((e, value) => {
setNewMember((prevState) => { return { ...prevState, roleProject: value?.value } });
}, []);
const onChangeNewMemberHandler = useCallback((e, value) => {
setNewMember((prevState) => { return { ...prevState, userId: value?.id } });
}, []);
const handleOpen = useCallback(async () => { const handleOpen = useCallback(async () => {
setModal(true) setModal(true)
}, []) }, [])
...@@ -38,19 +46,14 @@ const FullProject = () => { ...@@ -38,19 +46,14 @@ const FullProject = () => {
setNewMember(null) setNewMember(null)
}, []) }, [])
const createNewUser = useCallback( async ()=>{ const createNewMemberHandler = useCallback(()=>{
await dispatch(createMember({...newMember, projectId: project?.project?.id})) dispatch(createMember({...newMember, projectId: project?.project?.id}))
setModal(false) setModal(false)
await dispatch(fetchProject(params.id)) }, [dispatch, newMember, project?.project?.id])
}, [dispatch, newMember, project?.project?.id, params.id])
const onChangeRoleHandler = useCallback((e, value) => { const deleteMemberHandler = useCallback((id)=>{
setNewMember((prevState) => { return { ...prevState, roleProject: value?.value } }); dispatch(deleteMember(id, project?.project?.id))
}, []); }, [dispatch, project?.project?.id])
const onChangeNewMemberHandler = useCallback((e, value) => {
setNewMember((prevState) => { return { ...prevState, userId: value?.id } });
}, []);
return <> return <>
<Grid> <Grid>
...@@ -62,6 +65,7 @@ const FullProject = () => { ...@@ -62,6 +65,7 @@ const FullProject = () => {
<ProjectUsersColumnsWrapper <ProjectUsersColumnsWrapper
members={members} members={members}
deleteMemberHandler={deleteMemberHandler}
/> />
<DefaultModal <DefaultModal
...@@ -74,7 +78,7 @@ const FullProject = () => { ...@@ -74,7 +78,7 @@ const FullProject = () => {
onChangeRoleHandler={onChangeRoleHandler} onChangeRoleHandler={onChangeRoleHandler}
newMember={newMember} newMember={newMember}
handleClose={handleClose} handleClose={handleClose}
createNewUser={createNewUser} createNewMemberHandler={createNewMemberHandler}
> >
</NewMemberModalContent> </NewMemberModalContent>
......
...@@ -51,9 +51,9 @@ export const deleteMember = (memberId, projectId) => { ...@@ -51,9 +51,9 @@ export const deleteMember = (memberId, projectId) => {
return async (dispatch) => { return async (dispatch) => {
dispatch(deleteMemberRequest()); dispatch(deleteMemberRequest());
try { try {
await axios.delete(`/projects/remove-user/${memberId}`, { data: { projectId: projectId } }); await axios.delete(`/projects/remove-user/${memberId}`, {data: {projectId: projectId}});
dispatch(deleteMemberSuccess()) dispatch(deleteMemberSuccess())
dispatch(fetchProject(projectId)) await dispatch(fetchProject(projectId))
} catch (error) { } catch (error) {
dispatch(deleteMemberFailure(error.response.data)); dispatch(deleteMemberFailure(error.response.data));
} }
...@@ -118,6 +118,7 @@ export const createMember = (memberData) => { ...@@ -118,6 +118,7 @@ export const createMember = (memberData) => {
try { try {
await axios.post("/projects/add-user", memberData); await axios.post("/projects/add-user", memberData);
dispatch(createMemberSuccess()); dispatch(createMemberSuccess());
await dispatch(fetchProject(memberData?.projectId))
} catch (e) { } catch (e) {
dispatch(fetchProjectsError(e)); dispatch(fetchProjectsError(e));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment