Commit c3e7626b authored by Ermolaev Timur's avatar Ermolaev Timur

#141 Добавил разные валидации на удаление\добавление

parent f716d6d1
...@@ -2,10 +2,11 @@ import { Button } from "@mui/material"; ...@@ -2,10 +2,11 @@ import { Button } from "@mui/material";
import { memo } from "react"; import { memo } from "react";
import { projectRoles } from "../../../constants"; import { projectRoles } from "../../../constants";
import CustomAutocomplete from "../../UI/CustomAutocomplete/CustomAutocomplete"; import CustomAutocomplete from "../../UI/CustomAutocomplete/CustomAutocomplete";
import { isValidate } from "./helpers";
function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHandler, newMember, handleClose, createNewMemberHandler }) { function NewMemberModalContent({ members, users, onChangeNewMemberHandler, onChangeRoleHandler, newMember, handleClose, createNewMemberHandler }) {
return (<> return (<>
<CustomAutocomplete <CustomAutocomplete
name={'newMember'} name={'newMember'}
...@@ -27,7 +28,7 @@ function NewMemberModalContent({ users, onChangeNewMemberHandler, onChangeRoleHa ...@@ -27,7 +28,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={() => { createNewMemberHandler() }} >Выбрать</Button> <Button onClick={() => { createNewMemberHandler() }} disabled={!isValidate(members, newMember?.userId, newMember?.roleProject)}>Выбрать</Button>
<Button onClick={() => handleClose()}>Отмена</Button> <Button onClick={() => handleClose()}>Отмена</Button>
</div> </div>
......
export const isValidate = (members, userId, role) => {
if (!userId || !role) return false
return !members.find((member) => member?.user?.id === userId)
}
\ No newline at end of file
...@@ -16,7 +16,7 @@ const styleText = { ...@@ -16,7 +16,7 @@ const styleText = {
} }
const ProjectInfo = ({ project, handleOpen }) => { const ProjectInfo = ({ project, handleOpen, currentRoleInProject }) => {
return ( return (
<Box sx={style} justifyContent={'space-between'}> <Box sx={style} justifyContent={'space-between'}>
<Typography variant="h2">Проект - {project?.title} </Typography> <Typography variant="h2">Проект - {project?.title} </Typography>
...@@ -24,20 +24,24 @@ const ProjectInfo = ({ project, handleOpen }) => { ...@@ -24,20 +24,24 @@ const ProjectInfo = ({ project, handleOpen }) => {
Дата создания проекта: {moment(project?.createdAt).format('MM.DD.YYYY')} Дата создания проекта: {moment(project?.createdAt).format('MM.DD.YYYY')}
</Typography> </Typography>
<Typography variant="body1" sx={styleText}> <Typography variant="body1" sx={styleText}>
Цвет: {project?.color} Цвет: {project?.color || 'Зеленый'}
</Typography> </Typography>
<Typography variant="body1" sx={styleText}> <Typography variant="body1" sx={styleText}>
Автор проекта: {project?.members[0]?.user.displayName} Автор проекта: {project?.members[0]?.user.displayName}
</Typography> </Typography>
<Typography variant="body1" sx={styleText}> <Typography variant="body1" sx={styleText}>
Роль в проекте: {project?.members[0]?.roleProject} Роль в проекте: {currentRoleInProject}
</Typography> </Typography>
<Typography variant="body1" sx={styleText}> <Typography variant="body1" sx={styleText}>
Задачи: Задачи:
</Typography> </Typography>
<Typography variant="body1" sx={styleText}> {currentRoleInProject === 'admin' ?
Добавить участника: <PersonAddIcon style={{ cursor: 'pointer' }} onClick={()=>{handleOpen()}} /> <Typography variant="body1" sx={styleText}>
</Typography> Добавить участника: <PersonAddIcon style={{ cursor: 'pointer' }} onClick={() => { handleOpen() }} />
</Typography>
: null
}
</Box> </Box>
); );
}; };
......
...@@ -13,7 +13,7 @@ const styleColumn = { ...@@ -13,7 +13,7 @@ const styleColumn = {
} }
const ProjectUsersColumn = ({ role, members, deleteMemberHandler }) => { const ProjectUsersColumn = ({ role, members, deleteMemberHandler, currentRoleInProject }) => {
return ( return (
<Box sx={styleColumn}> <Box sx={styleColumn}>
<ColumnTitle text={role.text} /> <ColumnTitle text={role.text} />
...@@ -22,6 +22,7 @@ const ProjectUsersColumn = ({ role, members, deleteMemberHandler }) => { ...@@ -22,6 +22,7 @@ const ProjectUsersColumn = ({ role, members, deleteMemberHandler }) => {
role={role} role={role}
members={members} members={members}
deleteMemberHandler={deleteMemberHandler} deleteMemberHandler={deleteMemberHandler}
currentRoleInProject={currentRoleInProject}
> >
</UsersList> </UsersList>
</Box> </Box>
......
import { Grid } from "@mui/material"; import { Grid } from "@mui/material";
import DeleteIcon from "@mui/icons-material/Delete"; import DeleteIcon from "@mui/icons-material/Delete";
import { memo } from "react"; import { memo } from "react";
import { useSelector } from "react-redux";
const style = { const style = {
width: '90%', width: '90%',
...@@ -12,15 +13,17 @@ const style = { ...@@ -12,15 +13,17 @@ const style = {
alignItems: 'center' alignItems: 'center'
} }
const ProjectMembersItem = ({ user, deleteMemberHandler }) => { const ProjectMembersItem = ({ user, deleteMemberHandler, currentRoleInProject }) => {
const userId = useSelector(state => state.users.user.id);
return <> return <>
<Grid sx={style}> <Grid sx={style}>
{user?.displayName} {user?.displayName}
<DeleteIcon {currentRoleInProject === 'admin' && user.id !== userId ?
sx={{cursor: 'pointer'}} <DeleteIcon
onClick={deleteMemberHandler} sx={{ cursor: 'pointer' }}
/> onClick={deleteMemberHandler}
/>
: null}
</Grid> </Grid>
</> </>
}; };
......
...@@ -9,7 +9,7 @@ const styleList = { ...@@ -9,7 +9,7 @@ const styleList = {
alignItems: 'center', alignItems: 'center',
} }
const UsersList = ({ role, members, deleteMemberHandler }) => { const UsersList = ({ role, members, deleteMemberHandler, currentRoleInProject }) => {
return ( return (
<Box sx={styleList}> <Box sx={styleList}>
{members.map((member) => { {members.map((member) => {
...@@ -19,6 +19,7 @@ const UsersList = ({ role, members, deleteMemberHandler }) => { ...@@ -19,6 +19,7 @@ const UsersList = ({ role, members, deleteMemberHandler }) => {
key={member.id} key={member.id}
user={member?.user} user={member?.user}
deleteMemberHandler={() => { deleteMemberHandler(member?.user?.id) }} deleteMemberHandler={() => { deleteMemberHandler(member?.user?.id) }}
currentRoleInProject={currentRoleInProject}
/> />
) )
} else { } else {
......
...@@ -13,7 +13,7 @@ const style = { ...@@ -13,7 +13,7 @@ const style = {
} }
const ProjectUsersColumnsWrapper = ({ members, deleteMemberHandler }) => { const ProjectUsersColumnsWrapper = ({ members, deleteMemberHandler, currentRoleInProject }) => {
return ( return (
<Box sx={style} justifyContent={'space-between'}> <Box sx={style} justifyContent={'space-between'}>
{projectRoles.map((role, i) => { {projectRoles.map((role, i) => {
...@@ -23,6 +23,7 @@ const ProjectUsersColumnsWrapper = ({ members, deleteMemberHandler }) => { ...@@ -23,6 +23,7 @@ const ProjectUsersColumnsWrapper = ({ members, deleteMemberHandler }) => {
role={role} role={role}
members={members} members={members}
deleteMemberHandler={deleteMemberHandler} deleteMemberHandler={deleteMemberHandler}
currentRoleInProject={currentRoleInProject}
/> />
) )
})} })}
......
...@@ -10,6 +10,7 @@ import DefaultModal from "../../components/UI/DefaultModal/DefaultModal" ...@@ -10,6 +10,7 @@ import DefaultModal from "../../components/UI/DefaultModal/DefaultModal"
import NewMemberModalContent from "../../components/ProjectComponents/NewMemberModalContent/NewMemberModalContent"; import NewMemberModalContent from "../../components/ProjectComponents/NewMemberModalContent/NewMemberModalContent";
const FullProject = () => { const FullProject = () => {
const { project } = useSelector(state => state.projects); const { project } = useSelector(state => state.projects);
...@@ -29,6 +30,10 @@ const FullProject = () => { ...@@ -29,6 +30,10 @@ const FullProject = () => {
return project?.project?.members || [] return project?.project?.members || []
}, [project]) }, [project])
const currentRoleInProject = useMemo(()=>{
return members.find((member) => member.user.id === user.id)?.roleProject
}, [members, user.id])
const onChangeRoleHandler = useCallback((e, value) => { const onChangeRoleHandler = useCallback((e, value) => {
setNewMember((prevState) => { return { ...prevState, roleProject: value?.value } }); setNewMember((prevState) => { return { ...prevState, roleProject: value?.value } });
}, []); }, []);
...@@ -49,23 +54,26 @@ const FullProject = () => { ...@@ -49,23 +54,26 @@ const FullProject = () => {
const createNewMemberHandler = useCallback(()=>{ const createNewMemberHandler = useCallback(()=>{
dispatch(createMember({...newMember, projectId: project?.project?.id})) dispatch(createMember({...newMember, projectId: project?.project?.id}))
setModal(false) setModal(false)
setNewMember(null)
}, [dispatch, newMember, project?.project?.id]) }, [dispatch, newMember, project?.project?.id])
const deleteMemberHandler = useCallback((id)=>{ const deleteMemberHandler = useCallback((id)=>{
dispatch(deleteMember(id, project?.project?.id)) dispatch(deleteMember(id, project?.project?.id))
}, [dispatch, project?.project?.id]) }, [dispatch, project?.project?.id])
return <> return <>
<Grid> <Grid>
<ProjectInfo <ProjectInfo
project={project.project} project={project.project}
handleOpen={handleOpen} handleOpen={handleOpen}
currentRoleInProject={currentRoleInProject}
/> />
<ProjectUsersColumnsWrapper <ProjectUsersColumnsWrapper
members={members} members={members}
deleteMemberHandler={deleteMemberHandler} deleteMemberHandler={deleteMemberHandler}
currentRoleInProject={currentRoleInProject}
/> />
<DefaultModal <DefaultModal
...@@ -79,6 +87,7 @@ const FullProject = () => { ...@@ -79,6 +87,7 @@ const FullProject = () => {
newMember={newMember} newMember={newMember}
handleClose={handleClose} handleClose={handleClose}
createNewMemberHandler={createNewMemberHandler} createNewMemberHandler={createNewMemberHandler}
members={members}
> >
</NewMemberModalContent> </NewMemberModalContent>
......
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