diff --git a/src/pages/Category/index.tsx b/src/pages/Category/index.tsx index fa80190..9e30e19 100644 --- a/src/pages/Category/index.tsx +++ b/src/pages/Category/index.tsx @@ -1,27 +1,110 @@ -import { useReactiveVar } from '@apollo/client'; -import { Redirect } from 'react-router'; +import { useEffect, useState } from 'react'; +import { useLazyQuery, useReactiveVar } from '@apollo/client'; +import { Redirect, useHistory, useParams } from 'react-router'; import { roleVar } from '../../graphql/state'; -import { Empty } from '../../assets'; -import { Box } from '../../components'; +import { Empty, Settings } from '../../assets'; +import { Box, Button, Spinner, Text } from '../../components'; import { Wrapper } from './styles'; +import { + CategoryOutput, + GetAllCategoriesQuery, + GetAllCategoriesQueryVariables, + GetCategoryByIdQuery, + GetCategoryByIdQueryVariables, +} from '../../graphql/types'; +import { + GET_ALL_CATEGORIES, + GET_CATEGORY_BY_ID, +} from '../../graphql/category.api'; const Category = () => { const role = useReactiveVar(roleVar); + const history = useHistory(); + const { id } = useParams<{ id: string }>(); + const [category, setCategory] = useState(); + + const [getCategories, { loading: categoriesLoading }] = useLazyQuery< + GetAllCategoriesQuery, + GetAllCategoriesQueryVariables + >(GET_ALL_CATEGORIES, { + onCompleted({ getAllCategories }) { + setCategory(getAllCategories[0]); + }, + fetchPolicy: 'network-only', + }); + + const [getCategory, { loading: categoryLoading }] = useLazyQuery< + GetCategoryByIdQuery, + GetCategoryByIdQueryVariables + >(GET_CATEGORY_BY_ID, { + onCompleted({ getCategoryById }) { + setCategory(getCategoryById); + }, + fetchPolicy: 'network-only', + }); + + useEffect(() => { + if (id) { + getCategory({ variables: { id } }); + } else { + getCategories(); + } + + // eslint-disable-next-line + }, [id]); return role === 'developer' ? ( - - - - - - - + <> + {!categoriesLoading && !categoryLoading ? ( + <> + {category ? ( + + + + + + {category.name} + + +