From 4cc475bf24d6c0ce4b9e7df04a4edc2b34e4200f Mon Sep 17 00:00:00 2001 From: Hazem Krimi Date: Thu, 17 Jun 2021 03:28:04 +0100 Subject: [PATCH] Update sidebar component --- src/components/Sidebar/index.tsx | 108 +++++++++++++++++++++---------- 1 file changed, 73 insertions(+), 35 deletions(-) diff --git a/src/components/Sidebar/index.tsx b/src/components/Sidebar/index.tsx index 45c5b9a..8f587de 100644 --- a/src/components/Sidebar/index.tsx +++ b/src/components/Sidebar/index.tsx @@ -2,8 +2,14 @@ import { useEffect, useState } from 'react'; import { useHistory, useLocation } from 'react-router'; import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { roleVar, userVar } from '../../graphql/state'; -import { Box, ContextMenu, IconButton, SidebarItem } from '..'; -import { Add } from '../../assets'; +import { + Box, + ContextMenu, + IconButton, + MessagingSidebar, + SidebarItem, +} from '..'; +import { Add, Messaging } from '../../assets'; import { Wrapper } from './styles'; import { CategoryOutput, @@ -12,6 +18,8 @@ import { GetAllCategoriesQueryVariables, GetAllFeaturesQuery, GetAllFeaturesQueryVariables, + GetAllProjectsByClientIdQuery, + GetAllProjectsByClientIdQueryVariables, GetAllProjectsQuery, GetAllProjectsQueryVariables, GetAllTemplatesQuery, @@ -20,7 +28,10 @@ import { TemplateOutput, } from '../../graphql/types'; import { GET_ALL_CATEGORIES } from '../../graphql/category.api'; -import { GET_ALL_PROJECTS } from '../../graphql/project.api'; +import { + GET_ALL_PROJECTS, + GET_ALL_PROJECTS_BY_CLIENT_ID, +} from '../../graphql/project.api'; import { GET_ALL_TEMPLATES } from '../../graphql/template.api'; import { GET_ALL_FEATURES } from '../../graphql/feature.api'; @@ -33,19 +44,32 @@ const Sidebar = () => { const [templates, setTemplates] = useState>(); const [features, setFeatures] = useState>(); const [categories, setCategories] = useState>(); + const [messagingSidebarOpen, setMessagingSidebarOpen] = useState( + false + ); + + const [ + getProjectsByClientId, + { loading: clientProjectsLoading }, + ] = useLazyQuery< + GetAllProjectsByClientIdQuery, + GetAllProjectsByClientIdQueryVariables + >(GET_ALL_PROJECTS_BY_CLIENT_ID, { + variables: { + id: currentUser?.id!, + }, + onCompleted({ getAllProjectsByClientId }) { + setProjects(getAllProjectsByClientId); + }, + fetchPolicy: 'network-only', + }); const [getProjects] = useLazyQuery< GetAllProjectsQuery, GetAllProjectsQueryVariables >(GET_ALL_PROJECTS, { onCompleted({ getAllProjects }) { - setProjects( - role === 'client' - ? getAllProjects.filter( - (project) => project.clientId === currentUser?.id - ) - : getAllProjects - ); + setProjects(getAllProjects); }, fetchPolicy: 'network-only', }); @@ -82,7 +106,8 @@ const Sidebar = () => { useEffect(() => { if (/project/i.test(location.pathname)) { - getProjects(); + if (role !== 'client') getProjects(); + else getProjectsByClientId({ variables: { id: currentUser?.id! } }); } if (/template/i.test(location.pathname)) { @@ -112,15 +137,14 @@ const Sidebar = () => { <> {projects && - projects.map((project, index) => ( + projects.map((project) => (
history.push(`/project/${project.id}`)} /> @@ -192,28 +216,42 @@ const Sidebar = () => { ))} - - } - color={role} - onClick={() => { - if (/project/i.test(location.pathname)) { - history.push('/add-project'); - } - if (/template/i.test(location.pathname)) { - history.push('/add-template'); - } - if (/feature/i.test(location.pathname)) { - history.push('/add-feature'); - } - if (/category/i.test(location.pathname)) { - history.push('/add-category'); - } - }} - /> + + + } + color={role} + onClick={() => { + if (/project/i.test(location.pathname)) { + history.push('/add-project'); + } + if (/template/i.test(location.pathname)) { + history.push('/add-template'); + } + if (/feature/i.test(location.pathname)) { + history.push('/add-feature'); + } + if (/category/i.test(location.pathname)) { + history.push('/add-category'); + } + }} + /> + + {/\/project/i.test(location.pathname) && ( + + } + color={role} + onClick={() => setMessagingSidebarOpen(!messagingSidebarOpen)} + /> + + )} )} + {messagingSidebarOpen && ( + setMessagingSidebarOpen(false)} /> + )} ); };