From 228d2d75093288eba9550e8edaeb4655d0344eaf Mon Sep 17 00:00:00 2001 From: Arnaud AMBROSELLI Date: Thu, 1 Feb 2024 18:02:39 +0100 Subject: [PATCH] fix: sorting in app --- app/src/components/BubbleRow.js | 13 ++++++++++- app/src/components/Loader.js | 31 ++++++++++++++++++++----- app/src/recoil/selectors.js | 12 ++++++++-- app/src/scenes/Persons/PersonSummary.js | 16 ++++++++++--- 4 files changed, 60 insertions(+), 12 deletions(-) diff --git a/app/src/components/BubbleRow.js b/app/src/components/BubbleRow.js index 89269c73c..f8bd1a614 100644 --- a/app/src/components/BubbleRow.js +++ b/app/src/components/BubbleRow.js @@ -4,6 +4,7 @@ import { TouchableOpacity } from 'react-native'; import colors from '../utils/colors'; import { MyText } from './MyText'; import UserName from './UserName'; +import dayjs from 'dayjs'; const hitSlop = { top: 20, left: 20, @@ -11,6 +12,14 @@ const hitSlop = { bottom: 20, }; +function formatDateConditionally(date) { + const formatString = + dayjs(date).year() === dayjs().year() + ? 'dddd D MMMM à HH:mm' // Omit year if current year + : 'dddd D MMMM YYYY à HH:mm'; // Include year if not current year + + return dayjs(date).format(formatString); +} const BubbleRow = ({ onMorePress, caption, date, user, metaCaption, urgent, group, itemName, onItemNamePress }) => ( @@ -25,7 +34,9 @@ const BubbleRow = ({ onMorePress, caption, date, user, metaCaption, urgent, grou {!!user && } {'\u000A'} - {new Date(date).getLocaleDateAndTime('fr')} + + {/* show year if different than current year */} + {formatDateConditionally(date)} {!!onMorePress && ( diff --git a/app/src/components/Loader.js b/app/src/components/Loader.js index aa2d96916..bbb584fbf 100644 --- a/app/src/components/Loader.js +++ b/app/src/components/Loader.js @@ -55,14 +55,33 @@ export const refreshTriggerState = atom({ }, }); -export const mergeItems = (oldItems, newItems = []) => { +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 { + newItemsCleanedAndFormatted.push(newItem); + } } - const oldItemsPurged = oldItems.filter((item) => !newItemIds[item._id] && !item.deletedAt); - return [...oldItemsPurged, ...newItems.filter((item) => !item.deletedAt)]; -}; + + const oldItemsPurged = []; + for (const oldItem of oldItems) { + if (oldItem.deletedAt) continue; + if (!newItemIds[oldItem._id]) { + oldItemsPurged.push(oldItem); + } + } + + return [...oldItemsPurged, ...newItemsCleanedAndFormatted]; +} export const DataLoader = () => { const [lastRefresh, setLastRefresh] = useMMKVNumber(appCurrentCacheKey); @@ -199,7 +218,7 @@ export const DataLoader = () => { lastRefresh: initialLoad ? 0 : lastRefresh, // because we never save medical data in cache }); if (refreshedConsultations) { - setConsultations((oldConsultations) => mergeItems(oldConsultations, refreshedConsultations.map(formatConsultation))); + setConsultations((oldConsultations) => mergeItems(oldConsultations, refreshedConsultations, { formatNewItemsFunction: formatConsultation })); } } /* @@ -373,7 +392,7 @@ export const DataLoader = () => { lastRefresh, }); if (refreshedReports) { - setReports((oldReports) => mergeItems(oldReports, refreshedReports).filter((r) => !!r.team && !!r.date)); + setReports((oldReports) => mergeItems(oldReports, refreshedReports, { filterNewItemsFunction: (r) => !!r.team && !!r.date })); } } diff --git a/app/src/recoil/selectors.js b/app/src/recoil/selectors.js index 48d0d8698..a451c5b93 100644 --- a/app/src/recoil/selectors.js +++ b/app/src/recoil/selectors.js @@ -99,6 +99,8 @@ export const itemsGroupedByPersonSelector = selector({ const rencontres = get(rencontresState); const groups = get(groupsState); + console.log(JSON.stringify(comments, null, 2)); + for (const group of groups) { for (const person of group.persons) { if (!personsObject[person]) continue; @@ -137,9 +139,15 @@ export const itemsGroupedByPersonSelector = selector({ } } } - for (const comment of comments) { + for (const [index, comment] of Object.entries(comments)) { + // comment 9ff915fa-9e39-4f37-a760-f658345e52e4 + // person ffd41d5a-c273-4548-b94b-750354ff3aff if (!personsObject[comment.person]) continue; personsObject[comment.person].comments = personsObject[comment.person].comments || []; + if (comment._id === '9ff915fa-9e39-4f37-a760-f658345e52e4') { + console.log(`itemsGroupedByPersonSelector comment at index ${index}`); + console.log('already existing comments', personsObject[comment.person].comments.length); + } personsObject[comment.person].comments.push(comment); if (!!comment.group) { const group = personsObject[comment.person].group; @@ -311,7 +319,7 @@ const formatData = (data) => { return [ ...actions, { type: 'title', title: section.title, _id: section.title }, - ...section.data.sort((a, b) => dayjs(a.dueAt).diff(dayjs(b.dueAt))), + ...section.data.sort((a, b) => new Date(b.dueAt) - new Date(a.dueAt)), ]; }, []); }; diff --git a/app/src/scenes/Persons/PersonSummary.js b/app/src/scenes/Persons/PersonSummary.js index 870e4a868..dac923e2b 100644 --- a/app/src/scenes/Persons/PersonSummary.js +++ b/app/src/scenes/Persons/PersonSummary.js @@ -85,6 +85,15 @@ const PersonSummary = ({ const { actions, comments, rencontres, relsPersonPlace } = populatedPerson; const sortedActions = useMemo(() => [...(actions || [])].sort((p1, p2) => (p1.dueAt > p2.dueAt ? -1 : 1)), [actions]); + const sortedComments = useMemo( + () => [...(comments || [])].sort((c1, c2) => ((c1.date || c1.createdAt) > (c2.date || c2.createdAt) ? -1 : 1)), + [comments] + ); + const sortedRencontres = useMemo(() => [...(rencontres || [])].sort((r1, r2) => (r1.date > r2.date ? -1 : 1)), [rencontres]); + const sortedRelPersonPlace = useMemo( + () => [...(relsPersonPlace || [])].sort((r1, r2) => (r1.createdAt > r2.createdAt ? -1 : 1)), + [relsPersonPlace] + ); const allPlaces = useRecoilValue(placesState); const places = useMemo(() => { @@ -262,7 +271,7 @@ const PersonSummary = ({ /> ( [response.decryptedData, ...comments]); await createReportAtDateIfNotExist(response.decryptedData.date); }} @@ -332,7 +342,7 @@ const PersonSummary = ({ onUpdateRencontre(rencontre)} />} ifEmpty="Pas de rencontres" /> @@ -340,7 +350,7 @@ const PersonSummary = ({ { const place = places.find((pl) => pl._id === relPersonPlace.place); return ;