From 6f2aaebdb0a967ea34eae37a2c36cc1b780cc51c Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Fri, 21 May 2021 22:42:02 +0100 Subject: [PATCH] Update sidebar component --- src/components/Sidebar/index.tsx | 187 ++++++++++++++++++++++++++++++- src/components/Sidebar/styles.ts | 2 +- 2 files changed, 183 insertions(+), 6 deletions(-) diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index cfcbd15..d1f436a 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -1,17 +1,194 @@ -import { useReactiveVar } from '@apollo/client'; +import { useEffect, useState } from 'react'; +import { useHistory, useLocation } from 'react-router'; +import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { roleVar } from '../../graphql/state'; -import { IconButton } from '..'; +import { Box, ContextMenu, IconButton, SidebarItem } from '..'; import { Add } from '../../assets'; import { Wrapper } from './styles'; +import { + CategoryOutput, + FeatureOutput, + GetAllCategoriesQuery, + GetAllCategoriesQueryVariables, + GetAllFeaturesQuery, + GetAllFeaturesQueryVariables, + GetAllProjectsQuery, + GetAllProjectsQueryVariables, + GetAllTemplatesQuery, + GetAllTemplatesQueryVariables, + ProjectOutput, + TemplateOutput, +} from '../../graphql/types'; +import { GET_ALL_CATEGORIES } from '../../graphql/category.api'; +import { GET_ALL_PROJECTS } from '../../graphql/project.api'; +import { GET_ALL_TEMPLATES } from '../../graphql/template.api'; +import { GET_ALL_FEATURES } from '../../graphql/feature.api'; const Sidebar = () => { const role = useReactiveVar(roleVar); + const location = useLocation(); + const history = useHistory(); + const [projects, setProjects] = useState>(); + const [templates, setTemplates] = useState>(); + const [features, setFeatures] = useState>(); + const [categories, setCategories] = useState>(); + + const [getProjects] = useLazyQuery< + GetAllProjectsQuery, + GetAllProjectsQueryVariables + >(GET_ALL_PROJECTS, { + onCompleted({ getAllProjects }) { + setProjects(getAllProjects); + }, + fetchPolicy: 'network-only', + }); + + const [getTemplates] = useLazyQuery< + GetAllTemplatesQuery, + GetAllTemplatesQueryVariables + >(GET_ALL_TEMPLATES, { + onCompleted({ getAllTemplates }) { + setTemplates(getAllTemplates); + }, + fetchPolicy: 'network-only', + }); + + const [getFeatures] = useLazyQuery< + GetAllFeaturesQuery, + GetAllFeaturesQueryVariables + >(GET_ALL_FEATURES, { + onCompleted({ getAllFeatures }) { + setFeatures(getAllFeatures); + }, + fetchPolicy: 'network-only', + }); + + const [getCategories] = useLazyQuery< + GetAllCategoriesQuery, + GetAllCategoriesQueryVariables + >(GET_ALL_CATEGORIES, { + onCompleted({ getAllCategories }) { + setCategories(getAllCategories); + }, + fetchPolicy: 'network-only', + }); + + useEffect(() => { + if (/project/.test(location.pathname)) { + getProjects(); + } + + if (/template/.test(location.pathname)) { + getTemplates(); + } + + if (/feature/.test(location.pathname)) { + getFeatures(); + } + if (/category/.test(location.pathname)) { + getCategories(); + } + + return () => { + setProjects([]); + setTemplates([]); + setFeatures([]); + setCategories([]); + }; + + // eslint-disable-next-line + }, [location.pathname]); return ( - {role !== 'admin' && ( - } color={role} onClick={() => {}} /> - )} + + {projects && + projects.map((project) => ( + +
+ history.push(`/category/${project.id}`)} + /> +
+ {}, label: project.name }]} + /> +
+ ))} + {templates && + templates.map((template) => ( + +
+ history.push(`/template/${template.id}`)} + /> +
+ {}, label: template.name }]} + /> +
+ ))} + {features && + features.map((feature) => ( + +
+ history.push(`/category/${feature.id}`)} + /> +
+ {}, label: feature.name }]} + /> +
+ ))} + {categories && + categories.map((category) => ( + +
+ history.push(`/category/${category.id}`)} + /> +
+ {}, label: category.name }]} + /> +
+ ))} +
+ + {role !== 'admin' && ( + } + color={role} + onClick={() => { + if (/project/.test(location.pathname)) { + history.push('/add-project'); + } + if (/template/.test(location.pathname)) { + history.push('/add-template'); + } + if (/feature/.test(location.pathname)) { + history.push('/add-feature'); + } + if (/category/.test(location.pathname)) { + history.push('/add-category'); + } + }} + /> + )} +
); }; diff --git a/src/components/Sidebar/styles.ts b/src/components/Sidebar/styles.ts index 001e788..5f4d7c0 100644 --- a/src/components/Sidebar/styles.ts +++ b/src/components/Sidebar/styles.ts @@ -14,7 +14,7 @@ export const Wrapper = styled.div` background: ${({ theme, color }) => color ? theme.colors[color].light : theme.colors.client.light}; display: grid; + grid-template-rows: 1fr auto; justify-content: center; - align-items: flex-end; padding: 55px 0px; `;