taskcafe/web/src/Projects/index.tsx

100 lines
2.7 KiB
TypeScript
Raw Normal View History

import React, { useState, useContext } 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';
import { useGetProjectsQuery, useCreateProjectMutation, GetProjectsDocument } from 'shared/generated/graphql';
2020-04-10 04:40:22 +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';
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;
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
const ProjectLink = styled(Link)``;
2020-04-20 05:02:55 +02:00
2020-04-10 04:40:22 +02:00
const Projects = () => {
const { loading, data } = useGetProjectsQuery();
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) {
const { projects, teams } = data;
2020-04-10 04:40:22 +02:00
return (
2020-05-27 02:53:31 +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>
))}
<AddProjectItem
onAddProject={() => {
setShowNewProject(true);
}}
/>
2020-05-27 02:53:31 +02:00
</ProjectGrid>
{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;