From 2283148e6d02c6c7a1bb12754836fc58c7279777 Mon Sep 17 00:00:00 2001 From: "claudia.holhos" Date: Thu, 25 Feb 2021 14:14:21 +0200 Subject: [PATCH] code refactoring --- .../templates/Search/filterPrice.tsx | 116 +++++++++--------- 1 file changed, 57 insertions(+), 59 deletions(-) diff --git a/src/components/templates/Search/filterPrice.tsx b/src/components/templates/Search/filterPrice.tsx index c3563c5fc..11fe8162d 100644 --- a/src/components/templates/Search/filterPrice.tsx +++ b/src/components/templates/Search/filterPrice.tsx @@ -55,6 +55,57 @@ export default function FilterPrice({ navigate(urlLocation) } + async function handleSelectedFilter(isSelected: boolean, value: string) { + if ( + FilterByPriceOptions.Dynamic.includes(value) || + FilterByPriceOptions.Fixed.includes(value) + ) { + if (isSelected) { + if (priceSelections.length > 1) { + // both selected -> select the other one + const otherValue = priceFilterItems.find((p) => p.value !== value) + .value + await applyPriceFilter(otherValue) + setPriceSelections([otherValue]) + } else { + // only the current one selected -> deselect it + await applyPriceFilter(undefined) + setPriceSelections([]) + } + } else { + if (priceSelections.length) { + // one already selected -> both selected + await applyPriceFilter(undefined) + setPriceSelections(priceFilterItems.map((p) => p.value)) + } else { + // none selected -> select + await applyPriceFilter(value) + setPriceSelections([value]) + } + } + } else { + if (isSelected) { + if (serviceSelections.length > 1) { + const otherValue = serviceFilterItems.find((p) => p.value !== value) + .value + await applyServiceFilter(otherValue) + setServiceSelections([otherValue]) + } else { + await applyServiceFilter(undefined) + setServiceSelections([]) + } + } else { + if (serviceSelections.length) { + await applyServiceFilter(undefined) + setServiceSelections(serviceFilterItems.map((p) => p.value)) + } else { + await applyServiceFilter(value) + setServiceSelections([value]) + } + } + } + } + return (
@@ -71,35 +122,9 @@ export default function FilterPrice({ style="text" key={index} className={selectFilter} - onClick={ - // only works for two price options - isSelected - ? async () => { - if (priceSelections.length > 1) { - // both selected -> select the other one - const otherValue = priceFilterItems.find( - (p) => p.value !== e.value - ).value - await applyPriceFilter(otherValue) - setPriceSelections([otherValue]) - } else { - // only the current one selected -> deselect it - await applyPriceFilter(undefined) - setPriceSelections([]) - } - } - : async () => { - if (priceSelections.length) { - // one already selected -> both selected - await applyPriceFilter(undefined) - setPriceSelections(priceFilterItems.map((p) => p.value)) - } else { - // none selected -> select - await applyPriceFilter(e.value) - setPriceSelections([e.value]) - } - } - } + onClick={async () => { + handleSelectedFilter(isSelected, e.value) + }} > {e.display} @@ -118,36 +143,9 @@ export default function FilterPrice({ style="text" key={index} className={selectFilter} - onClick={ - isSelected - ? async () => { - if (serviceSelections.length > 1) { - // both selected -> select the other one - const otherValue = serviceFilterItems.find( - (p) => p.value !== e.value - ).value - await applyServiceFilter(otherValue) - setServiceSelections([otherValue]) - } else { - // only the current one selected -> deselect it - await applyServiceFilter(undefined) - setServiceSelections([]) - } - } - : async () => { - if (serviceSelections.length) { - // one already selected -> both selected - await applyServiceFilter(undefined) - setServiceSelections( - serviceFilterItems.map((p) => p.value) - ) - } else { - // none selected -> select - await applyServiceFilter(e.value) - setServiceSelections([e.value]) - } - } - } + onClick={async () => { + handleSelectedFilter(isSelected, e.value) + }} > {e.display}