From cfdf5ebc98fd323101952455068868a27ebbbb39 Mon Sep 17 00:00:00 2001 From: Arnaud AMBROSELLI Date: Tue, 3 Oct 2023 22:25:07 +0200 Subject: [PATCH] fix: sorting when necessary --- dashboard/src/components/ActionModal.js | 4 +++- dashboard/src/components/DataLoader.js | 9 ++++++--- dashboard/src/scenes/person/Places.js | 7 ++++++- dashboard/src/scenes/report/view.js | 3 ++- dashboard/src/scenes/territory-observations/list.js | 8 +++++++- 5 files changed, 24 insertions(+), 7 deletions(-) diff --git a/dashboard/src/components/ActionModal.js b/dashboard/src/components/ActionModal.js index 971c67a34..7af701cae 100644 --- a/dashboard/src/components/ActionModal.js +++ b/dashboard/src/components/ActionModal.js @@ -612,7 +612,9 @@ function ActionContent({ onClose, action, personId = null, personIds = null, isM .filter(Boolean) .join(' ')}> ({ ...comment, type: 'action', person: action.person }))} + comments={action?.comments + .map((comment) => ({ ...comment, type: 'action', person: action.person })) + .sort((a, b) => new Date(b.date || b.createdAt) - new Date(a.date || a.createdAt))} color="main" canToggleUrgentCheck typeForNewComment="action" diff --git a/dashboard/src/components/DataLoader.js b/dashboard/src/components/DataLoader.js index 0c2282412..46ad7963b 100644 --- a/dashboard/src/components/DataLoader.js +++ b/dashboard/src/components/DataLoader.js @@ -211,7 +211,7 @@ export function useDataLoader(options = { refreshOnMount: false }) { const res = await API.get({ path: '/report', query: { ...query, page: String(page) } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setReports((items) => mergeItems(items, res.decryptedData)); + setReports((items) => mergeItems(items, res.decryptedData, { filterNewItemsFunction: (r) => !!r.team && !!r.date })); if (res.hasMore) return loadReports(page + 1); return true; } @@ -344,7 +344,7 @@ export function useDataLoader(options = { refreshOnMount: false }) { const res = await API.get({ path: '/consultation', query: { ...query, page: String(page), after: initialLoad ? 0 : lastLoadValue } }); if (!res.ok || !res.data.length) return resetLoaderOnError(); setProgress((p) => p + res.data.length); - setConsultations((items) => mergeItems(items, res.decryptedData, formatConsultation)); + setConsultations((items) => mergeItems(items, res.decryptedData, { formatNewItemsFunction: formatConsultation })); if (res.hasMore) return loadConsultations(page + 1); return true; } @@ -412,13 +412,16 @@ export function useDataLoader(options = { refreshOnMount: false }) { }; } -export function mergeItems(oldItems, newItems = [], formatNewItemsFunction) { +export function mergeItems(oldItems, newItems = [], { formatNewItemsFunction, filterNewItemsFunction } = {}) { const newItemsCleanedAndFormatted = []; const newItemIds = {}; for (const newItem of newItems) { newItemIds[newItem._id] = true; if (newItem.deletedAt) continue; + if (filterNewItemsFunction) { + if (!filterNewItemsFunction(newItem)) continue; + } if (formatNewItemsFunction) { newItemsCleanedAndFormatted.push(formatNewItemsFunction(newItem)); } else { diff --git a/dashboard/src/scenes/person/Places.js b/dashboard/src/scenes/person/Places.js index 1ed929490..6051c8839 100644 --- a/dashboard/src/scenes/person/Places.js +++ b/dashboard/src/scenes/person/Places.js @@ -40,6 +40,11 @@ const PersonPlaces = ({ person }) => { return personPlaces.length !== new Set(personPlaces).size; }, [person.relsPersonPlace]); + const sortedPlaces = useMemo(() => { + if (!person.relsPersonPlace?.length) return []; + return [...person.relsPersonPlace]?.sort((a, b) => new Date(b.createdAt) - new Date(a.createdAt)); + }, [person.relsPersonPlace]); + return ( <>
@@ -74,7 +79,7 @@ const PersonPlaces = ({ person }) => { - {person.relsPersonPlace?.map((relPersonPlace) => { + {sortedPlaces?.map((relPersonPlace) => { const { place: placeId, createdAt, user } = relPersonPlace; const place = places.find((p) => p._id === placeId); return ( diff --git a/dashboard/src/scenes/report/view.js b/dashboard/src/scenes/report/view.js index fd0dc0d8c..182a2866e 100644 --- a/dashboard/src/scenes/report/view.js +++ b/dashboard/src/scenes/report/view.js @@ -347,7 +347,8 @@ const View = () => { { referenceStartDay: dateString, referenceEndDay: dateString }, currentTeam?.nightSession ? 12 : 0 ); - }), + }) + .sort((a, b) => new Date(b.observedAt || b.createdAt) - new Date(a.observedAt || a.createdAt)), [dateString, selectedTeamsObject, territoryObservations] ); diff --git a/dashboard/src/scenes/territory-observations/list.js b/dashboard/src/scenes/territory-observations/list.js index ef88195e5..05e1f6804 100644 --- a/dashboard/src/scenes/territory-observations/list.js +++ b/dashboard/src/scenes/territory-observations/list.js @@ -15,7 +15,13 @@ const List = ({ territory = {} }) => { const [observation, setObservation] = useState({}); const [openObservationModale, setOpenObservationModale] = useState(null); - const observations = useMemo(() => territoryObservations.filter((obs) => obs.territory === territory._id), [territory._id, territoryObservations]); + const observations = useMemo( + () => + territoryObservations + .filter((obs) => obs.territory === territory._id) + .sort((a, b) => new Date(b.observedAt || b.createdAt) - new Date(a.observedAt || a.createdAt)), + [territory._id, territoryObservations] + ); if (!observations) return null;