Skip to content

Commit

Permalink
fix: sorting in app
Browse files Browse the repository at this point in the history
  • Loading branch information
Arnaud AMBROSELLI authored and arnaudambro committed Feb 1, 2024
1 parent 0e796af commit 228d2d7
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 12 deletions.
13 changes: 12 additions & 1 deletion app/src/components/BubbleRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,22 @@ 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,
right: 20,
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 }) => (
<Container urgent={urgent}>
<CaptionsContainer>
Expand All @@ -25,7 +34,9 @@ const BubbleRow = ({ onMorePress, caption, date, user, metaCaption, urgent, grou
<CreationDate>
{!!user && <UserName caption={metaCaption} id={user?._id || user} />}
{'\u000A'}
{new Date(date).getLocaleDateAndTime('fr')}

{/* show year if different than current year */}
{formatDateConditionally(date)}
</CreationDate>
</CaptionsContainer>
{!!onMorePress && (
Expand Down
31 changes: 25 additions & 6 deletions app/src/components/Loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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 }));
}
}
/*
Expand Down Expand Up @@ -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 }));
}
}

Expand Down
12 changes: 10 additions & 2 deletions app/src/recoil/selectors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)),
];
}, []);
};
Expand Down
16 changes: 13 additions & 3 deletions app/src/scenes/Persons/PersonSummary.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand Down Expand Up @@ -262,7 +271,7 @@ const PersonSummary = ({
/>
<SubList
label="Commentaires"
data={comments}
data={sortedComments}
renderItem={(comment) => (
<CommentRow
key={comment._id}
Expand Down Expand Up @@ -323,6 +332,7 @@ const PersonSummary = ({
return;
}

console.log('response', response);
setComments((comments) => [response.decryptedData, ...comments]);
await createReportAtDateIfNotExist(response.decryptedData.date);
}}
Expand All @@ -332,15 +342,15 @@ const PersonSummary = ({
<SubList
label="Rencontres"
onAdd={onAddRencontre}
data={rencontres}
data={sortedRencontres}
renderItem={(rencontre) => <RencontreRow key={rencontre._id} rencontre={rencontre} onUpdate={() => onUpdateRencontre(rencontre)} />}
ifEmpty="Pas de rencontres"
/>
)}
<SubList
label="Lieux fréquentés"
onAdd={onAddPlaceRequest}
data={relsPersonPlace}
data={sortedRelPersonPlace}
renderItem={(relPersonPlace, index) => {
const place = places.find((pl) => pl._id === relPersonPlace.place);
return <PlaceRow key={index} place={place} relPersonPlace={relPersonPlace} personDB={personDB} />;
Expand Down

0 comments on commit 228d2d7

Please sign in to comment.