import { useEffect, useState } from 'react'; import { useHistory, useLocation } from 'react-router'; import { useLazyQuery, useReactiveVar } from '@apollo/client'; import { roleVar } from '../../graphql/state'; 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 ( {projects && projects.map((project) => (
history.push(`/project/${project.id}`)} />
{}, label: project.name }]} />
))} {templates && templates.map((template) => (
history.push(`/template/${template.id}`)} />
{}, label: template.name }]} />
))} {features && features.map((feature) => (
history.push(`/feature/${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'); } }} /> )}
); }; export default Sidebar;