From 2e7131583cd023d91bb316b265594808dd4c81e5 Mon Sep 17 00:00:00 2001 From: Jeff Sieu Date: Sat, 22 Oct 2022 20:02:44 +0800 Subject: [PATCH] [questions][feat] browse: add sort by, order by --- .../questions/QuestionSearchBar.tsx | 107 ++++++++++------ .../forms/ContributeQuestionForm.tsx | 1 - apps/portal/src/pages/questions/browse.tsx | 114 +++++++++++++----- apps/portal/src/utils/questions/constants.ts | 24 ++++ .../{useSearchFilter.ts => useSearchParam.ts} | 53 +++++--- 5 files changed, 210 insertions(+), 89 deletions(-) rename apps/portal/src/utils/questions/{useSearchFilter.ts => useSearchParam.ts} (53%) diff --git a/apps/portal/src/components/questions/QuestionSearchBar.tsx b/apps/portal/src/components/questions/QuestionSearchBar.tsx index c5f401ea..69bb3ed5 100644 --- a/apps/portal/src/components/questions/QuestionSearchBar.tsx +++ b/apps/portal/src/components/questions/QuestionSearchBar.tsx @@ -4,29 +4,41 @@ import { } from '@heroicons/react/24/outline'; import { Button, Select, TextInput } from '@tih/ui'; -export type SortOption = { +export type SortOption = { label: string; - value: string; + value: Value; }; -export type QuestionSearchBarProps> = { - onFilterOptionsToggle: () => void; - onSortChange?: (sortValue: SortOptions[number]['value']) => void; - sortOptions: SortOptions; - sortValue: SortOptions[number]['value']; +type SortOrderProps = { + onSortOrderChange?: (sortValue: SortOrder) => void; + sortOrderOptions: ReadonlyArray>; + sortOrderValue: SortOrder; }; -export default function QuestionSearchBar< - SortOptions extends Array, ->({ - onSortChange, - sortOptions, - sortValue, +type SortTypeProps = { + onSortTypeChange?: (sortType: SortType) => void; + sortTypeOptions: ReadonlyArray>; + sortTypeValue: SortType; +}; + +export type QuestionSearchBarProps = + SortOrderProps & + SortTypeProps & { + onFilterOptionsToggle: () => void; + }; + +export default function QuestionSearchBar({ + onSortOrderChange, + sortOrderOptions, + sortOrderValue, + onSortTypeChange, + sortTypeOptions, + sortTypeValue, onFilterOptionsToggle, -}: QuestionSearchBarProps) { +}: QuestionSearchBarProps) { return ( -
-
+
+
-
- - Sort by: - - { + const chosenOption = sortTypeOptions.find( + (option) => String(option.value) === value, + ); + if (chosenOption) { + onSortTypeChange?.(chosenOption.value); + } + }} + /> +
+
+