Skip to content

Commit

Permalink
refactor(frontend): remove useFiltersNext; refactor the current useFi…
Browse files Browse the repository at this point in the history
…lters
  • Loading branch information
Falinor committed Nov 13, 2024
1 parent 4ff486c commit b9b66b1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 93 deletions.
42 changes: 5 additions & 37 deletions frontend/src/hooks/useFilters.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import { useMemo, useState } from 'react';
import housingSlice, {
initialHousingFilters,
initialHousingFilters
} from '../store/reducers/housingReducer';
import { HousingFilters } from '../models/HousingFilters';
import { TrackEventActions, TrackEventCategories } from '../models/TrackEvent';
import { useMatomo } from '@jonkoops/matomo-tracker-react';
import { useAppDispatch, useAppSelector } from './useStore';

interface FiltersOptions {
Expand All @@ -17,7 +15,6 @@ interface FiltersOptions {

export function useFilters(opts?: FiltersOptions) {
const dispatch = useAppDispatch();
const { trackEvent } = useMatomo();
const initialState = opts?.initialState ?? initialHousingFilters;

const storage = opts?.storage ?? 'store';
Expand All @@ -31,10 +28,6 @@ export function useFilters(opts?: FiltersOptions) {
const [filters, setFilters] =
storage === 'store' ? [store.filters, changeFilters] : state;

const establishment = useAppSelector(
(state) => state.authentication.authUser?.establishment,
);

const { filtersExpanded: expand } = useAppSelector((state) => state.housing);
const { expandFilters } = housingSlice.actions;

Expand All @@ -45,42 +38,17 @@ export function useFilters(opts?: FiltersOptions) {
function removeFilter(removed: HousingFilters) {
setFilters({
...filters,
...removed,
...removed
});
}

const length = useMemo<number>(() => Object.keys(filters).length, [filters]);

function onChange(changed: HousingFilters, filterLabel?: string): void {
function onChange(changed: HousingFilters): void {
setFilters({
...filters,
...changed,
...changed
});
if (filterLabel) {
trackNewFilter(changed, filterLabel);
}
}

function trackNewFilter(changedFilters: HousingFilters, filterLabel: string) {
const filterEntry = Object.entries(changedFilters)[0];
const prevFilterEntry = Object.entries(filters).find(
(_) => _[0] === filterEntry[0],
);
const filterValues = filterEntry[1] as Array<string>;
const prevFilterValues = prevFilterEntry
? (prevFilterEntry[1] as Array<string>)
: [];
const newValues = filterValues.filter
? filterValues.filter((_) => prevFilterValues?.indexOf(_) === -1)
: [];
if (newValues.length) {
trackEvent({
category: TrackEventCategories.Filter,
action: TrackEventActions.Filter(filterLabel),
name: newValues.toString(),
value: establishment?.siren,
});
}
}

function onReset(): void {
Expand All @@ -95,6 +63,6 @@ export function useFilters(opts?: FiltersOptions) {
removeFilter,
onChangeFilters: onChange,
onResetFilters: onReset,
setExpand,
setExpand
};
}
48 changes: 0 additions & 48 deletions frontend/src/hooks/useFiltersNext.ts

This file was deleted.

15 changes: 8 additions & 7 deletions frontend/src/views/Group/GroupView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ import {
useFindCampaignsQuery
} from '../../services/campaign.service';
import NotFoundView from '../NotFoundView';
import { useFilters } from '../../hooks/useFiltersNext';
import { useFilters } from '../../hooks/useFilters';

interface RouterState {
alert?: string;
}

function GroupView() {
const router = useHistory<RouterState | undefined>();
const { id } = useParams<{ id: string }>();

const { data: group, isLoading: isLoadingGroup } = useGetGroupQuery(id);
Expand All @@ -42,13 +41,14 @@ function GroupView() {

const {
filters,
expand,
removeFilters,
setExpand,
removeFilter: removeFilters,
setFilters: onChangeFilters,
resetFilters: onResetFilters
onResetFilters,
expand,
setExpand
} = useFilters({
initialFilters: {
storage: 'state',
initialState: {
groupIds: [id]
}
});
Expand All @@ -61,6 +61,7 @@ function GroupView() {

const { view } = useAppSelector((state) => state.housing);

const router = useHistory<RouterState | undefined>();
const alert = router.location.state?.alert ?? '';
const [removeGroup] = useRemoveGroupMutation();
async function onGroupRemove(): Promise<void> {
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/views/HousingList/HousingListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ const HousingListView = () => {
filters,
setExpand,
setFilters,
onChangeFilters,
onResetFilters,
removeFilter
} = useFilters({
storage: 'store',
initialState: initialHousingFilters
});

Expand Down Expand Up @@ -66,7 +68,7 @@ const HousingListView = () => {
<HousingListFiltersSidemenu
filters={filters}
expand={expand}
onChange={setFilters}
onChange={onChangeFilters}
onReset={onResetFilters}
onClose={() => setExpand(false)}
/>
Expand Down

0 comments on commit b9b66b1

Please sign in to comment.