From 84ee65e8f8c943d0fd4307d66f595f845eced7dc Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Thu, 17 Mar 2022 11:54:49 +0200 Subject: [PATCH] fix: added loading state to categories drop-down, removed logs, improved search query typing --- .../Navbar/CategoriesList/CategoriesList.tsx | 23 +++++++++++++++---- src/Components/Navbar/CategoriesList/query.ts | 22 ++++++++++++++++++ src/Components/Navbar/Search/Search.tsx | 1 - .../SearchProjectCard/SearchProjectCard.tsx | 5 ++-- .../Search/SearchResults/SearchResults.tsx | 3 ++- src/Components/Navbar/Search/query.ts | 20 +++++++++++++--- src/mocks/handlers.ts | 1 - src/pages/HottestPage/HottestPage.tsx | 1 - 8 files changed, 62 insertions(+), 14 deletions(-) create mode 100644 src/Components/Navbar/CategoriesList/query.ts diff --git a/src/Components/Navbar/CategoriesList/CategoriesList.tsx b/src/Components/Navbar/CategoriesList/CategoriesList.tsx index 1923c92..d91d5d6 100644 --- a/src/Components/Navbar/CategoriesList/CategoriesList.tsx +++ b/src/Components/Navbar/CategoriesList/CategoriesList.tsx @@ -1,8 +1,8 @@ -import React from 'react' +import { useQuery } from '@apollo/client' +import Skeleton from 'react-loading-skeleton' import { Link } from 'react-router-dom' -import { MOCK_DATA } from 'src/mocks/data' import { numberFormatter } from 'src/utils/helperFunctions' -import { ProjectCategory } from 'src/utils/interfaces' +import { ALL_CATEGORIES_QUERY, ALL_CATEGORIES_QUERY_RES } from './query' interface Props { // categories: Pick[] @@ -10,12 +10,25 @@ interface Props { onClick?: (categoryId: number) => void } -const categories = MOCK_DATA['categories'] export default function CategoriesList({ classes = {}, onClick }: Props) { + + + const { data, loading } = useQuery(ALL_CATEGORIES_QUERY); + + + if (loading) + return
    + {Array(5).fill(0).map((_, idx) => +
  • + +
  • + )} +
+ return (
    - {categories.map(category => + {data?.allCategories.map(category => onClick?.(category.id)} key={category.id} diff --git a/src/Components/Navbar/CategoriesList/query.ts b/src/Components/Navbar/CategoriesList/query.ts new file mode 100644 index 0000000..66c7982 --- /dev/null +++ b/src/Components/Navbar/CategoriesList/query.ts @@ -0,0 +1,22 @@ +import { gql } from "@apollo/client"; +import { ProjectCategory } from "src/utils/interfaces"; + +export const ALL_CATEGORIES_QUERY = gql` + query AllCategories { + allCategories { + id + title + icon + votes_sum + } + } +`; + +export type ALL_CATEGORIES_QUERY_RES = { + allCategories: Pick[]; +}; diff --git a/src/Components/Navbar/Search/Search.tsx b/src/Components/Navbar/Search/Search.tsx index b4380fe..0645793 100644 --- a/src/Components/Navbar/Search/Search.tsx +++ b/src/Components/Navbar/Search/Search.tsx @@ -117,7 +117,6 @@ export default function Search({ value={searchInput} onChange={handleChange} onKeyDown={e => { - console.log(e.key); if (e.key === 'Escape') dispatch(toggleSearch(false)) }} diff --git a/src/Components/Navbar/Search/SearchProjectCard/SearchProjectCard.tsx b/src/Components/Navbar/Search/SearchProjectCard/SearchProjectCard.tsx index b96f406..432a376 100644 --- a/src/Components/Navbar/Search/SearchProjectCard/SearchProjectCard.tsx +++ b/src/Components/Navbar/Search/SearchProjectCard/SearchProjectCard.tsx @@ -1,4 +1,5 @@ -import { ProjectCard } from 'src/utils/interfaces' + +import { ProjectSearchItem } from '../query' import SearchProjectCardSkeleton from './SearchProjectCard.Skeleton' type Props = @@ -8,7 +9,7 @@ type Props = | { loading?: false - project: ProjectCard + project: ProjectSearchItem onClick: (projectId: number) => void; } diff --git a/src/Components/Navbar/Search/SearchResults/SearchResults.tsx b/src/Components/Navbar/Search/SearchResults/SearchResults.tsx index 600665b..5137134 100644 --- a/src/Components/Navbar/Search/SearchResults/SearchResults.tsx +++ b/src/Components/Navbar/Search/SearchResults/SearchResults.tsx @@ -3,12 +3,13 @@ import { openModal } from 'src/redux/features/modals.slice'; import { toggleSearch } from 'src/redux/features/ui.slice'; import { useAppDispatch } from 'src/utils/hooks'; import { ProjectCard } from 'src/utils/interfaces' +import { ProjectSearchItem } from '../query'; import SearchProjectCard from '../SearchProjectCard/SearchProjectCard'; import styles from './styles.module.css' interface Props { isLoading?: boolean; - projects: ProjectCard[] | undefined, + projects: ProjectSearchItem[] | undefined, } export default function SearchResults({ projects, isLoading }: Props) { diff --git a/src/Components/Navbar/Search/query.ts b/src/Components/Navbar/Search/query.ts index 33679c0..53d5dfb 100644 --- a/src/Components/Navbar/Search/query.ts +++ b/src/Components/Navbar/Search/query.ts @@ -1,5 +1,5 @@ import { gql } from "@apollo/client"; -import { ProjectCard } from "src/utils/interfaces"; +import { ProjectCard, ProjectCategory } from "src/utils/interfaces"; export const SEARCH_PROJECTS_QUERY = gql` query SEARCH_PROJECTS_QUERY($search: String!) { @@ -17,9 +17,23 @@ query SEARCH_PROJECTS_QUERY($search: String!) { } ` export type SEARCH_PROJECTS_QUERY_VARS = { - search: string; + search: string; } +export type ProjectSearchItem = + ( + Pick + & + { + category: Pick + } + ) + export type SEARCH_PROJECTS_QUERY_RES_TYPE = { - searchProjects: ProjectCard[], + searchProjects: ProjectSearchItem[], } \ No newline at end of file diff --git a/src/mocks/handlers.ts b/src/mocks/handlers.ts index 9836212..f3a6c02 100644 --- a/src/mocks/handlers.ts +++ b/src/mocks/handlers.ts @@ -62,7 +62,6 @@ export const handlers = [ graphql.query('HOTTEST_PROJECTS', async (req, res, ctx) => { await delay() - console.log(hottestProjects()); return res( ctx.data({ diff --git a/src/pages/HottestPage/HottestPage.tsx b/src/pages/HottestPage/HottestPage.tsx index 336cc9f..fca6f72 100644 --- a/src/pages/HottestPage/HottestPage.tsx +++ b/src/pages/HottestPage/HottestPage.tsx @@ -19,7 +19,6 @@ export default function HottestPage() { } - console.log(data); return (