2020-06-13 00:21:58 +02:00
|
|
|
import React, { useState, useContext, 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-06-01 04:20:03 +02:00
|
|
|
import { useGetProjectsQuery, useCreateProjectMutation, GetProjectsDocument } from 'shared/generated/graphql';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-06-01 04:20:03 +02:00
|
|
|
import ProjectGridItem, { AddProjectItem } from 'shared/components/ProjectGridItem';
|
2020-04-10 04:40:22 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
|
|
|
import Navbar from 'App/Navbar';
|
2020-06-01 04:20:03 +02:00
|
|
|
import NewProject from 'shared/components/NewProject';
|
|
|
|
import UserIDContext from 'App/context';
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
const MainContent = styled.div`
|
|
|
|
padding: 0 0 50px 80px;
|
|
|
|
height: 100%;
|
|
|
|
background: #262c49;
|
|
|
|
`;
|
|
|
|
|
|
|
|
const ProjectGrid = styled.div`
|
|
|
|
width: 60%;
|
2020-04-20 05:02:55 +02:00
|
|
|
max-width: 780px;
|
2020-04-10 04:40:22 +02:00
|
|
|
margin: 25px auto;
|
2020-06-01 04:20:03 +02:00
|
|
|
display: grid;
|
|
|
|
grid-template-columns: 240px 240px 240px;
|
|
|
|
gap: 20px 10px;
|
2020-04-10 04:40:22 +02:00
|
|
|
`;
|
2020-04-20 05:02:55 +02:00
|
|
|
|
2020-06-01 04:20:03 +02:00
|
|
|
const ProjectLink = styled(Link)``;
|
2020-04-20 05:02:55 +02:00
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
const Projects = () => {
|
2020-04-10 05:27:57 +02:00
|
|
|
const { loading, data } = useGetProjectsQuery();
|
2020-06-13 00:21:58 +02:00
|
|
|
useEffect(() => {
|
|
|
|
document.title = 'Citadel';
|
|
|
|
}, []);
|
2020-06-01 04:20:03 +02:00
|
|
|
const [createProject] = useCreateProjectMutation({
|
|
|
|
update: (client, newProject) => {
|
|
|
|
const cacheData: any = client.readQuery({
|
|
|
|
query: GetProjectsDocument,
|
|
|
|
});
|
|
|
|
|
|
|
|
console.log(cacheData);
|
|
|
|
console.log(newProject);
|
|
|
|
|
|
|
|
const newData = {
|
|
|
|
...cacheData,
|
|
|
|
projects: [...cacheData.projects, { ...newProject.data.createProject }],
|
|
|
|
};
|
|
|
|
console.log(newData);
|
|
|
|
client.writeQuery({
|
|
|
|
query: GetProjectsDocument,
|
|
|
|
data: newData,
|
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
const [showNewProject, setShowNewProject] = useState(false);
|
|
|
|
const { userID, setUserID } = useContext(UserIDContext);
|
2020-04-10 04:40:22 +02:00
|
|
|
if (loading) {
|
2020-04-10 18:31:29 +02:00
|
|
|
return (
|
|
|
|
<>
|
2020-04-20 05:02:55 +02:00
|
|
|
<span>loading</span>
|
2020-04-10 18:31:29 +02:00
|
|
|
</>
|
|
|
|
);
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
if (data) {
|
2020-06-01 04:20:03 +02:00
|
|
|
const { projects, teams } = data;
|
2020-04-10 04:40:22 +02:00
|
|
|
return (
|
2020-05-27 02:53:31 +02:00
|
|
|
<>
|
2020-05-31 07:15:10 +02:00
|
|
|
<GlobalTopNavbar onSaveProjectName={() => {}} name={null} />
|
2020-05-27 02:53:31 +02:00
|
|
|
<ProjectGrid>
|
|
|
|
{projects.map(project => (
|
|
|
|
<ProjectLink key={project.id} to={`/projects/${project.id}`}>
|
|
|
|
<ProjectGridItem
|
|
|
|
project={{ ...project, projectID: project.id, teamTitle: project.team.name, taskGroups: [] }}
|
|
|
|
/>
|
|
|
|
</ProjectLink>
|
|
|
|
))}
|
2020-06-01 04:20:03 +02:00
|
|
|
<AddProjectItem
|
|
|
|
onAddProject={() => {
|
|
|
|
setShowNewProject(true);
|
|
|
|
}}
|
|
|
|
/>
|
2020-05-27 02:53:31 +02:00
|
|
|
</ProjectGrid>
|
2020-06-01 04:20:03 +02:00
|
|
|
{showNewProject && (
|
|
|
|
<NewProject
|
|
|
|
onCreateProject={(name, teamID) => {
|
|
|
|
if (userID) {
|
|
|
|
createProject({ variables: { teamID, name, userID } });
|
|
|
|
setShowNewProject(false);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
onClose={() => {
|
|
|
|
setShowNewProject(false);
|
|
|
|
}}
|
|
|
|
teams={teams}
|
|
|
|
/>
|
|
|
|
)}
|
2020-05-27 02:53:31 +02:00
|
|
|
</>
|
2020-04-10 04:40:22 +02:00
|
|
|
);
|
|
|
|
}
|
2020-04-20 05:02:55 +02:00
|
|
|
return <div>Error!</div>;
|
2020-04-10 04:40:22 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Projects;
|