Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: City filter #279

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions src/js/filters/CityFilters.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useCallback, useContext, useMemo } from 'react';
import { MultiValue } from 'react-select';
import { Option, Venue } from '../types';
import { useDb } from '../dbHooks';
import { FilterSection } from './FilterSection';
import { FiltersContext } from './FiltersContext';
import { formatVenueName, sortByLabel } from '../utils';
import { DropdownMultiselect } from './DropdownMultiselect';

const nothingFound = () => 'Nothing found';

export const CityFilters = () => {
const {
venues: { data: venues },
} = useDb();

const { cityFilters, setCityFilters } = useContext(FiltersContext);

const handleChange = useCallback(
(options: MultiValue<Option>) => {
setCityFilters(options.map(({ value }) => value as string));
},
[setCityFilters]
);

const options = useMemo(() => {
return Object.entries(venues || {})
.reduce<Record<string, Set<string>>>((accum, [, { country, city }]) => {
if (!accum[country]) {
accum[country] = new Set();
}
accum[country].add(city);

return accum;
}, {});
}, [venues]);

const defaultValue =
venues &&
cityFilters.map(city => ({
value: city,
label: city,
}));

return (
venues && (
<FilterSection label="City filters:">
<DropdownMultiselect
defaultValue={defaultValue}
noOptionsMessage={nothingFound}
placeholder="Search cities"
options={options}
onChange={handleChange}
/>
</FilterSection>
)
);
};
2 changes: 2 additions & 0 deletions src/js/filters/FilterBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { EpisodeTypeFilters } from './EpisodeTypeFilters';
import { PresenterFilters } from './PresenterFilters';
import { FiltersToggle } from './FiltersToggle';
import { VenueFilters } from './VenueFilters';
import { CityFilters } from './CityFilters';

const StyledWrapper = styled.div`
display: flex;
Expand Down Expand Up @@ -46,6 +47,7 @@ export const FilterBar = () => {
<EpisodeTypeFilters />
<PresenterFilters />
<VenueFilters />
<CityFilters />
</FiltersWrapper>
)}
</StyledWrapper>
Expand Down
21 changes: 20 additions & 1 deletion src/js/filters/FiltersContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ import {

type PresenterFiltersState = number[];
type VenueFiltersState = number[];
type FilterToggleHandler<T> = (args: { name: T; checked: boolean }) => void;
type CityFiltersState = string[];
type FilterToggleHandler<T> = (args: { name: T; checked: boolean; }) => void;

// Four Undisclosed Locations
const WFH_VENUE_ID = 2;
Expand All @@ -41,6 +42,7 @@ const defaultSearchFiltersState = {

const defaultPresenterFiltersState: PresenterFiltersState = [];
const defaultVenueFiltersState: VenueFiltersState = [];
const defaultCityFiltersState: CityFiltersState = [];

const noop = () => {
return undefined;
Expand All @@ -52,10 +54,12 @@ export const FiltersContext = createContext<{
searchFilters: SearchFiltersState;
presenterFilters: PresenterFiltersState;
venueFilters: VenueFiltersState;
cityFilters: CityFiltersState;
handleEpisodeTypeFilterToggle: FilterToggleHandler<EpisodeType>;
handleSearchFilterToggle: FilterToggleHandler<SearchField>;
setPresenterFilters: Dispatch<SetStateAction<PresenterFiltersState>>;
setVenueFilters: Dispatch<SetStateAction<VenueFiltersState>>;
setCityFilters: Dispatch<SetStateAction<CityFiltersState>>;
setEpisodeTypeFilters: Dispatch<SetStateAction<EpisodeTypeFiltersState>>;
numFiltersAltered: number;
}>({
Expand All @@ -64,10 +68,12 @@ export const FiltersContext = createContext<{
searchFilters: defaultSearchFiltersState,
presenterFilters: defaultPresenterFiltersState,
venueFilters: defaultVenueFiltersState,
cityFilters: defaultCityFiltersState,
handleEpisodeTypeFilterToggle: noop,
handleSearchFilterToggle: noop,
setPresenterFilters: noop,
setVenueFilters: noop,
setCityFilters: noop,
setEpisodeTypeFilters: n => n,
numFiltersAltered: 0,
});
Expand All @@ -82,6 +88,8 @@ export const FiltersContextProvider = ({

const [venueFilters, setVenueFilters] = useState<VenueFiltersState>([]);

const [cityFilters, setCityFilters] = useState<CityFiltersState>([]);

const [episodeTypeFilters, setEpisodeTypeFilters] =
useState<EpisodeTypeFiltersState>(defaultEpisodeTypeFiltersState);

Expand Down Expand Up @@ -131,6 +139,15 @@ export const FiltersContextProvider = ({

return venueFilters.includes(ep.venue);
})
.filter(ep => {
if (cityFilters.length === 0) {
return true;
}



return cityFilters.includes(ep.venue);
})
.filter(ep => {
return (
(episodeTypeFilters.live && ep.live) ||
Expand Down Expand Up @@ -161,10 +178,12 @@ export const FiltersContextProvider = ({
searchFilters,
presenterFilters,
venueFilters,
cityFilters,
handleEpisodeTypeFilterToggle,
handleSearchFilterToggle,
setPresenterFilters,
setVenueFilters,
setCityFilters,
setEpisodeTypeFilters,
numFiltersAltered,
}}
Expand Down
2 changes: 1 addition & 1 deletion src/js/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export type EpisodeTypeFilterLabels = Record<EpisodeType, string>;

export type Option = {
label: string;
value: number;
value: string | number;
};

export type SelectedOption<T> = {
Expand Down
Loading