Update template page

This commit is contained in:
Hazem Krimi
2021-06-04 16:35:36 +01:00
parent 25f466a972
commit 3ee656f73c
+47 -11
View File
@@ -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 { useLazyQuery, useReactiveVar } from '@apollo/client';
import { Redirect } from 'react-router'; import { Redirect } from 'react-router';
import { useHistory, useParams } from 'react-router-dom'; import { useHistory, useParams } from 'react-router-dom';
@@ -11,11 +12,16 @@ import {
Text, Text,
Spinner, Spinner,
Link, Link,
Specification as SpecificationPrint,
Chip,
} from '../../components'; } from '../../components';
import { Wrapper } from './styles'; import { Wrapper } from './styles';
import { import {
CategoryOutput,
GetAllTemplatesQuery, GetAllTemplatesQuery,
GetAllTemplatesQueryVariables, GetAllTemplatesQueryVariables,
GetCategoryByIdQuery,
GetCategoryByIdQueryVariables,
GetTemplateByIdQuery, GetTemplateByIdQuery,
GetTemplateByIdQueryVariables, GetTemplateByIdQueryVariables,
TemplateOutput, TemplateOutput,
@@ -24,12 +30,25 @@ import {
GET_ALL_TEMPLATES, GET_ALL_TEMPLATES,
GET_TEMPLATE_BY_ID, GET_TEMPLATE_BY_ID,
} from '../../graphql/template.api'; } from '../../graphql/template.api';
import { GET_CATEGORY_BY_ID } from '../../graphql/category.api';
const Template = () => { const Template = () => {
const role = useReactiveVar(roleVar); const role = useReactiveVar(roleVar);
const history = useHistory(); const history = useHistory();
const printRef = useRef<HTMLDivElement>(null);
const { id } = useParams<{ id: string }>(); const { id } = useParams<{ id: string }>();
const [template, setTemplate] = useState<TemplateOutput>(); const [template, setTemplate] = useState<TemplateOutput>();
const [category, setCategory] = useState<CategoryOutput>();
const [getCategory, { loading: categoryLoading }] = useLazyQuery<
GetCategoryByIdQuery,
GetCategoryByIdQueryVariables
>(GET_CATEGORY_BY_ID, {
onCompleted({ getCategoryById }) {
setCategory(getCategoryById);
},
fetchPolicy: 'network-only',
});
const [getTemplates, { loading: templatesLoading }] = useLazyQuery< const [getTemplates, { loading: templatesLoading }] = useLazyQuery<
GetAllTemplatesQuery, GetAllTemplatesQuery,
@@ -37,6 +56,7 @@ const Template = () => {
>(GET_ALL_TEMPLATES, { >(GET_ALL_TEMPLATES, {
onCompleted({ getAllTemplates }) { onCompleted({ getAllTemplates }) {
setTemplate(getAllTemplates[0]); setTemplate(getAllTemplates[0]);
getCategory({ variables: { id: template?.category! } });
}, },
fetchPolicy: 'network-only', fetchPolicy: 'network-only',
}); });
@@ -47,10 +67,15 @@ const Template = () => {
>(GET_TEMPLATE_BY_ID, { >(GET_TEMPLATE_BY_ID, {
onCompleted({ getTemplateById }) { onCompleted({ getTemplateById }) {
setTemplate(getTemplateById); setTemplate(getTemplateById);
getCategory({ variables: { id: template?.category! } });
}, },
fetchPolicy: 'network-only', fetchPolicy: 'network-only',
}); });
const handlePrint = useReactToPrint({
content: () => printRef.current,
});
useEffect(() => { useEffect(() => {
if (id) { if (id) {
getTemplate({ variables: { id } }); getTemplate({ variables: { id } });
@@ -63,9 +88,9 @@ const Template = () => {
return role === 'productOwner' || role === 'developer' ? ( return role === 'productOwner' || role === 'developer' ? (
<> <>
{!templatesLoading && !templateLoading ? ( {!templatesLoading && !templateLoading && !categoryLoading ? (
<> <>
{template ? ( {template && category ? (
<Wrapper> <Wrapper>
<Box padding='35px 45px 0px 120px'> <Box padding='35px 45px 0px 120px'>
<Box <Box
@@ -74,10 +99,18 @@ const Template = () => {
alignItems='center' alignItems='center'
marginBottom='20px' marginBottom='20px'
> >
<Box flexGrow='1'> <Box
flexGrow='1'
display='flex'
flexDirection='row'
alignItems='center'
>
<Text variant='headline' weight='bold'> <Text variant='headline' weight='bold'>
{template.name} {template.name}
</Text> </Text>
<Box marginLeft='20px'>
<Chip text={category?.name} color={role} />
</Box>
</Box> </Box>
<Box <Box
marginRight={role === 'productOwner' ? '20px' : undefined} marginRight={role === 'productOwner' ? '20px' : undefined}
@@ -163,18 +196,21 @@ const Template = () => {
</Box> </Box>
<Text variant='title'>Specification</Text> <Text variant='title'>Specification</Text>
</Box> </Box>
<Link href='#' color={role} onClick={() => {}}> <Link href='#' color={role} onClick={handlePrint}>
Download Download
</Link> </Link>
</Box> </Box>
</Box> </Box>
)} )}
<Box {template.specification && template.features && (
display='flex' <Box display='none'>
flexDirection='row' <SpecificationPrint
alignItems='center' ref={printRef}
marginBottom='30px' specification={template.specification}
></Box> features={template.features}
/>
</Box>
)}
</Box> </Box>
</Wrapper> </Wrapper>
) : ( ) : (