Skip to content

Commit

Permalink
chore: cleanup unused filter code (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
dave-hay authored Jun 18, 2022
1 parent e51c891 commit 37a4c8e
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 124 deletions.
109 changes: 57 additions & 52 deletions src/components/results/FilterBar.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import {CurrentFilters} from "../../types";
import React, {useContext} from "react";
import FormControlLabel from "@mui/material/FormControlLabel";
import Checkbox from "@mui/material/Checkbox";
import styled from "styled-components";
import FormControl from "@mui/material/FormControl";
import FormLabel from "@mui/material/FormLabel";
import FormGroup from "@mui/material/FormGroup";
import {filterGroups} from "./filterHelpers";
import Button from "@mui/material/Button";
import {
FormControlLabel,
Checkbox,
FormGroup,
FormControl,
FormLabel,
Button,
} from "@mui/material";
import {GlobalStateContext} from "../../context/globalStates";
import { colors } from "../../theme";
import {filterGroups} from "./filterHelpers";
import {colors} from "../../theme";
import {CurrentFilters} from "../../types";

const Sidebar = styled.div<{isFilterOpen: boolean}>`
display: flex;
Expand Down Expand Up @@ -38,58 +40,61 @@ const Sidebar = styled.div<{isFilterOpen: boolean}>`
&:not(:first-child) {
margin-top: 15px;
}
}
@media (max-width: 752px) {
display: ${props => (props.isFilterOpen ? "flex" : "none")};
display: ${(props) => (props.isFilterOpen ? "flex" : "none")};
background-color: white;
position: relative;
box-shadow: 0px 5px 5px -3px rgb(0 0 0 / 20%),
0px 8px 10px 1px rgb(0 0 0 / 14%),
0px 3px 14px 2px rgb(0 0 0 / 12%);
0px 8px 10px 1px rgb(0 0 0 / 14%), 0px 3px 14px 2px rgb(0 0 0 / 12%);
padding: 30px;
}
`;

export const FilterBar: React.FC = () => {
const {
isFilterOpen,
currentFilters,
handleFilterChange,
handleClearFilters
} = useContext(GlobalStateContext);
const {isFilterOpen, currentFilters, handleFilterChange, handleClearFilters} =
useContext(GlobalStateContext);

return (
<Sidebar isFilterOpen={isFilterOpen}>
{filterGroups.map(({groupName, groupLabel, filters}) => (
<FormControl key={groupName} component="fieldset">
<FormLabel component="legend">{groupLabel}</FormLabel>
<FormGroup>
{filters.map(({label, name}) => (
<FormControlLabel
key={name}
label={label}
control={
<Checkbox
name={name}
checked={
!!currentFilters[
groupName as keyof CurrentFilters
]?.includes(name)
}
onChange={handleFilterChange(groupName as keyof CurrentFilters)}
/>
}
sx={{fontSize: '.75rem', height: '1.25rem', marginBottom: '0.3125rem', marginTop: '0.3125rem', whiteSpace: 'nowrap', lineHeight: '1'}}
/>
))}
</FormGroup>
</FormControl>
))}
<Button color="secondary" onClick={handleClearFilters}>
clear
</Button>
</Sidebar>);
}
;
return (
<Sidebar isFilterOpen={isFilterOpen}>
{filterGroups.map(({groupName, groupLabel, filters}) => (
<FormControl key={groupName} component="fieldset">
<FormLabel component="legend">{groupLabel}</FormLabel>
<FormGroup>
{filters.map(({label, name}) => (
<FormControlLabel
key={name}
label={label}
control={
<Checkbox
name={name}
checked={
!!currentFilters[
groupName as keyof CurrentFilters
]?.includes(name)
}
onChange={handleFilterChange(
groupName as keyof CurrentFilters
)}
/>
}
sx={{
fontSize: ".75rem",
height: "1.25rem",
marginBottom: "0.3125rem",
marginTop: "0.3125rem",
whiteSpace: "nowrap",
lineHeight: "1",
}}
/>
))}
</FormGroup>
</FormControl>
))}
<Button color="secondary" onClick={handleClearFilters}>
clear
</Button>
</Sidebar>
);
};
84 changes: 12 additions & 72 deletions src/components/results/filterHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import {CurrentFilters, Result} from "../../types";
import {Dispatch, SetStateAction} from "react";

const isEmpty = (currentFilters: CurrentFilters) =>
Object.keys(currentFilters).length === 0;

type MatchGroupArgs = {
result: Result;
group: string;
currentFilters: CurrentFilters;
};

type FilterGroup = {
groupName: string;
groupLabel: string;
filters: {
name: string;
label: string;
}[];
};

export const matchGroup = ({result, group, currentFilters}: MatchGroupArgs) => {
for (const filter of currentFilters[group as keyof CurrentFilters]) {
if (result[filter as keyof Result]) {
Expand All @@ -19,6 +25,9 @@ export const matchGroup = ({result, group, currentFilters}: MatchGroupArgs) => {
return false;
};

const isEmpty = (currentFilters: CurrentFilters) =>
Object.keys(currentFilters).length === 0;

export const applyFilters = (
results: Result[],
currentFilters: CurrentFilters
Expand Down Expand Up @@ -58,55 +67,6 @@ export const applyFilterChanges =
setCurrentFilters(newFilters);
};

export const allFilters = [
"smallBusiness",
"nonProfit",
"sfCounty",
"alamedaCounty",
"sanMateoCounty",
"contraCostaCounty",
"santaClaraCounty",
"employs100OrFewer",
"employs500OrFewer",
"employs750OrFewer",
"employs750More",
"hasInterest",
"doesNotHaveInterest",
"covid19",
"protestDamage",
"blackOwned",
"lgbtq",
"public",
"private",
"spanish",
"chinese"
];

const defaultMatchCounts = () => {
const matchCounts = {} as {[key: string]: number};
for (const filter of allFilters) matchCounts[filter] = 0;
return matchCounts;
};

export const getMatchCounts = (filteredResults: Result[]) =>
filteredResults.reduce((matchCounts, result) => {
for (const filter of allFilters) {
if (result[filter as keyof Result]) {
matchCounts[filter]++;
}
}
return matchCounts;
}, defaultMatchCounts());

type FilterGroup = {
groupName: string;
groupLabel: string;
filters: {
name: string;
label: string;
}[];
};

export const filterGroups: FilterGroup[] = [
{
groupName: "orgType",
Expand Down Expand Up @@ -197,23 +157,3 @@ export const getFilterNameFromGroupAndTargetName = (
}
return undefined;
};


export const getFilterNameFromGroupAndLabel = (
groupName: string,
label: string
): string | undefined => {
const foundGroup = filterGroups.find(
(group) => group.groupName === groupName
);
if (foundGroup) {
const foundFilter = foundGroup.filters.find(
(filter) => filter.label === label
);
if (foundFilter) {

return foundFilter.name;
}
}
return undefined;
};

0 comments on commit 37a4c8e

Please sign in to comment.