Skip to content

Commit

Permalink
Merge pull request #1000 from MTES-MCT/fix-group-refresh
Browse files Browse the repository at this point in the history
Fix group refresh
  • Loading branch information
Falinor authored Nov 13, 2024
2 parents e9a8aac + 4f67472 commit bf353d9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 69 deletions.
1 change: 1 addition & 0 deletions .talismanrc
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ allowed_patterns:
- keyof
- \[key\]
- key=
- "key:"
version: "1.0"
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: 20 additions & 28 deletions frontend/src/views/Group/GroupView.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Alert from '@codegouvfr/react-dsfr/Alert';
import { useMatomo } from '@jonkoops/matomo-tracker-react';
import Grid from '@mui/material/Unstable_Grid2';
import { useEffect } from 'react';
import { useHistory, useParams } from 'react-router-dom';

import {
Expand All @@ -10,12 +10,7 @@ import {
} from '../../services/group.service';
import Group from '../../components/Group/Group';
import { useDocumentTitle } from '../../hooks/useDocumentTitle';
import { useFilters } from '../../hooks/useFilters';
import HousingListFiltersSidemenu from '../../components/HousingListFilters/HousingListFiltersSidemenu';
import {
TrackEventActions,
TrackEventCategories
} from '../../models/TrackEvent';
import HousingFiltersBadges from '../../components/HousingFiltersBadges/HousingFiltersBadges';
import HousingListMap from '../HousingList/HousingListMap';
import HousingListTabs from '../HousingList/HousingListTabs';
Expand All @@ -26,49 +21,46 @@ import authService from '../../services/auth.service';
import { GroupPayload } from '../../models/GroupPayload';
import AppSearchBar from '../../components/_app/AppSearchBar/AppSearchBar';
import { HousingDisplaySwitch } from '../../components/HousingDisplaySwitch/HousingDisplaySwitch';
import { useCampaignList } from '../../hooks/useCampaignList';
import { useCreateCampaignFromGroupMutation } from '../../services/campaign.service';
import {
useCreateCampaignFromGroupMutation,
useFindCampaignsQuery
} from '../../services/campaign.service';
import NotFoundView from '../NotFoundView';
import { useFilters } from '../../hooks/useFilters';

interface RouterState {
alert?: string;
}

function GroupView() {
const { id } = useParams<{ id: string }>();

const { data: group, isLoading: isLoadingGroup } = useGetGroupQuery(id);

useDocumentTitle(group ? `Groupe - ${group?.title}` : 'Page non trouvée');

const { trackEvent } = useMatomo();
const {
filters,
removeFilter: removeFilters,
setFilters,
expand,
removeFilter,
setExpand,
onChangeFilters,
onResetFilters
onResetFilters,
expand,
setExpand
} = useFilters({
storage: 'state',
initialState: {
groupIds: [id]
}
});

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

function searchWithQuery(query: string): void {
trackEvent({
category: TrackEventCategories.Group,
action: TrackEventActions.HousingList.Search,
name: query
});
useEffect(() => {
setFilters({
...filters,
query
groupIds: [id]
});
}
}, [setFilters, id]);

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

const router = useHistory<RouterState | undefined>();
const alert = router.location.state?.alert ?? '';
Expand Down Expand Up @@ -115,14 +107,14 @@ function GroupView() {
}
}

const campaigns = useCampaignList({
const { data: campaigns } = useFindCampaignsQuery({
filters: {
groupIds: [id]
}
});

if (isLoadingGroup) {
return <></>;
return null;
}

if (!group || !!group.archivedAt) {
Expand Down Expand Up @@ -163,7 +155,7 @@ function GroupView() {
<Grid container mb={1} spacing={2} xs={12}>
<Grid xs>
<AppSearchBar
onSearch={searchWithQuery}
onSearch={(query) => onChangeFilters({ query })}
initialQuery={filters.query}
placeholder="Rechercher (propriétaire, identifiant fiscal, ref. cadastrale...)"
/>
Expand All @@ -174,7 +166,7 @@ function GroupView() {
</Grid>

<Grid xs={12}>
<HousingFiltersBadges filters={filters} onChange={removeFilter} />
<HousingFiltersBadges filters={filters} onChange={removeFilters} />
</Grid>

{view === 'map' ? (
Expand Down
12 changes: 8 additions & 4 deletions frontend/src/views/HousingList/HousingListView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
TrackEventCategories
} from '../../models/TrackEvent';
import AppSearchBar from '../../components/_app/AppSearchBar/AppSearchBar';
import { useFilters } from '../../hooks/useFilters';
import { useDocumentTitle } from '../../hooks/useDocumentTitle';
import { useAppSelector } from '../../hooks/useStore';
import HousingListFiltersSidemenu from '../../components/HousingListFilters/HousingListFiltersSidemenu';
Expand All @@ -20,20 +19,25 @@ import HousingListMap from './HousingListMap';
import { HousingDisplaySwitch } from '../../components/HousingDisplaySwitch/HousingDisplaySwitch';
import HousingCreationModal from '../../components/modals/HousingCreationModal/HousingCreationModal';
import { useUser } from '../../hooks/useUser';
import { useFilters } from '../../hooks/useFilters';
import { initialHousingFilters } from '../../store/reducers/housingReducer';

const HousingListView = () => {
useDocumentTitle('Parc de logements');
const { trackEvent } = useMatomo();

const {
expand,
filters,
setExpand,
setFilters,
expand,
onChangeFilters,
onResetFilters,
setExpand,
removeFilter
} = useFilters();
} = useFilters({
storage: 'store',
initialState: initialHousingFilters
});

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

Expand Down

0 comments on commit bf353d9

Please sign in to comment.