From 8aeb86ef0063f730a6c5eddf972b5e08c9a1ebfc Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Sat, 22 Oct 2022 18:19:39 +0800 Subject: [PATCH] [questions][ui] add clear filters button --- .../questions/filter/FilterSection.tsx | 19 +++++++- apps/portal/src/pages/questions/browse.tsx | 48 +++++++++++++++++-- 2 files changed, 60 insertions(+), 7 deletions(-) diff --git a/apps/portal/src/components/questions/filter/FilterSection.tsx b/apps/portal/src/components/questions/filter/FilterSection.tsx index 7f22a2a9..b72bc7ff 100644 --- a/apps/portal/src/components/questions/filter/FilterSection.tsx +++ b/apps/portal/src/components/questions/filter/FilterSection.tsx @@ -79,9 +79,24 @@ export default function FilterSection< return options.filter((option) => !option.checked) as FilterOptions; }, [options]); + const selectedCount = useMemo(() => { + return options.filter((option) => option.checked).length; + }, [options]); + + const collapsibleLabel = useMemo(() => { + if (isSingleSelect) { + return label; + } + if (selectedCount === 0) { + return `${label} (all)`; + } + + return `${label} (${selectedCount})`; + }, [label, selectedCount, isSingleSelect]); + return ( -
- +
+
{!showAll && (
diff --git a/apps/portal/src/pages/questions/browse.tsx b/apps/portal/src/pages/questions/browse.tsx index c6e59ccf..89ca2954 100644 --- a/apps/portal/src/pages/questions/browse.tsx +++ b/apps/portal/src/pages/questions/browse.tsx @@ -2,9 +2,10 @@ import { subMonths, subYears } from 'date-fns'; import Head from 'next/head'; import Router, { useRouter } from 'next/router'; import { useEffect, useMemo, useState } from 'react'; +import { Bars3BottomLeftIcon } from '@heroicons/react/20/solid'; import { NoSymbolIcon } from '@heroicons/react/24/outline'; import type { QuestionsQuestionType } from '@prisma/client'; -import { SlideOut, Typeahead } from '@tih/ui'; +import { Button, SlideOut, Typeahead } from '@tih/ui'; import QuestionOverviewCard from '~/components/questions/card/question/QuestionOverviewCard'; import ContributeQuestionCard from '~/components/questions/ContributeQuestionCard'; @@ -69,6 +70,22 @@ export default function QuestionsBrowsePage() { const [selectedLocations, setSelectedLocations, areLocationsInitialized] = useSearchFilter('locations'); + const hasFilters = useMemo( + () => + selectedCompanies.length > 0 || + selectedQuestionTypes.length > 0 || + selectedQuestionAge !== 'all' || + selectedRoles.length > 0 || + selectedLocations.length > 0, + [ + selectedCompanies, + selectedQuestionTypes, + selectedQuestionAge, + selectedRoles, + selectedLocations, + ], + ); + const today = useMemo(() => new Date(), []); const startDate = useMemo(() => { return selectedQuestionAge === 'last-year' @@ -197,7 +214,22 @@ export default function QuestionsBrowsePage() { return null; } const filterSidebar = ( -
+
+