From 93669ca15a1a3ab4f0ed84e4af001c474942add2 Mon Sep 17 00:00:00 2001 From: ekamuktia Date: Sun, 25 Jul 2021 22:07:14 +0700 Subject: [PATCH 1/3] revert filter ui, usefiltermodal as prop --- components/search-filter-modal.tsx | 125 +++---------------------- components/search-filter.tsx | 142 +++++++++++++++++++++++++++++ components/search-form.tsx | 66 +++++++++----- 3 files changed, 199 insertions(+), 134 deletions(-) create mode 100644 components/search-filter.tsx diff --git a/components/search-filter-modal.tsx b/components/search-filter-modal.tsx index 3e20e2a33..d5793e53f 100644 --- a/components/search-filter-modal.tsx +++ b/components/search-filter-modal.tsx @@ -2,11 +2,9 @@ /* eslint-disable @typescript-eslint/no-unsafe-call */ /* eslint-disable @typescript-eslint/no-unsafe-member-access */ /* eslint-disable @typescript-eslint/no-unsafe-assignment */ -import { ChangeEventHandler, Fragment, useMemo, useRef } from "react"; +import { ChangeEventHandler, Fragment, useRef } from "react"; -import { FormLabel } from "./ui/forms/form-label"; -import { InputSelect } from "./ui/forms/input-select"; -import { SelectSkeleton } from "./ui/skeleton-loading"; +import { SearchFilter } from "~/components/search-filter"; import { Dialog, Transition } from "@headlessui/react"; import { XIcon } from "@heroicons/react/solid"; @@ -17,10 +15,10 @@ export interface SortSetting { } export interface SearchFilterModalProps { - isLoading?: boolean; checkDocSize?: boolean; filters: any; filterItems?: {}; + isLoading?: boolean; isOpen: boolean; handleFilterChange: ChangeEventHandler; handleSortChange: ChangeEventHandler; @@ -43,110 +41,6 @@ export function SearchFilterModal({ }: SearchFilterModalProps) { const closeButtonRef = useRef(null); - const filterForms = useMemo( - () => - filterItems && ( -
- {Object.entries(filterItems).map(([key, value], idx) => { - const { title, buckets }: any = value; - return ( -
- {title} - - - {buckets.map((bucket: any, bIdx: number) => { - if (bucket.key) { - if (checkDocSize) { - if (bucket.doc_count > 0) { - return ( - - ); - } - // Do not print, when doc_count = 0 and checkDocSize set to true - return null; - } else { - // FAQ page doesn't check the doc_count - // just pass checkDocSize props to false - return ( - - ); - } - } - return null; - })} - -
- ); - })} -
- ), - [filterItems, handleFilterChange, filters, checkDocSize], - ); - - const sortForms = useMemo( - () => - sortSettings?.length && ( -
-
- Urut berdasarkan - - {sortSettings.map((cur, idx) => { - return ( - - ); - })} - -
-
- ), - [handleSortChange, sortBy, sortSettings], - ); - - const renderFilterForms = () => { - if (isLoading) { - return ( - <> -
- -
-
- - -
- - ); - } - - return ( - <> - {filterForms} - {sortForms} - - ); - }; - return ( Filter -
{renderFilterForms()}
+
+ +
diff --git a/components/search-filter.tsx b/components/search-filter.tsx new file mode 100644 index 000000000..80643b846 --- /dev/null +++ b/components/search-filter.tsx @@ -0,0 +1,142 @@ +/* eslint-disable @typescript-eslint/no-explicit-any */ +/* eslint-disable @typescript-eslint/no-unsafe-call */ +/* eslint-disable @typescript-eslint/no-unsafe-member-access */ +/* eslint-disable @typescript-eslint/no-unsafe-assignment */ +import { ChangeEventHandler, useMemo } from "react"; + +import { FormLabel } from "./ui/forms/form-label"; +import { InputSelect } from "./ui/forms/input-select"; +import { SelectSkeleton } from "./ui/skeleton-loading"; + +export interface SortSetting { + value: string; + label: string; +} + +export interface SearchFilterProps { + checkDocSize?: boolean; + filters: any; + filterItems?: {}; + handleFilterChange: ChangeEventHandler; + handleSortChange: ChangeEventHandler; + isLoading?: boolean; + sortBy: string; + sortSettings?: SortSetting[]; +} + +export function SearchFilter({ + checkDocSize, + filters, + filterItems, + handleFilterChange, + handleSortChange, + isLoading, + sortBy, + sortSettings, +}: SearchFilterProps) { + const filterForms = useMemo( + () => + filterItems && ( +
+ {Object.entries(filterItems).map(([key, value], idx) => { + const { title, buckets }: any = value; + return ( +
+ {title} + + + {buckets.map((bucket: any, bIdx: number) => { + if (bucket.key) { + if (checkDocSize) { + if (bucket.doc_count > 0) { + return ( + + ); + } + // Do not print, when doc_count = 0 and checkDocSize set to true + return null; + } else { + // FAQ page doesn't check the doc_count + // just pass checkDocSize props to false + return ( + + ); + } + } + return null; + })} + +
+ ); + })} +
+ ), + [filterItems, handleFilterChange, filters, checkDocSize], + ); + + const sortForms = useMemo( + () => + sortSettings?.length && ( +
+
+ Urut berdasarkan + + {sortSettings.map((cur, idx) => { + return ( + + ); + })} + +
+
+ ), + [handleSortChange, sortBy, sortSettings], + ); + + const renderFilterForms = () => { + if (isLoading) { + return ( + <> +
+ +
+
+ + +
+ + ); + } + + return ( + <> + {filterForms} + {sortForms} + + ); + }; + + return renderFilterForms(); +} diff --git a/components/search-form.tsx b/components/search-form.tsx index 7822c206e..ec554fd4f 100644 --- a/components/search-form.tsx +++ b/components/search-form.tsx @@ -19,6 +19,7 @@ import { debounce } from "ts-debounce"; import { SearchFilterModal, SortSetting } from "./search-filter-modal"; import { FormGroup } from "./ui/forms/form-group"; import { FilterIcon } from "@heroicons/react/outline"; +import { SearchFilter } from "~/components/search-filter"; interface FormElements extends HTMLFormControlsCollection { keywordsInput: HTMLInputElement; @@ -42,6 +43,7 @@ interface SearchFormProps { }; isLoading?: boolean; sortSettings?: SortSetting[]; + useFilterModal?: boolean; } export function SearchForm({ @@ -54,6 +56,7 @@ export function SearchForm({ initialValue, isLoading, sortSettings, + useFilterModal = false, }: SearchFormProps) { const defaultSort = sortSettings?.length ? sortSettings[0].value : ""; const [keywords, setKeywords] = useState(""); @@ -138,18 +141,20 @@ export function SearchForm({ type="text" value={keywords} /> - {!isLoading && (filterItems || sortSettings?.length) && ( - - )} + {useFilterModal && + !isLoading && + (filterItems || sortSettings?.length) && ( + + )} @@ -165,18 +170,31 @@ export function SearchForm({ - + {useFilterModal ? ( + + ) : ( + + )} ); } From dda5b7606386567caecd10cdeca9c833e6962535 Mon Sep 17 00:00:00 2001 From: ekamuktia Date: Sun, 25 Jul 2021 22:32:31 +0700 Subject: [PATCH 2/3] remove reset button, remove sort in contact list page --- components/__tests__/search-form.test.tsx | 6 ----- components/search-form.tsx | 27 +++++------------------ pages/provinces/[provinceSlug]/index.tsx | 4 ---- 3 files changed, 6 insertions(+), 31 deletions(-) diff --git a/components/__tests__/search-form.test.tsx b/components/__tests__/search-form.test.tsx index eb753b434..a62301721 100644 --- a/components/__tests__/search-form.test.tsx +++ b/components/__tests__/search-form.test.tsx @@ -59,12 +59,6 @@ describe("SearchForm", () => { > Cari - diff --git a/components/search-form.tsx b/components/search-form.tsx index ec554fd4f..91ce1e20b 100644 --- a/components/search-form.tsx +++ b/components/search-form.tsx @@ -11,7 +11,7 @@ import React, { useState, } from "react"; -import { PrimaryButton, SecondaryButton } from "./ui/button"; +import { PrimaryButton } from "./ui/button"; import { FormLabel } from "./ui/forms/form-label"; import { InputText } from "./ui/forms/input-text"; @@ -70,14 +70,6 @@ export function SearchForm({ onSubmitKeywords(keywords, {}, sortBy); } - function handleReset(event: FormEvent) { - event.preventDefault(); - setKeywords(""); - setFilters({}); - setSortBy(defaultSort); - onSubmitKeywords(""); - } - const debouncedSearch = useCallback( debounce( (keywordsValue: string, filtersValue?: any, sortValue?: string) => @@ -121,11 +113,7 @@ export function SearchForm({ }, [initialValue]); return ( -
+
@@ -158,16 +146,13 @@ export function SearchForm({
-
- {!autoSearch && ( + {!autoSearch && ( +
Cari - )} - - Reset - -
+
+ )}
{useFilterModal ? ( diff --git a/pages/provinces/[provinceSlug]/index.tsx b/pages/provinces/[provinceSlug]/index.tsx index 78503140f..d4a4e0611 100644 --- a/pages/provinces/[provinceSlug]/index.tsx +++ b/pages/provinces/[provinceSlug]/index.tsx @@ -96,10 +96,6 @@ export default function ProvincePage(props: ProvinceProps) { itemName="kontak" onSubmitKeywords={handleSubmitKeywords} placeholderText="Cari berdasarkan kontak, alamat, provider, dan keterangan" - sortSettings={[ - { value: "verified_first", label: "Terverifikasi" }, - { value: "penyedia_asc", label: "Nama" }, - ]} /> Date: Sun, 25 Jul 2021 22:45:51 +0700 Subject: [PATCH 3/3] rename useFilterModal to withFilterModal --- components/search-form.tsx | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/components/search-form.tsx b/components/search-form.tsx index 91ce1e20b..c38f5dccb 100644 --- a/components/search-form.tsx +++ b/components/search-form.tsx @@ -43,7 +43,7 @@ interface SearchFormProps { }; isLoading?: boolean; sortSettings?: SortSetting[]; - useFilterModal?: boolean; + withFilterModal?: boolean; } export function SearchForm({ @@ -56,7 +56,7 @@ export function SearchForm({ initialValue, isLoading, sortSettings, - useFilterModal = false, + withFilterModal = false, }: SearchFormProps) { const defaultSort = sortSettings?.length ? sortSettings[0].value : ""; const [keywords, setKeywords] = useState(""); @@ -129,7 +129,7 @@ export function SearchForm({ type="text" value={keywords} /> - {useFilterModal && + {withFilterModal && !isLoading && (filterItems || sortSettings?.length) && (