Skip to content

Commit

Permalink
Use sortedByOrder for order activities
Browse files Browse the repository at this point in the history
  • Loading branch information
dtrucs committed Nov 29, 2023
1 parent f1e95fb commit f046c83
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 32 deletions.
54 changes: 32 additions & 22 deletions frontend/src/modules/activities/adapter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { PRACTICE_ID } from 'modules/filters/constant';
import { FilterWithoutType } from 'modules/filters/interface';
import { sortedByOrder } from 'modules/utils/array';
import { Activity, ActivityChoices, ActivityFilter, RawListActivity } from './interface';

const isCompleteRawListActivity = (
Expand All @@ -13,11 +14,14 @@ export const adaptActivityFilter = (
rawActivities: Partial<RawListActivity>[],
): FilterWithoutType => ({
id: PRACTICE_ID,
options: rawActivities.filter(isCompleteRawListActivity).map(rawActivity => ({
value: `${rawActivity.id}`,
label: rawActivity.name,
pictogramUrl: rawActivity.pictogram,
})),
options: rawActivities
.filter(isCompleteRawListActivity)
.sort(sortedByOrder)
.map(rawActivity => ({
value: `${rawActivity.id}`,
label: rawActivity.name,
pictogramUrl: rawActivity.pictogram,
})),
});

export const adaptActivity = (rawActivity: RawListActivity): Activity => ({
Expand All @@ -27,24 +31,30 @@ export const adaptActivity = (rawActivity: RawListActivity): Activity => ({
});

export const adaptActivities = (rawActivities: Partial<RawListActivity>[]): ActivityChoices =>
rawActivities.filter(isCompleteRawListActivity).reduce(
(activities, { name, pictogram, id }) => ({
...activities,
[id]: {
label: name,
pictogramUri: pictogram,
},
}),
{} as ActivityChoices,
);
rawActivities
.filter(isCompleteRawListActivity)
.sort(sortedByOrder)
.reduce(
(activities, { name, pictogram, id }) => ({
...activities,
[id]: {
label: name,
pictogramUri: pictogram,
},
}),
{} as ActivityChoices,
);

export const adaptActivitiesFilter = (
rawActivities: Partial<RawListActivity>[],
): ActivityFilter[] =>
rawActivities.filter(isCompleteRawListActivity).map(({ name, pictogram, id, order = null }) => ({
label: name,
pictogramUri: pictogram,
id: `${id}`,
order,
type: 'PRACTICE',
}));
rawActivities
.filter(isCompleteRawListActivity)
.sort(sortedByOrder)
.map(({ name, pictogram, id, order = null }) => ({
label: name,
pictogramUri: pictogram,
id: `${id}`,
order,
type: 'PRACTICE',
}));
14 changes: 5 additions & 9 deletions frontend/src/modules/activities/connector.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { FilterWithoutType } from 'modules/filters/interface';
import { adaptTouristicContentCategoryList } from 'modules/touristicContentCategory/adapter';
import { fetchTouristicContentCategories } from 'modules/touristicContentCategory/api';
import { sortedByOrder } from 'modules/utils/array';
import { adaptOutdoorPracticesForActivities } from '../outdoorPractice/adapter';
import { fetchOutdoorPractices } from '../outdoorPractice/api';
import { adaptTouristicEventTypesForActivities } from '../touristicEventType/adapter';
Expand Down Expand Up @@ -37,9 +38,6 @@ export const getActivity = async (
return adaptActivity(rawActivity);
};

const sortedActivitiesByOrder = (a: ActivityFilter, b: ActivityFilter) =>
(a.order ?? Infinity) - (b.order ?? Infinity);

export const getActivityBarContent = async (language: string): Promise<ActivityFilter[]> => {
const [rawPractices, rawTouristicContentCategories, rawOutdoorPractices, rawTouristicEvents] =
await Promise.all([
Expand All @@ -50,15 +48,13 @@ export const getActivityBarContent = async (language: string): Promise<ActivityF
]);

return [
...adaptActivitiesFilter(rawPractices.results).sort(sortedActivitiesByOrder),
...adaptActivitiesFilter(rawPractices.results).sort(sortedByOrder),
...adaptOutdoorPracticesForActivities(
rawOutdoorPractices ? rawOutdoorPractices.results : [],
).sort(sortedActivitiesByOrder),
...adaptTouristicContentCategoryList(rawTouristicContentCategories.results).sort(
sortedActivitiesByOrder,
),
).sort(sortedByOrder),
...adaptTouristicContentCategoryList(rawTouristicContentCategories.results).sort(sortedByOrder),
...adaptTouristicEventTypesForActivities(
rawTouristicEvents ? rawTouristicEvents.results : [],
).sort(sortedActivitiesByOrder),
).sort(sortedByOrder),
];
};
2 changes: 1 addition & 1 deletion frontend/src/modules/activities/interface.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export interface RawActivity {
id: number;
order: null;
order: null | number;
pictogram: string;
}

Expand Down

0 comments on commit f046c83

Please sign in to comment.