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

🐛 fix duplicated parameter vocabs #198

Merged
merged 1 commit into from
Oct 25, 2024
Merged
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
56 changes: 27 additions & 29 deletions src/components/common/filters/ParameterVocabFilter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, {
Dispatch,
SetStateAction,
} from "react";
import { Grid, SxProps, Theme } from "@mui/material";
import { Box, SxProps, Theme } from "@mui/material";
import { useSelector } from "react-redux";
import { RootState } from "../store/store";
import { fetchParameterVocabsWithStore } from "../store/searchReducer";
Expand Down Expand Up @@ -46,8 +46,8 @@ const ParameterVocabFilter: FC<ParameterVocabFilterProps> = ({
// Update the local filter state using the setFilter
const handleChange = useCallback(
(_: React.MouseEvent<HTMLElement>, newAlignment: string[]) => {
const selected: Vocab[] = parameterVocabs.filter((c) =>
newAlignment.includes(c.label)
const selected: Vocab[] = parameterVocabs.filter((vocab) =>
newAlignment.includes(vocab.label)
);
setFilter((prevFilter) => ({
...prevFilter,
Expand All @@ -57,45 +57,43 @@ const ParameterVocabFilter: FC<ParameterVocabFilterProps> = ({
[parameterVocabs, setFilter]
);

// Remove all items whose label is already in the labels array
useEffect(() => {
const labels = [...new Set(parameterVocabs.map((c) => c.label))];
setButtonLabels(labels);
}, [parameterVocabs]);

useEffect(() => {
dispatch(fetchParameterVocabsWithStore(null))
.unwrap()
.then((vocabs: Array<Vocab>) => {
// we only want second level of vocabs
const secondLevelVocabs = vocabs
.flatMap((rootVocab) => rootVocab.narrower)
.filter((vocab) => vocab !== undefined)
.sort((a, b) => (a.label < b.label ? -1 : a.label > b.label ? 1 : 0));
// Extract second level vocabs and remove duplicates
const secondLevelVocabs = Array.from(
new Set(
vocabs
.flatMap((rootVocab) => rootVocab.narrower || [])
.map((vocab) => JSON.stringify(vocab)) // Convert to string for Set deduplication
)
)
.map((vocabString) => JSON.parse(vocabString)) // Convert back to object
.sort((a, b) => a.label.localeCompare(b.label)); // localeCompare() is better for handling of international characters eg. non-English text

setParameterVocabs(secondLevelVocabs);
setButtonLabels(secondLevelVocabs.map((vocab) => vocab.label));
})
.catch((error) =>
console.error("Error fetching parameterVocabs:", error)
);
}, [dispatch]);

return (
<Grid container sx={{ ...sx }}>
<Grid item xs={12}>
<StyledToggleButtonGroup
value={filter.parameterVocabs?.map((vocab) => vocab.label) || []}
onChange={handleChange}
aria-label="parameter vocab selection"
>
{buttonLabels.map((label) => (
<StyledToggleButton value={label} key={label} aria-label={label}>
{label}
</StyledToggleButton>
))}
</StyledToggleButtonGroup>
</Grid>
</Grid>
<Box sx={{ ...sx }}>
<StyledToggleButtonGroup
value={filter.parameterVocabs?.map((vocab) => vocab.label) || []}
onChange={handleChange}
aria-label="parameter vocab selection"
>
{buttonLabels.map((label) => (
<StyledToggleButton value={label} key={label} aria-label={label}>
{label}
</StyledToggleButton>
))}
</StyledToggleButtonGroup>
</Box>
);
};

Expand Down
Loading