From 3ee656f73cf4fefa749513e5d937138adbc99e9a Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 4 Jun 2021 16:35:36 +0100 Subject: [PATCH] Update template page --- src/pages/Template/index.tsx | 58 +++++++++++++++++++++++++++++------- 1 file changed, 47 insertions(+), 11 deletions(-) diff --git a/src/pages/Template/index.tsx b/src/pages/Template/index.tsx index 5e2163f..b457311 100644 --- a/src/pages/Template/index.tsx +++ b/src/pages/Template/index.tsx @@ -1,4 +1,5 @@ -import { useEffect, useState } from 'react'; +import { useReactToPrint } from 'react-to-print'; +import { useEffect, useState, useRef } from 'react'; import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { Redirect } from 'react-router'; import { useHistory, useParams } from 'react-router-dom'; @@ -11,11 +12,16 @@ import { Text, Spinner, Link, + Specification as SpecificationPrint, + Chip, } from '../../components'; import { Wrapper } from './styles'; import { + CategoryOutput, GetAllTemplatesQuery, GetAllTemplatesQueryVariables, + GetCategoryByIdQuery, + GetCategoryByIdQueryVariables, GetTemplateByIdQuery, GetTemplateByIdQueryVariables, TemplateOutput, @@ -24,12 +30,25 @@ import { GET_ALL_TEMPLATES, GET_TEMPLATE_BY_ID, } from '../../graphql/template.api'; +import { GET_CATEGORY_BY_ID } from '../../graphql/category.api'; const Template = () => { const role = useReactiveVar(roleVar); const history = useHistory(); + const printRef = useRef(null); const { id } = useParams<{ id: string }>(); const [template, setTemplate] = useState(); + const [category, setCategory] = useState(); + + const [getCategory, { loading: categoryLoading }] = useLazyQuery< + GetCategoryByIdQuery, + GetCategoryByIdQueryVariables + >(GET_CATEGORY_BY_ID, { + onCompleted({ getCategoryById }) { + setCategory(getCategoryById); + }, + fetchPolicy: 'network-only', + }); const [getTemplates, { loading: templatesLoading }] = useLazyQuery< GetAllTemplatesQuery, @@ -37,6 +56,7 @@ const Template = () => { >(GET_ALL_TEMPLATES, { onCompleted({ getAllTemplates }) { setTemplate(getAllTemplates[0]); + getCategory({ variables: { id: template?.category! } }); }, fetchPolicy: 'network-only', }); @@ -47,10 +67,15 @@ const Template = () => { >(GET_TEMPLATE_BY_ID, { onCompleted({ getTemplateById }) { setTemplate(getTemplateById); + getCategory({ variables: { id: template?.category! } }); }, fetchPolicy: 'network-only', }); + const handlePrint = useReactToPrint({ + content: () => printRef.current, + }); + useEffect(() => { if (id) { getTemplate({ variables: { id } }); @@ -63,9 +88,9 @@ const Template = () => { return role === 'productOwner' || role === 'developer' ? ( <> - {!templatesLoading && !templateLoading ? ( + {!templatesLoading && !templateLoading && !categoryLoading ? ( <> - {template ? ( + {template && category ? ( { alignItems='center' marginBottom='20px' > - + {template.name} + + + { Specification - {}}> + Download )} - + {template.specification && template.features && ( + + + + )} ) : (