2020-08-23 19:27:56 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-04-10 04:40:22 +02:00
|
|
|
import styled from 'styled-components/macro';
|
2020-05-27 02:53:31 +02:00
|
|
|
import GlobalTopNavbar from 'App/TopNavbar';
|
2020-07-05 01:02:57 +02:00
|
|
|
import Empty from 'shared/undraw/Empty';
|
2020-06-21 00:49:11 +02:00
|
|
|
import {
|
|
|
|
useCreateTeamMutation,
|
|
|
|
useGetProjectsQuery,
|
|
|
|
useCreateProjectMutation,
|
|
|
|
GetProjectsDocument,
|
2020-06-23 23:55:17 +02:00
|
|
|
GetProjectsQuery,
|
2020-06-21 00:49:11 +02:00
|
|
|
} from 'shared/generated/graphql';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
import { Link } from 'react-router-dom';
|
2020-06-01 04:20:03 +02:00
|
|
|
import NewProject from 'shared/components/NewProject';
|
2021-04-29 04:32:19 +02:00
|
|
|
import { useCurrentUser } from 'App/context';
|
2020-06-21 00:49:11 +02:00
|
|
|
import Button from 'shared/components/Button';
|
|
|
|
import { usePopup, Popup } from 'shared/components/PopupMenu';
|
|
|
|
import { useForm } from 'react-hook-form';
|
|
|
|
import Input from 'shared/components/Input';
|
2020-06-23 23:55:17 +02:00
|
|
|
import updateApolloCache from 'shared/utils/cache';
|
|
|
|
import produce from 'immer';
|
2020-08-23 19:27:56 +02:00
|
|
|
import NOOP from 'shared/utils/noop';
|
2020-12-18 00:13:34 +01:00
|
|
|
import theme from 'App/ThemeStyles';
|
|
|
|
import { mixin } from '../shared/utils/styles';
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
type CreateTeamData = { teamName: string };
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
type CreateTeamFormProps = {
|
|
|
|
onCreateTeam: (teamName: string) => void;
|
|
|
|
};
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
const CreateTeamFormContainer = styled.form``;
|
|
|
|
|
2020-09-20 03:20:36 +02:00
|
|
|
const CreateTeamButton = styled(Button)`
|
|
|
|
width: 100%;
|
|
|
|
`;
|
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
const CreateTeamForm: React.FC<CreateTeamFormProps> = ({ onCreateTeam }) => {
|
2020-08-23 19:27:56 +02:00
|
|
|
const { register, handleSubmit } = useForm<CreateTeamData>();
|
2020-06-21 00:49:11 +02:00
|
|
|
const createTeam = (data: CreateTeamData) => {
|
|
|
|
onCreateTeam(data.teamName);
|
|
|
|
};
|
|
|
|
return (
|
|
|
|
<CreateTeamFormContainer onSubmit={handleSubmit(createTeam)}>
|
|
|
|
<Input
|
|
|
|
width="100%"
|
|
|
|
label="Team name"
|
|
|
|
id="teamName"
|
|
|
|
name="teamName"
|
|
|
|
variant="alternate"
|
|
|
|
ref={register({ required: 'Team name is required' })}
|
|
|
|
/>
|
|
|
|
<CreateTeamButton type="submit">Create</CreateTeamButton>
|
|
|
|
</CreateTeamFormContainer>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const ProjectAddTile = styled.div`
|
2020-12-18 00:13:34 +01:00
|
|
|
background-color: ${props => mixin.rgba(props.theme.colors.bg.primary, 0.4)};
|
2020-06-21 00:49:11 +02:00
|
|
|
background-size: cover;
|
|
|
|
background-position: 50%;
|
|
|
|
color: #fff;
|
|
|
|
line-height: 20px;
|
|
|
|
padding: 8px;
|
|
|
|
position: relative;
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
border-radius: 3px;
|
|
|
|
display: block;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectTile = styled(Link)<{ color: string }>`
|
|
|
|
background-color: ${props => props.color};
|
|
|
|
background-size: cover;
|
|
|
|
background-position: 50%;
|
|
|
|
color: #fff;
|
|
|
|
line-height: 20px;
|
|
|
|
padding: 8px;
|
|
|
|
position: relative;
|
|
|
|
text-decoration: none;
|
|
|
|
|
|
|
|
border-radius: 3px;
|
|
|
|
display: block;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectTileFade = styled.div`
|
|
|
|
background-color: rgba(0, 0, 0, 0.15);
|
|
|
|
bottom: 0;
|
|
|
|
left: 0;
|
|
|
|
position: absolute;
|
|
|
|
right: 0;
|
|
|
|
top: 0;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectListItem = styled.li`
|
|
|
|
width: 23.5%;
|
|
|
|
padding: 0;
|
|
|
|
margin: 0 2% 2% 0;
|
|
|
|
|
|
|
|
box-sizing: border-box;
|
|
|
|
position: relative;
|
|
|
|
cursor: pointer;
|
|
|
|
|
|
|
|
&:hover ${ProjectTileFade} {
|
|
|
|
background-color: rgba(0, 0, 0, 0.25);
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectList = styled.ul`
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
|
|
|
|
& ${ProjectListItem}:nth-of-type(4n) {
|
|
|
|
margin-right: 0;
|
|
|
|
}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectTileDetails = styled.div`
|
|
|
|
display: flex;
|
|
|
|
height: 80px;
|
|
|
|
position: relative;
|
|
|
|
flex-direction: column;
|
|
|
|
justify-content: space-between;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectAddTileDetails = styled.div`
|
|
|
|
display: flex;
|
|
|
|
height: 80px;
|
|
|
|
position: relative;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
justify-content: center;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectTileName = styled.div<{ centered?: boolean }>`
|
|
|
|
flex: 0 0 auto;
|
|
|
|
font-size: 16px;
|
|
|
|
font-weight: 700;
|
|
|
|
display: inline-block;
|
|
|
|
overflow: hidden;
|
|
|
|
max-height: 40px;
|
|
|
|
width: 100%;
|
|
|
|
word-wrap: break-word;
|
|
|
|
${props => props.centered && 'text-align: center;'}
|
|
|
|
`;
|
|
|
|
|
|
|
|
const Wrapper = styled.div`
|
2020-06-23 22:20:53 +02:00
|
|
|
position: relative;
|
2020-06-21 00:49:11 +02:00
|
|
|
display: flex;
|
|
|
|
flex-direction: row;
|
|
|
|
align-items: flex-start;
|
|
|
|
justify-content: center;
|
2021-01-06 02:06:49 +01:00
|
|
|
overflow-y: auto;
|
2020-06-21 00:49:11 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectSectionTitleWrapper = styled.div`
|
|
|
|
align-items: center;
|
|
|
|
display: flex;
|
2020-06-23 22:20:53 +02:00
|
|
|
justify-content: space-between;
|
2020-06-21 00:49:11 +02:00
|
|
|
height: 32px;
|
|
|
|
margin-bottom: 24px;
|
|
|
|
padding: 8px 0;
|
|
|
|
position: relative;
|
2020-06-23 22:20:53 +02:00
|
|
|
margin-top: 16px;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const SectionActions = styled.div`
|
|
|
|
display: flex;
|
|
|
|
align-items: center;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const SectionAction = styled(Button)`
|
|
|
|
padding: 6px 12px;
|
|
|
|
`;
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-06-23 22:20:53 +02:00
|
|
|
const SectionActionLink = styled(Link)`
|
|
|
|
margin-right: 8px;
|
2020-06-21 00:49:11 +02:00
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectSectionTitle = styled.h3`
|
|
|
|
font-size: 16px;
|
2020-12-18 00:13:34 +01:00
|
|
|
color: ${props => props.theme.colors.text.primary};
|
2020-04-10 04:40:22 +02:00
|
|
|
`;
|
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
const ProjectsContainer = styled.div`
|
|
|
|
margin: 40px 16px 0;
|
|
|
|
width: 100%;
|
|
|
|
max-width: 825px;
|
|
|
|
min-width: 288px;
|
|
|
|
`;
|
2020-08-23 19:27:56 +02:00
|
|
|
|
2020-06-21 00:49:11 +02:00
|
|
|
const AddTeamButton = styled(Button)`
|
|
|
|
padding: 6px 12px;
|
2020-06-23 22:20:53 +02:00
|
|
|
position: absolute;
|
|
|
|
top: 6px;
|
|
|
|
right: 12px;
|
2020-06-21 00:49:11 +02:00
|
|
|
`;
|
2020-07-05 01:02:57 +02:00
|
|
|
|
2020-06-23 22:20:53 +02:00
|
|
|
type ShowNewProject = {
|
|
|
|
open: boolean;
|
|
|
|
initialTeamID: null | string;
|
|
|
|
};
|
2020-04-20 05:02:55 +02:00
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const Projects = () => {
|
2020-06-23 22:20:53 +02:00
|
|
|
const { showPopup, hidePopup } = usePopup();
|
2020-12-23 22:55:17 +01:00
|
|
|
const { loading, data } = useGetProjectsQuery({ pollInterval: 3000, fetchPolicy: 'cache-and-network' });
|
2020-06-13 00:21:58 +02:00
|
|
|
useEffect(() => {
|
2020-08-07 03:50:35 +02:00
|
|
|
document.title = 'Taskcafé';
|
2020-06-13 00:21:58 +02:00
|
|
|
}, []);
|
2020-06-01 04:20:03 +02:00
|
|
|
const [createProject] = useCreateProjectMutation({
|
|
|
|
update: (client, newProject) => {
|
2020-06-23 23:55:17 +02:00
|
|
|
updateApolloCache<GetProjectsQuery>(client, GetProjectsDocument, cache =>
|
|
|
|
produce(cache, draftCache => {
|
2020-12-19 03:34:35 +01:00
|
|
|
if (newProject.data) {
|
|
|
|
draftCache.projects.push({ ...newProject.data.createProject });
|
|
|
|
}
|
2020-06-23 23:55:17 +02:00
|
|
|
}),
|
|
|
|
);
|
2020-06-01 04:20:03 +02:00
|
|
|
},
|
|
|
|
});
|
2020-06-23 22:20:53 +02:00
|
|
|
|
|
|
|
const [showNewProject, setShowNewProject] = useState<ShowNewProject>({ open: false, initialTeamID: null });
|
2020-08-23 19:27:56 +02:00
|
|
|
const { user } = useCurrentUser();
|
2020-06-23 22:20:53 +02:00
|
|
|
const [createTeam] = useCreateTeamMutation({
|
|
|
|
update: (client, createData) => {
|
2020-06-23 23:55:17 +02:00
|
|
|
updateApolloCache<GetProjectsQuery>(client, GetProjectsDocument, cache =>
|
|
|
|
produce(cache, draftCache => {
|
2020-12-19 03:34:35 +01:00
|
|
|
if (createData.data) {
|
|
|
|
draftCache.teams.push({ ...createData.data?.createTeam });
|
|
|
|
}
|
2020-06-23 23:55:17 +02:00
|
|
|
}),
|
|
|
|
);
|
2020-06-23 22:20:53 +02:00
|
|
|
},
|
|
|
|
});
|
2020-06-21 00:49:11 +02:00
|
|
|
|
2020-12-18 00:13:34 +01:00
|
|
|
const colors = theme.colors.multiColors;
|
2020-08-01 03:01:14 +02:00
|
|
|
if (data && user) {
|
2020-06-21 00:49:11 +02:00
|
|
|
const { projects, teams, organizations } = data;
|
|
|
|
const organizationID = organizations[0].id ?? null;
|
2020-09-20 03:20:36 +02:00
|
|
|
const personalProjects = projects
|
|
|
|
.filter(p => p.team === null)
|
|
|
|
.sort((a, b) => {
|
|
|
|
const textA = a.name.toUpperCase();
|
|
|
|
const textB = b.name.toUpperCase();
|
|
|
|
return textA < textB ? -1 : textA > textB ? 1 : 0; // eslint-disable-line no-nested-ternary
|
|
|
|
});
|
2020-08-01 03:01:14 +02:00
|
|
|
const projectTeams = teams
|
|
|
|
.sort((a, b) => {
|
|
|
|
const textA = a.name.toUpperCase();
|
|
|
|
const textB = b.name.toUpperCase();
|
2020-08-23 19:27:56 +02:00
|
|
|
return textA < textB ? -1 : textA > textB ? 1 : 0; // eslint-disable-line no-nested-ternary
|
2020-08-01 03:01:14 +02:00
|
|
|
})
|
|
|
|
.map(team => {
|
|
|
|
return {
|
|
|
|
id: team.id,
|
|
|
|
name: team.name,
|
|
|
|
projects: projects
|
2020-09-20 03:20:36 +02:00
|
|
|
.filter(project => project.team && project.team.id === team.id)
|
2020-08-01 03:01:14 +02:00
|
|
|
.sort((a, b) => {
|
|
|
|
const textA = a.name.toUpperCase();
|
|
|
|
const textB = b.name.toUpperCase();
|
2020-08-23 19:27:56 +02:00
|
|
|
return textA < textB ? -1 : textA > textB ? 1 : 0; // eslint-disable-line no-nested-ternary
|
2020-08-01 03:01:14 +02:00
|
|
|
}),
|
|
|
|
};
|
|
|
|
});
|
2020-04-10 04:40:22 +02:00
|
|
|
return (
|
2020-05-27 02:53:31 +02:00
|
|
|
<>
|
2020-08-23 19:27:56 +02:00
|
|
|
<GlobalTopNavbar onSaveProjectName={NOOP} projectID={null} name={null} />
|
2020-06-21 00:49:11 +02:00
|
|
|
<Wrapper>
|
|
|
|
<ProjectsContainer>
|
2021-04-29 04:32:19 +02:00
|
|
|
{true && ( // TODO: add permision check
|
2020-08-01 03:01:14 +02:00
|
|
|
<AddTeamButton
|
|
|
|
variant="outline"
|
|
|
|
onClick={$target => {
|
|
|
|
showPopup(
|
|
|
|
$target,
|
|
|
|
<Popup
|
|
|
|
title="Create team"
|
|
|
|
tab={0}
|
|
|
|
onClose={() => {
|
|
|
|
hidePopup();
|
2020-06-21 00:49:11 +02:00
|
|
|
}}
|
2020-08-01 03:01:14 +02:00
|
|
|
>
|
|
|
|
<CreateTeamForm
|
|
|
|
onCreateTeam={teamName => {
|
|
|
|
if (organizationID) {
|
|
|
|
createTeam({ variables: { name: teamName, organizationID } });
|
|
|
|
hidePopup();
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
</Popup>,
|
|
|
|
);
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
Add Team
|
|
|
|
</AddTeamButton>
|
|
|
|
)}
|
2020-09-20 03:20:36 +02:00
|
|
|
<div>
|
|
|
|
<ProjectSectionTitleWrapper>
|
|
|
|
<ProjectSectionTitle>Personal Projects</ProjectSectionTitle>
|
|
|
|
</ProjectSectionTitleWrapper>
|
|
|
|
<ProjectList>
|
|
|
|
{personalProjects.map((project, idx) => (
|
|
|
|
<ProjectListItem key={project.id}>
|
|
|
|
<ProjectTile color={colors[idx % 5]} to={`/projects/${project.id}`}>
|
|
|
|
<ProjectTileFade />
|
|
|
|
<ProjectTileDetails>
|
|
|
|
<ProjectTileName>{project.name}</ProjectTileName>
|
|
|
|
</ProjectTileDetails>
|
|
|
|
</ProjectTile>
|
|
|
|
</ProjectListItem>
|
|
|
|
))}
|
|
|
|
<ProjectListItem>
|
|
|
|
<ProjectAddTile
|
|
|
|
onClick={() => {
|
|
|
|
setShowNewProject({ open: true, initialTeamID: 'no-team' });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ProjectTileFade />
|
|
|
|
<ProjectAddTileDetails>
|
|
|
|
<ProjectTileName centered>Create new project</ProjectTileName>
|
|
|
|
</ProjectAddTileDetails>
|
|
|
|
</ProjectAddTile>
|
|
|
|
</ProjectListItem>
|
|
|
|
</ProjectList>
|
|
|
|
</div>
|
2020-06-21 00:49:11 +02:00
|
|
|
{projectTeams.map(team => {
|
|
|
|
return (
|
|
|
|
<div key={team.id}>
|
|
|
|
<ProjectSectionTitleWrapper>
|
|
|
|
<ProjectSectionTitle>{team.name}</ProjectSectionTitle>
|
2021-04-29 04:32:19 +02:00
|
|
|
{true && ( // TODO: add permision check
|
2020-08-01 03:01:14 +02:00
|
|
|
<SectionActions>
|
|
|
|
<SectionActionLink to={`/teams/${team.id}`}>
|
|
|
|
<SectionAction variant="outline">Projects</SectionAction>
|
|
|
|
</SectionActionLink>
|
|
|
|
<SectionActionLink to={`/teams/${team.id}/members`}>
|
|
|
|
<SectionAction variant="outline">Members</SectionAction>
|
|
|
|
</SectionActionLink>
|
|
|
|
<SectionActionLink to={`/teams/${team.id}/settings`}>
|
|
|
|
<SectionAction variant="outline">Settings</SectionAction>
|
|
|
|
</SectionActionLink>
|
|
|
|
</SectionActions>
|
|
|
|
)}
|
2020-06-21 00:49:11 +02:00
|
|
|
</ProjectSectionTitleWrapper>
|
|
|
|
<ProjectList>
|
|
|
|
{team.projects.map((project, idx) => (
|
|
|
|
<ProjectListItem key={project.id}>
|
|
|
|
<ProjectTile color={colors[idx % 5]} to={`/projects/${project.id}`}>
|
|
|
|
<ProjectTileFade />
|
|
|
|
<ProjectTileDetails>
|
|
|
|
<ProjectTileName>{project.name}</ProjectTileName>
|
|
|
|
</ProjectTileDetails>
|
|
|
|
</ProjectTile>
|
|
|
|
</ProjectListItem>
|
|
|
|
))}
|
2021-04-29 04:32:19 +02:00
|
|
|
{true && ( // TODO: add permision check
|
2020-08-01 03:01:14 +02:00
|
|
|
<ProjectListItem>
|
|
|
|
<ProjectAddTile
|
|
|
|
onClick={() => {
|
|
|
|
setShowNewProject({ open: true, initialTeamID: team.id });
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
<ProjectTileFade />
|
|
|
|
<ProjectAddTileDetails>
|
|
|
|
<ProjectTileName centered>Create new project</ProjectTileName>
|
|
|
|
</ProjectAddTileDetails>
|
|
|
|
</ProjectAddTile>
|
|
|
|
</ProjectListItem>
|
|
|
|
)}
|
2020-06-21 00:49:11 +02:00
|
|
|
</ProjectList>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
2020-06-23 22:20:53 +02:00
|
|
|
{showNewProject.open && (
|
2020-06-21 00:49:11 +02:00
|
|
|
<NewProject
|
2020-06-23 22:20:53 +02:00
|
|
|
initialTeamID={showNewProject.initialTeamID}
|
2020-06-21 00:49:11 +02:00
|
|
|
onCreateProject={(name, teamID) => {
|
2020-08-01 03:01:14 +02:00
|
|
|
if (user) {
|
2020-09-20 03:20:36 +02:00
|
|
|
createProject({ variables: { teamID, name } });
|
2020-06-23 22:20:53 +02:00
|
|
|
setShowNewProject({ open: false, initialTeamID: null });
|
2020-06-21 00:49:11 +02:00
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClose={() => {
|
2020-06-23 22:20:53 +02:00
|
|
|
setShowNewProject({ open: false, initialTeamID: null });
|
2020-06-21 00:49:11 +02:00
|
|
|
}}
|
|
|
|
teams={teams}
|
2020-05-27 02:53:31 +02:00
|
|
|
/>
|
2020-06-21 00:49:11 +02:00
|
|
|
)}
|
|
|
|
</ProjectsContainer>
|
|
|
|
</Wrapper>
|
2020-05-27 02:53:31 +02:00
|
|
|
</>
|
2020-04-10 04:40:22 +02:00
|
|
|
);
|
|
|
|
}
|
2020-12-24 03:02:38 +01:00
|
|
|
return <GlobalTopNavbar onSaveProjectName={NOOP} projectID={null} name={null} />;
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Projects;
|