Skip to content

Commit

Permalink
Merge pull request #189 from Vizzuality/fix-filters
Browse files Browse the repository at this point in the history
Change how table filtering works + fix filters' select all and clear all buttons
  • Loading branch information
SARodrigues authored Feb 26, 2024
2 parents 52ba8fa + 1f4e9d9 commit a410636
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 25 deletions.
10 changes: 7 additions & 3 deletions frontend/src/components/filters-button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { useEffect, useMemo, useState } from 'react';

import { useForm } from 'react-hook-form';

import { isEqual } from 'lodash-es';
import { Filter } from 'lucide-react';

import { Button } from '@/components/ui/button';
Expand Down Expand Up @@ -30,7 +31,7 @@ const FiltersButton: React.FC<FiltersButtonProps> = ({
field,
options,
values,
headerButtons = false,
headerButtons = true,
showNoFiltersError = false,
onChange,
}) => {
Expand All @@ -51,6 +52,11 @@ const FiltersButton: React.FC<FiltersButtonProps> = ({
setValue('filters', values);
}, [setValue, values]);

useEffect(() => {
if (isEqual(filters, values)) return;
onChange(field, filters);
}, [field, filters, values, onChange]);

const handleSelectAll = () => {
setValue('filters', allFilterValues);
};
Expand All @@ -63,11 +69,9 @@ const FiltersButton: React.FC<FiltersButtonProps> = ({
if (checked) {
const filtersValues = [...filters, type];
setValue('filters', filtersValues);
onChange(field, filtersValues);
} else {
const filtersValues = filters.filter((entry) => entry !== type);
setValue('filters', filtersValues);
onChange(field, filtersValues);
}
};

Expand Down
1 change: 1 addition & 0 deletions frontend/src/containers/map/content/details/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const applyFilters = (data, filters) => {
const filteredData = data.filter((item) => {
for (const key in filters) {
if (!filters[key].length) continue;
if (!filters[key].includes(item[key])) return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import Link from 'next/link';

import { ColumnDef } from '@tanstack/react-table';

import FiltersButton from '@/components/filters-button';
import { PAGES } from '@/constants/pages';
import HeaderItem from '@/containers/map/content/details/table/header-item';
import { cellFormatter } from '@/containers/map/content/details/table/helpers';
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,10 @@ const NationalHighseasTable: React.FC = () => {
);

const [filters, setFilters] = useState({
// ! This shouldn't be hardcoded. The setup needs to be able to work the same without any default filters here.
protectedAreaType: ['mpa', 'oecm'],
establishmentStage: [
'actively-managed',
'designated',
'designated-unimplemented',
'implemented',
'proposed-committed',
'unknown',
],
protectionLevel: ['fully-highly-protected', 'less-protected-unknown'],
fishingProtectionLevel: ['highly', 'less', 'moderately'],
protectedAreaType: [],
establishmentStage: [],
protectionLevel: [],
fishingProtectionLevel: [],
});

const handleOnFiltersChange = (field, values) => {
Expand Down

0 comments on commit a410636

Please sign in to comment.