Skip to content

Commit

Permalink
fix comments
Browse files Browse the repository at this point in the history
  • Loading branch information
TalMalka123 committed Nov 2, 2023
1 parent 0996712 commit 12da6ca
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 29 deletions.
37 changes: 9 additions & 28 deletions src/components/elements/controlledDisplayFiltersGroup/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Box, Divider } from '@mui/material';
import { DisplayFilter } from '../displayFilter';
import React from 'react';
import { LabeledSwitch } from '../../forms';
import _ from 'lodash';

export interface DisplayFilterPartialProps {
label: string;
Expand All @@ -11,16 +12,14 @@ export interface DisplayFilterPartialProps {

export interface ControlledDisplayFiltersGroupProps {
filters: DisplayFilterPartialProps[];
label?: string;
toggleAllLabel?: string;
toggleShowAll: (show: boolean) => void;
onFilterChange: (name: string, show: boolean) => void;
}

export function ControlledDisplayFiltersGroup({
filters,
onFilterChange,
toggleShowAll,
label
toggleAllLabel
}: ControlledDisplayFiltersGroupProps) {
const [showAll, setShowAll] = useState<boolean>(false);
const [displayedFilters, setDisplayedFilters] = useState<Set<string>>(() => new Set<string>());
Expand All @@ -38,38 +37,19 @@ export function ControlledDisplayFiltersGroup({
toggleShowAll(!showAll);
};

function setsAreEqual(setA:Set<string>, setB:Set<string>) {
if (setA.size !== setB.size) {
return false;
}
for (const item of setA) {
if (!setB.has(item)) {
return false;
}
}
return true;
}

useEffect(() => {
// When metadataToDisplay changes, check if it's equal to availableFiltersNames
if (setsAreEqual(displayedFilters, availableFiltersNames)) {
if (_.isEqual(displayedFilters, availableFiltersNames)) {
setShowAll(true);
} else {
setShowAll(false);
}
}, [displayedFilters, availableFiltersNames]);

const updateFilter = (item: DisplayFilterPartialProps, show: boolean) => {
if(item.onChange){
item.onChange(show)
}
onFilterChange(item.label, show);
};

return (
<Box sx={{ backgroundColor: 'rgba(248, 250, 252, 1)' }}>
<Box>
<LabeledSwitch label={label} onChange={toggleAll} checked={showAll}/>
<LabeledSwitch label={toggleAllLabel} onChange={toggleAll} checked={showAll}/>
</Box>
{filters.map((item) => {
return (
Expand All @@ -85,9 +65,10 @@ export function ControlledDisplayFiltersGroup({
updatedFilters.add(item.label);
}
setDisplayedFilters(updatedFilters);

updateFilter(item, show)
}}
if(item.onChange)
item.onChange(show)
}
}
/>
<Divider sx={{ backgroundColor: '#F8FAFC' }} />
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ ControlledDisplayFilterBasic.args = {
],
onFilterChange: (value) => console.log('show all', value),
toggleShowAll: (value) => console.log('toggle show all', value),
label: 'Show all'
toggleAllLabel: 'Show all'
};

0 comments on commit 12da6ca

Please sign in to comment.