import React, { useContext, useState } from 'react'; import { Filter, FilterOption } from '../../types'; import MultiRangeFilter from '../multi-range-filter'; import SearchResultsConfigurationContext from '../../search-results-configuration-context'; import Spinner from '../spinner/spinner'; import Icon from '../../../shared/components/icon'; import { getTranslations } from '../../../shared/utils/localization-util'; interface FiltersProps { initialFilters: Filter[]; filters: Filter[]; isOpen: boolean; handleSetIsOpen: () => void; isLoading?: boolean; setFilters: (filters: Filter[]) => void; resetFilters: (filters: Filter[]) => void; } const Filters: React.FC = ({ initialFilters, filters, isOpen, handleSetIsOpen, isLoading, setFilters, resetFilters }) => { const context = useContext(SearchResultsConfigurationContext); if (!context || !context.showFilters) { return null; } const translations = getTranslations(context?.languageCode ?? 'en-GB'); const [visibleFilters, setVisibleFilters] = useState>({}); const toggleFilterVisibility = (filterId: string) => { setVisibleFilters((prev) => ({ ...prev, [filterId]: !prev[filterId] })); }; const handleCheckBoxFilter = (filter: Filter, option: FilterOption) => { const updated = filters.map((f) => { if (f.property !== filter.property) return f; return { ...f, options: f.options?.map((opt) => (opt.value === option.value ? { ...opt, isChecked: !opt.isChecked } : opt)) }; }); setFilters(updated); }; const handleStarRatingFilter = (filter: Filter, rating: number) => { const updated = filters.map((f) => { if (f.property !== filter.property) return f; return { ...f, selectedRating: f.selectedRating === rating ? 0 : rating }; }); setFilters(updated); }; const handleSliderChange = (filter: Filter, newMin: number, newMax: number) => { const updated = filters.map((f) => { if (f.property !== filter.property) return f; return { ...f, selectedMin: newMin, selectedMax: newMax }; }); setFilters(updated); }; const handleFullReset = () => { if (!isLoading) { resetFilters(initialFilters); } }; return (
handleSetIsOpen()}>

{translations.SRP.FILTERS}

{!isLoading && ( handleFullReset()}> {translations.SRP.RESET} )}
{isLoading ? ( ) : ( <>
{filters.map((filter, index) => { const isVisible = visibleFilters[filter.property] ?? true; return (
toggleFilterVisibility(filter.property)} role="button" tabIndex={0}>
{filter.label}
{isVisible && filter.type === 'checkbox' && (
{filter.options && filter.options.map((option: FilterOption, optionIndex) => (
))}
)} {isVisible && filter.type === 'toggle' && (
{filter.options?.map((option: FilterOption, optionIndex) => (
{option.label}
))}
)} {isVisible && filter && filter.type === 'slider' && (() => { const min = filter.min ?? 0; const max = filter.max ?? 100; const selectedMin = filter.selectedMin ?? min; const selectedMax = filter.selectedMax ?? max; const range = max - min || 1; return ( `${value}`} onChange={(newMin, newMax) => { handleSliderChange(filter, newMin, newMax); }} /> ); })()} {isVisible && filter.property === 'rating' && filter.type === 'star-rating' && (() => { const selectedRating = filter.selectedRating ?? 0; return (
{[5, 4, 3, 2, 1].map((star) => ( handleStarRatingFilter(filter, star)} role="button" tabIndex={0} onKeyDown={(e) => { if (e.key === 'Enter' || e.key === ' ') handleStarRatingFilter(filter, star); }}> ★ ))}
); })()}
); })}
)}
); }; export default Filters;