From 9599f2c6237cb9fe46a6b4c115869ddfb0f0f244 Mon Sep 17 00:00:00 2001 From: Jonathan Green Date: Wed, 27 Mar 2024 14:29:50 -0300 Subject: [PATCH 1/6] Add fiction list filter --- src/components/AdvancedSearchBuilder.tsx | 6 ++++ src/components/AdvancedSearchFilterInput.tsx | 37 ++++++++++++++++++-- src/stylesheets/advanced_search.scss | 6 ++++ 3 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/components/AdvancedSearchBuilder.tsx b/src/components/AdvancedSearchBuilder.tsx index 578e47be3..ac5431b4b 100644 --- a/src/components/AdvancedSearchBuilder.tsx +++ b/src/components/AdvancedSearchBuilder.tsx @@ -33,6 +33,12 @@ export const fields = [ { value: "audience", label: "audience" }, { value: "author", label: "author" }, { value: "title", label: "title" }, + { + value: "fiction", + label: "fiction", + options: ["fiction", "nonfiction"], + operators: ["eq"], + }, ]; export const operators = [ diff --git a/src/components/AdvancedSearchFilterInput.tsx b/src/components/AdvancedSearchFilterInput.tsx index c053e4750..feb7500e4 100644 --- a/src/components/AdvancedSearchFilterInput.tsx +++ b/src/components/AdvancedSearchFilterInput.tsx @@ -25,6 +25,7 @@ export default function AdvancedSearchFilterInput({ const handleKeyChange = (value) => { setFilterKey(value); + setFilterValue(""); }; const handleOpChange = () => { @@ -64,6 +65,26 @@ export default function AdvancedSearchFilterInput({ }; const selectedField = fields.find((field) => field.value === filterKey); + const selectedFieldOperators = + selectedField.operators !== undefined + ? operators.filter((element) => { + return selectedField.operators.includes(element.value); + }) + : operators; + + const selectedFieldOptions = selectedField.options ?? []; + const selectedFieldElementType = selectedFieldOptions.length + ? "input" + : "select"; + if (selectedFieldOptions.length !== 0) { + if ( + !selectedFieldOptions.find((element) => { + return element === filterValue; + }) + ) { + setFilterValue(selectedFieldOptions[0]); + } + } return (
@@ -89,8 +110,9 @@ export default function AdvancedSearchFilterInput({ onChange={handleOpChange} ref={opSelect} value={filterOp} + className="filter-operator" > - {operators.map(({ value, label }) => ( + {selectedFieldOperators.map(({ value, label }) => ( + ))} +