From 2e4a042ec6c5149a40e220466920df1eac9e46ea Mon Sep 17 00:00:00 2001 From: MTG2000 Date: Tue, 8 Nov 2022 14:53:19 +0200 Subject: [PATCH] chore (explore page): clean up code --- .../pages/ExplorePage/ExplorePage.tsx | 123 ++++++++---------- .../pages/ExplorePage/Header/Header.tsx | 8 -- src/utils/apollo.ts | 4 - 3 files changed, 55 insertions(+), 80 deletions(-) diff --git a/src/features/Projects/pages/ExplorePage/ExplorePage.tsx b/src/features/Projects/pages/ExplorePage/ExplorePage.tsx index 74da77c..92e9410 100644 --- a/src/features/Projects/pages/ExplorePage/ExplorePage.tsx +++ b/src/features/Projects/pages/ExplorePage/ExplorePage.tsx @@ -19,67 +19,16 @@ import { useUpdateUrlWithFilters } from './helpers'; import { withBasicProvider } from 'src/utils/helperFunctions'; import { ProjectsFiltersProvider, useProjectsFilters } from './filters-context'; -const UPDATE_FILTERS_ACTION = createAction>('PROJECTS_FILTERS_UPDATED')({}) - - -type QueryFilter = Partial<{ - categoryId: string[] | null - tags: string[] | null - yearFounded: number | null - dead: boolean | null - license: string | null -}> - -const PAGE_SIZE = 28; function ExplorePage() { const dispatch = useAppDispatch(); - const { filters, updateFilters } = useProjectsFilters(); - const projectsLength = useRef(0); const [canFetchMore, setCanFetchMore] = useState(true); + const { filters, updateFilters } = useProjectsFilters(); useUpdateUrlWithFilters(filters) - - - const { queryFilters, hasSearchFilters } = useMemo(() => { - let filter: QueryFilter = {} - let hasSearchFilters = false; - - - if (filters?.categories) { - filter.categoryId = filters?.categories.map(c => c.id!); - hasSearchFilters = true; - } - - - if (filters?.tags) { - filter.tags = filters?.tags.map(t => t.id) - hasSearchFilters = true; - } - - if (filters?.yearFounded) { - filter.yearFounded = Number(filters?.yearFounded) - hasSearchFilters = true; - } - - if (filters?.projectStatus) { - filter.dead = filters?.projectStatus === 'alive' ? false : true; - hasSearchFilters = true; - } - - - if (filters?.projectLicense) { - filter.license = filters?.projectLicense - hasSearchFilters = true; - } - - if (Object.keys(filter).length === 0) - return { queryFilters: null, hasSearchFilters } - - return { queryFilters: filter, hasSearchFilters }; - }, [filters]) + const { queryFilters, hasSearchFilters } = useMemo(() => createQueryFilters(filters), [filters]) const { data, networkStatus, error, fetchMore } = useExplorePageQuery({ variables: { @@ -94,9 +43,6 @@ function ExplorePage() { }); - projectsLength.current = data?.projects?.length ?? 0; - - const onFiltersUpdated = useCallback(({ payload }: typeof UPDATE_FILTERS_ACTION) => { updateFilters(payload) }, [updateFilters]) @@ -171,17 +117,6 @@ function ExplorePage() { {hasSearchFilters && {Object.keys(filters ?? {}).length}} - {/*
- -
*/} >('PROJECTS_FILTERS_UPDATED')({}) + +const PAGE_SIZE = 28; + +type QueryFilter = Partial<{ + categoryId: string[] | null + tags: string[] | null + yearFounded: number | null + dead: boolean | null + license: string | null +}> + + +const createQueryFilters = (filters: Partial | null) => { + let filter: QueryFilter = {} + let hasSearchFilters = false; + + + if (filters?.categories) { + filter.categoryId = filters?.categories.map(c => c.id!); + hasSearchFilters = true; + } + + + if (filters?.tags) { + filter.tags = filters?.tags.map(t => t.id) + hasSearchFilters = true; + } + + if (filters?.yearFounded) { + filter.yearFounded = Number(filters?.yearFounded) + hasSearchFilters = true; + } + + if (filters?.projectStatus) { + filter.dead = filters?.projectStatus === 'alive' ? false : true; + hasSearchFilters = true; + } + + + if (filters?.projectLicense) { + filter.license = filters?.projectLicense + hasSearchFilters = true; + } + + if (Object.keys(filter).length === 0) + return { queryFilters: null, hasSearchFilters } + + return { queryFilters: filter, hasSearchFilters }; +} \ No newline at end of file diff --git a/src/features/Projects/pages/ExplorePage/Header/Header.tsx b/src/features/Projects/pages/ExplorePage/Header/Header.tsx index 60f6b98..85f5620 100644 --- a/src/features/Projects/pages/ExplorePage/Header/Header.tsx +++ b/src/features/Projects/pages/ExplorePage/Header/Header.tsx @@ -30,14 +30,6 @@ export default function Header(props: Props) { return (
- {/*
*/} - {/* - */}