Skip to content

Commit

Permalink
Merge pull request #420 from ekamuktia/revert-filter-ui
Browse files Browse the repository at this point in the history
Revert filter & sort ui
  • Loading branch information
ekamuktia authored Jul 25, 2021
2 parents 9f247ed + 886d214 commit 01614d8
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 165 deletions.
6 changes: 0 additions & 6 deletions components/__tests__/search-form.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,6 @@ describe("SearchForm", () => {
>
Cari
</button>
<button
class="flex px-4 py-2 text-sm rounded-md items-center justify-center border border-transparent font-medium focus:outline-none focus:ring-2 focus:ring-offset-2 text-blue-700 bg-blue-100 hover:bg-blue-200 focus:ring-blue-500 disabled:cursor-not-allowed disabled:opacity-75 flex-1 ml-2"
type="reset"
>
Reset
</button>
</div>
</div>
</form>
Expand Down
125 changes: 15 additions & 110 deletions components/search-filter-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -17,10 +15,10 @@ export interface SortSetting {
}

export interface SearchFilterModalProps {
isLoading?: boolean;
checkDocSize?: boolean;
filters: any;
filterItems?: {};
isLoading?: boolean;
isOpen: boolean;
handleFilterChange: ChangeEventHandler<HTMLSelectElement>;
handleSortChange: ChangeEventHandler<HTMLSelectElement>;
Expand All @@ -43,110 +41,6 @@ export function SearchFilterModal({
}: SearchFilterModalProps) {
const closeButtonRef = useRef(null);

const filterForms = useMemo(
() =>
filterItems && (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
{Object.entries(filterItems).map(([key, value], idx) => {
const { title, buckets }: any = value;
return (
<div key={`filter-${idx}`} className="space-y-1">
<FormLabel htmlFor={`filter-${key}`}>{title}</FormLabel>
<InputSelect
name={key}
onChange={handleFilterChange}
title={title}
value={filters?.[key]?.length ? filters[key][0] : ""}
>
<option value="">Semua</option>
{buckets.map((bucket: any, bIdx: number) => {
if (bucket.key) {
if (checkDocSize) {
if (bucket.doc_count > 0) {
return (
<option
key={`option-${key}-${bIdx + 1}`}
value={bucket.key}
>
{bucket.key}
</option>
);
}
// 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 (
<option
key={`option-${key}-${bIdx + 1}`}
value={bucket.key}
>
{bucket.key}
</option>
);
}
}
return null;
})}
</InputSelect>
</div>
);
})}
</div>
),
[filterItems, handleFilterChange, filters, checkDocSize],
);

const sortForms = useMemo(
() =>
sortSettings?.length && (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1">
<FormLabel htmlFor="sort-by">Urut berdasarkan</FormLabel>
<InputSelect
name="sort-by"
onChange={handleSortChange}
title="Urut berdasarkan"
value={sortBy}
>
{sortSettings.map((cur, idx) => {
return (
<option key={`sort-by-${idx}`} value={cur.value}>
{cur.label}
</option>
);
})}
</InputSelect>
</div>
</div>
),
[handleSortChange, sortBy, sortSettings],
);

const renderFilterForms = () => {
if (isLoading) {
return (
<>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<SelectSkeleton />
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<SelectSkeleton />
<SelectSkeleton />
</div>
</>
);
}

return (
<>
{filterForms}
{sortForms}
</>
);
};

return (
<Transition.Root as={Fragment} show={isOpen}>
<Dialog
Expand Down Expand Up @@ -206,7 +100,18 @@ export function SearchFilterModal({
>
Filter
</Dialog.Title>
<div className="mt-4 space-y-4">{renderFilterForms()}</div>
<div className="mt-4 space-y-4">
<SearchFilter
checkDocSize={checkDocSize}
filterItems={filterItems}
filters={filters}
handleFilterChange={handleFilterChange}
handleSortChange={handleSortChange}
isLoading={isLoading}
sortBy={sortBy}
sortSettings={sortSettings}
/>
</div>
</div>
</div>
</div>
Expand Down
142 changes: 142 additions & 0 deletions components/search-filter.tsx
Original file line number Diff line number Diff line change
@@ -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<HTMLSelectElement>;
handleSortChange: ChangeEventHandler<HTMLSelectElement>;
isLoading?: boolean;
sortBy: string;
sortSettings?: SortSetting[];
}

export function SearchFilter({
checkDocSize,
filters,
filterItems,
handleFilterChange,
handleSortChange,
isLoading,
sortBy,
sortSettings,
}: SearchFilterProps) {
const filterForms = useMemo(
() =>
filterItems && (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
{Object.entries(filterItems).map(([key, value], idx) => {
const { title, buckets }: any = value;
return (
<div key={`filter-${idx}`} className="space-y-1">
<FormLabel htmlFor={`filter-${key}`}>{title}</FormLabel>
<InputSelect
name={key}
onChange={handleFilterChange}
title={title}
value={filters?.[key]?.length ? filters[key][0] : ""}
>
<option value="">Semua</option>
{buckets.map((bucket: any, bIdx: number) => {
if (bucket.key) {
if (checkDocSize) {
if (bucket.doc_count > 0) {
return (
<option
key={`option-${key}-${bIdx + 1}`}
value={bucket.key}
>
{bucket.key}
</option>
);
}
// 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 (
<option
key={`option-${key}-${bIdx + 1}`}
value={bucket.key}
>
{bucket.key}
</option>
);
}
}
return null;
})}
</InputSelect>
</div>
);
})}
</div>
),
[filterItems, handleFilterChange, filters, checkDocSize],
);

const sortForms = useMemo(
() =>
sortSettings?.length && (
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<div className="space-y-1">
<FormLabel htmlFor="sort-by">Urut berdasarkan</FormLabel>
<InputSelect
name="sort-by"
onChange={handleSortChange}
title="Urut berdasarkan"
value={sortBy}
>
{sortSettings.map((cur, idx) => {
return (
<option key={`sort-by-${idx}`} value={cur.value}>
{cur.label}
</option>
);
})}
</InputSelect>
</div>
</div>
),
[handleSortChange, sortBy, sortSettings],
);

const renderFilterForms = () => {
if (isLoading) {
return (
<>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<SelectSkeleton />
</div>
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
<SelectSkeleton />
<SelectSkeleton />
</div>
</>
);
}

return (
<>
{filterForms}
{sortForms}
</>
);
};

return renderFilterForms();
}
Loading

0 comments on commit 01614d8

Please sign in to comment.