Skip to content

Commit 746c1f7

Browse files
authored
fix: 🐛 Corrige un problème de refresh sur le moodboard (#1428)
* fix: 🐛 Corrige un problème de refresh sur le moodboard ✅ Closes: #1427 * refactor: 💡 Rename interface MoodsCalendarHandle
1 parent aeb5c1e commit 746c1f7

File tree

4 files changed

+46
-14
lines changed

4 files changed

+46
-14
lines changed

front/src/components/moodboard/moodsCalendar.component.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
import * as React from "react";
2-
import { useCallback, useEffect, useState } from "react";
2+
import {
3+
forwardRef,
4+
useCallback,
5+
useEffect,
6+
useImperativeHandle,
7+
useRef,
8+
useState,
9+
} from "react";
310
import { AccessibilityInfo, StyleSheet, View } from "react-native";
411
import type { DateData } from "react-native-calendars";
512
import { Calendar, LocaleConfig } from "react-native-calendars";
@@ -8,17 +15,26 @@ import type { Direction, Theme } from "react-native-calendars/src/types";
815
import { Labels, StorageKeysConstants } from "../../constants";
916
import { Colors, FontWeight, Margins, Paddings, Sizes } from "../../styles";
1017
import type { MoodStorageItem } from "../../type";
18+
import type { MarkedDatesType } from "../../types";
1119
import { MoodboardUtils, StorageUtils } from "../../utils";
1220
import { Icomoon, IcomoonIcons, SecondaryText } from "../baseComponents";
1321
import EditMoodDay from "./editMoodDay.component";
1422

1523
const CALENDAR_MONTH_FORMAT = "MMMM yyyy";
1624

17-
const MoodsCalendar: React.FC = () => {
25+
interface RefreshMoodCalendar {
26+
refresh: () => void;
27+
}
28+
29+
const MoodsCalendar: React.ForwardRefRenderFunction<RefreshMoodCalendar> = (
30+
props,
31+
forwardedRef
32+
) => {
1833
const [moods, setMoods] = useState<MoodStorageItem[]>();
1934
const [showEditModal, setShowEditModal] = useState(false);
2035
const [dateToEdit, setDateToEdit] = useState<string>();
2136
const [monthToDisplay, setMonthToDisplay] = useState(new Date().getMonth());
37+
const calendarRef = useRef<Calendar>(null);
2238

2339
const calenderTheme: Theme = {
2440
arrowColor: Colors.primaryBlue,
@@ -28,16 +44,23 @@ const MoodsCalendar: React.FC = () => {
2844
textMonthFontSize: Sizes.xs,
2945
};
3046

31-
const findMoods = async () => {
47+
useImperativeHandle(forwardedRef, () => ({
48+
async refresh() {
49+
await findMoods();
50+
},
51+
}));
52+
53+
const findMoods = useCallback(async () => {
3254
const moodsStorage: MoodStorageItem[] =
3355
(await StorageUtils.getObjectValue(StorageKeysConstants.moodsByDate)) ??
3456
[];
3557

3658
setMoods(moodsStorage);
37-
};
59+
}, []);
3860

3961
useEffect(() => {
4062
void findMoods();
63+
// eslint-disable-next-line react-hooks/exhaustive-deps
4164
}, []);
4265

4366
const onDayPress = useCallback((day: DateData) => {
@@ -93,10 +116,10 @@ const MoodsCalendar: React.FC = () => {
93116
);
94117
}, []);
95118

96-
const hideEditModal = useCallback(() => {
119+
const hideEditModal = useCallback(async () => {
97120
setShowEditModal(false);
98-
void findMoods();
99-
}, []);
121+
await findMoods();
122+
}, [findMoods]);
100123

101124
return (
102125
<>
@@ -118,6 +141,7 @@ const MoodsCalendar: React.FC = () => {
118141
markedDates={buildMarkedDatesForCalendar(moods)}
119142
renderArrow={renderMonthArrow}
120143
onMonthChange={onMonthChange}
144+
ref={calendarRef}
121145
/>
122146

123147
<EditMoodDay
@@ -128,11 +152,12 @@ const MoodsCalendar: React.FC = () => {
128152
</>
129153
);
130154
};
155+
MoodsCalendar.displayName = "MoodsCalendar";
131156

132157
export const buildMarkedDatesForCalendar = (
133158
moods: MoodStorageItem[] | undefined
134-
) => {
135-
const markedList = {};
159+
): MarkedDatesType | undefined => {
160+
const markedList: MarkedDatesType = {};
136161

137162
moods?.forEach((item) => {
138163
const moodItem = MoodboardUtils.MOODBOARD_ITEMS.find(
@@ -167,4 +192,4 @@ const styles = StyleSheet.create({
167192
},
168193
});
169194

170-
export default MoodsCalendar;
195+
export default forwardRef(MoodsCalendar);

front/src/screens/moodboard/moodboard.component.tsx

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import type { RootStackParamList } from "../../types";
2929
import { MoodboardUtils, TrackerUtils } from "../../utils";
3030
import { getObjectValue } from "../../utils/storage.util";
3131

32+
type MoodsCalendarRef = React.ElementRef<typeof MoodsCalendar>;
33+
3234
const firstItemIndexToShow = 1;
3335

3436
interface Props {
@@ -42,6 +44,7 @@ const Moodboard: FC<Props> = ({ navigation }) => {
4244
const [dismissAnimation, setDismissAnimation] = useState<unknown>(null);
4345
const isAccessibilityMode = useAccessibilityReader();
4446
const elementRef = useRef<Animatable.View & View>(null);
47+
const moodsCalendarRef = useRef<MoodsCalendarRef>(null);
4548

4649
const onViewLayout = useCallback((event: LayoutChangeEvent) => {
4750
const { layout } = event.nativeEvent;
@@ -77,12 +80,13 @@ const Moodboard: FC<Props> = ({ navigation }) => {
7780
navigation.goBack();
7881
}, [navigation]);
7982

80-
const validate = useCallback(() => {
81-
void MoodboardUtils.saveMood(
83+
const validate = useCallback(async () => {
84+
await MoodboardUtils.saveMood(
8285
MoodboardUtils.MOODBOARD_ITEMS[activeIndex].title
8386
);
8487
setTrackerAction(MoodboardUtils.MOODBOARD_ITEMS[activeIndex].title);
8588
hideCarouselChoice();
89+
moodsCalendarRef.current?.refresh();
8690
}, [activeIndex, hideCarouselChoice]);
8791

8892
return (
@@ -130,7 +134,7 @@ const Moodboard: FC<Props> = ({ navigation }) => {
130134
</Animatable.View>
131135
) : null}
132136

133-
<MoodsCalendar />
137+
<MoodsCalendar ref={moodsCalendarRef} />
134138
</ScrollView>
135139
);
136140
};

front/src/types.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import type { StackNavigationProp } from "@react-navigation/stack";
22
import type { PoiType } from "@socialgouv/nos1000jours-lib";
33
import type { FC, ReactNode } from "react";
44
import type React from "react";
5+
import type { MarkingProps } from "react-native-calendars/src/calendar/day/marking";
56
import type { LatLng } from "react-native-maps";
67

78
/* eslint-disable @typescript-eslint/consistent-type-definitions */
@@ -247,3 +248,5 @@ export type ArticleListHeaderParams = {
247248
export type Config = {
248249
lastAppVersionNumber: string | null;
249250
};
251+
252+
export type MarkedDatesType = Record<string, MarkingProps>;

front/src/utils/moodboard/moodboard.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,5 +44,5 @@ export const saveMood = async (
4444
title: mood,
4545
});
4646

47-
void storeObjectValue(StorageKeysConstants.moodsByDate, newMoods);
47+
await storeObjectValue(StorageKeysConstants.moodsByDate, newMoods);
4848
};

0 commit comments

Comments
 (0)