Skip to content

Commit

Permalink
fix: sorting when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI committed Oct 3, 2023
1 parent da818c5 commit cfdf5eb
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 7 deletions.
4 changes: 3 additions & 1 deletion dashboard/src/components/ActionModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -612,7 +612,9 @@ function ActionContent({ onClose, action, personId = null, personIds = null, isM
.filter(Boolean)
.join(' ')}>
<CommentsModule
comments={action?.comments.map((comment) => ({ ...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"
Expand Down
9 changes: 6 additions & 3 deletions dashboard/src/components/DataLoader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 {
Expand Down
7 changes: 6 additions & 1 deletion dashboard/src/scenes/person/Places.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
<div className="tw-my-10 tw-flex tw-items-center tw-gap-2">
Expand Down Expand Up @@ -74,7 +79,7 @@ const PersonPlaces = ({ person }) => {
</tr>
</thead>
<tbody className="small">
{person.relsPersonPlace?.map((relPersonPlace) => {
{sortedPlaces?.map((relPersonPlace) => {
const { place: placeId, createdAt, user } = relPersonPlace;
const place = places.find((p) => p._id === placeId);
return (
Expand Down
3 changes: 2 additions & 1 deletion dashboard/src/scenes/report/view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
);

Expand Down
8 changes: 7 additions & 1 deletion dashboard/src/scenes/territory-observations/list.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down

0 comments on commit cfdf5eb

Please sign in to comment.