From befb9e35242c108ffd31757312aaa2ff5d289ac2 Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Tue, 15 Jun 2021 02:30:20 +0100 Subject: [PATCH] Update project page --- src/pages/Project/index.tsx | 252 +++++++++++++++++++++++++++++++++--- 1 file changed, 236 insertions(+), 16 deletions(-) diff --git a/src/pages/Project/index.tsx b/src/pages/Project/index.tsx index 1de9f37..d97aa69 100644 --- a/src/pages/Project/index.tsx +++ b/src/pages/Project/index.tsx @@ -1,27 +1,247 @@ -import { useReactiveVar } from '@apollo/client'; +import { useReactToPrint } from 'react-to-print'; +import { useHistory, useParams } from 'react-router-dom'; +import { useEffect, useState, useRef } from 'react'; +import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { Redirect } from 'react-router'; import { roleVar } from '../../graphql/state'; -import { Empty } from '../../assets'; -import { Box } from '../../components'; +import { Design, Empty, Settings, Specification } from '../../assets'; +import { + Box, + Button, + FeatureCard, + Text, + Spinner, + Link, + Specification as SpecificationPrint, + Chip, +} from '../../components'; import { Wrapper } from './styles'; +import { + GetAllProjectsQuery, + GetAllProjectsQueryVariables, + GetProjectByIdQuery, + GetProjectByIdQueryVariables, + GetPrototypeByIdQuery, + GetPrototypeByIdQueryVariables, + ProjectOutput, + ProtoTypeOutput, +} from '../../graphql/types'; +import { GET_ALL_PROJECTS, GET_PROJECT_BY_ID } from '../../graphql/project.api'; +import { GET_PROTOTYPE_BY_ID } from '../../graphql/prototype.api'; const Project = () => { const role = useReactiveVar(roleVar); + const history = useHistory(); + const printRef = useRef(null); + const { id } = useParams<{ id: string }>(); + const [project, setProject] = useState(); + const [prototype, setPrototype] = useState>(); + + const [getProjects, { loading: projectsLoading }] = useLazyQuery< + GetAllProjectsQuery, + GetAllProjectsQueryVariables + >(GET_ALL_PROJECTS, { + onCompleted({ getAllProjects }) { + setProject(getAllProjects[0]); + }, + fetchPolicy: 'network-only', + }); + + const [getProject, { loading: projectLoading }] = useLazyQuery< + GetProjectByIdQuery, + GetProjectByIdQueryVariables + >(GET_PROJECT_BY_ID, { + onCompleted({ getProjectById }) { + setProject(getProjectById); + }, + fetchPolicy: 'network-only', + }); + + const [getPrototype, { loading: prototypeLoading }] = useLazyQuery< + GetPrototypeByIdQuery, + GetPrototypeByIdQueryVariables + >(GET_PROTOTYPE_BY_ID, { + onCompleted({ getPrototypeById }) { + setPrototype(getPrototypeById.prototype); + }, + }); + + const handlePrint = useReactToPrint({ + content: () => printRef.current, + }); + + useEffect(() => { + if (id) { + getProject({ variables: { id } }); + } else { + getProjects(); + } + + // eslint-disable-next-line + }, [id]); + + useEffect(() => { + if (project) getPrototype({ variables: { id: project?.template?.id } }); + + // eslint-disable-next-line + }, [project]); return role !== 'admin' ? ( - - - - - - - + <> + {!projectLoading && !projectLoading && !prototypeLoading ? ( + <> + {project ? ( + + + + + + {project.name} + + + {project.state === 'Approved' ? ( + <> + +