Skip to content

Commit

Permalink
Feat/notifications evenements essentiels 1413 (#1468)
Browse files Browse the repository at this point in the history
* feat(notif): fréquence mood

* fix(notif): retours PR

* feat(notifications): ajout gestion evenements essentiels

* fix: retours PR

Co-authored-by: Lebret Audrey <[email protected]>
  • Loading branch information
MorganeDe and alebret authored Oct 19, 2022
1 parent 59de7e7 commit dcd39c9
Show file tree
Hide file tree
Showing 9 changed files with 191 additions and 22 deletions.
2 changes: 2 additions & 0 deletions front/src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import NotificationHandler, {
setNotificationHandler,
} from "./notification/notificationHandler.component";
import NotificationPermissionInSettingsModal from "./notification/notificationPermissionInSettingsModal.component";
import NotificationsEssentialEvents from "./notification/notificationsEssentialEvents.component";
import NotificationsFrequency from "./notification/notificationsFrequency.component";
import NotificationToggle from "./notification/notificationToggle.component";
import CustomPagination from "./onboardingAndProfile/customOnboardingPagination.component";
Expand Down Expand Up @@ -87,6 +88,7 @@ export {
MoodsCalendar,
NotificationHandler,
NotificationPermissionInSettingsModal,
NotificationsEssentialEvents,
NotificationsFrequency,
NotificationToggle,
ParenthequeItem,
Expand Down
15 changes: 7 additions & 8 deletions front/src/components/menu/menu.component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,13 @@ const Menu: React.FC<Props> = ({ showMenu, setShowMenu }) => {
},
title: Labels.timeline.library.nom,
},
// TODO: feature-flipping => centre de notifications
// {
// icon: IcomoonIcons.notification,
// onPress: () => {
// void RootNavigation.navigate("notificationsCenter");
// },
// title: Labels.menu.notificationsCenter,
// },
{
icon: IcomoonIcons.notification,
onPress: () => {
void RootNavigation.navigate("notificationsCenter");
},
title: Labels.menu.notificationsCenter,
},
{
icon: IcomoonIcons.email,
onPress: () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from "../../utils";
import { NotificationType } from "../../utils/notifications/notification.util";
import * as NotificationUtils from "../../utils/notifications/notification.util";
import { NotificationsEssentialEvents } from "..";
import TrackerHandler from "../tracker/trackerHandler.component";
import NotificationsFrequency from "./notificationsFrequency.component";

Expand Down Expand Up @@ -65,6 +66,8 @@ const NotificationToggle: FC<Props> = ({
const showOptionByType = (_type: NotificationType): ReactElement => {
if (_type == NotificationType.moodboard)
return <NotificationsFrequency type={_type} />;
if (_type == NotificationType.event && events)
return <NotificationsEssentialEvents events={events} />;
return <View />;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
import type { FC } from "react";
import * as React from "react";
import { useCallback, useEffect, useState } from "react";
import { StyleSheet } from "react-native";
import { CheckBox } from "react-native-elements";

import { Labels, StorageKeysConstants } from "../../constants";
import { Colors, Sizes } from "../../styles";
import type { TrackerEvent } from "../../type";
import type { Event } from "../../types";
import {
EventUtils,
NotificationUtils,
StorageUtils,
TrackerUtils,
} from "../../utils";
import { NotificationType } from "../../utils/notifications/notification.util";
import { BaseAssets } from "../assets";
import TrackerHandler from "../tracker/trackerHandler.component";

interface Props {
events: Event[];
}

const NotificationsEssentialEvents: FC<Props> = ({ events }) => {
const [trackerEventObject, setTrackerEventObject] = useState<TrackerEvent>();
const [isCheckboxChecked, setIsCheckboxChecked] = useState(false);

const initCheckboxState = useCallback(async () => {
const areEssentialEventsChecked = (await StorageUtils.getObjectValue(
StorageKeysConstants.notifToggleEssentialEvents
)) as boolean;
setIsCheckboxChecked(areEssentialEventsChecked);
}, []);

const scheduleEssentialEvents = useCallback(
async (shouldScheduleEssentialEvents: boolean) => {
const essentialEvents: Event[] = EventUtils.essentialEvents(events);

await StorageUtils.storeObjectValue(
StorageKeysConstants.notifToggleEssentialEvents,
shouldScheduleEssentialEvents
);
if (shouldScheduleEssentialEvents) {
void NotificationUtils.cancelScheduleEventsNotification();
void NotificationUtils.scheduleEventsNotification(essentialEvents);
}
},
[events]
);

const onCheckboxPressed = useCallback(() => {
const shouldScheduleEssentialEvents = !isCheckboxChecked;
setIsCheckboxChecked(shouldScheduleEssentialEvents);

void scheduleEssentialEvents(shouldScheduleEssentialEvents);
setTrackerEventObject({
action: TrackerUtils.TrackingEvent.NOTIFICATIONS_CENTER,
name: `${TrackerUtils.TrackingEvent.NOTIFICATIONS_CENTER} : ${NotificationType.event} essentiels`,
});
}, [isCheckboxChecked, scheduleEssentialEvents]);

useEffect(() => {
void initCheckboxState();
}, [initCheckboxState]);

return (
<>
<TrackerHandler eventObject={trackerEventObject} />
<CheckBox
containerStyle={styles.checkboxItem}
textStyle={{
color: Colors.primaryBlueDark,
fontWeight: isCheckboxChecked ? "bold" : "normal",
}}
key={1}
iconRight={false}
uncheckedIcon={
<BaseAssets.CheckboxUncheckedIcon
width={Sizes.sm}
height={Sizes.sm}
/>
}
checkedIcon={
<BaseAssets.CheckboxCheckedIcon width={Sizes.sm} height={Sizes.sm} />
}
title={Labels.notification.essentialEvents}
accessibilityLabel={Labels.notification.essentialEvents}
checked={isCheckboxChecked}
onPress={onCheckboxPressed}
/>
</>
);
};

const styles = StyleSheet.create({
checkboxItem: {
backgroundColor: "transparent",
borderColor: "transparent",
marginStart: 0,
minHeight: Sizes.accessibilityMinButton,
paddingStart: 0,
},
});

export default NotificationsEssentialEvents;
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const NotificationsFrequency: FC<Props> = ({ type }) => {
setRadioValue(frequency);
}, []);

const saveFraquency = useCallback(
const saveFrequency = useCallback(
async (frequency: NotificationUtils.Frequencies) => {
await StorageUtils.storeStringValue(
StorageKeys.notifToggleMoodboardFrequency,
Expand All @@ -52,13 +52,13 @@ const NotificationsFrequency: FC<Props> = ({ type }) => {
const newValue = value as NotificationUtils.Frequencies;
setRadioValue(newValue);

void saveFraquency(newValue);
void saveFrequency(newValue);
setTrackerEventObject({
action: TrackerUtils.TrackingEvent.NOTIFICATIONS_CENTER,
name: `${TrackerUtils.TrackingEvent.NOTIFICATIONS_CENTER} : ${type} & ${newValue}`,
});
},
[saveFraquency, type]
[saveFrequency, type]
);

const radioButtonFrequency = (
Expand Down
1 change: 1 addition & 0 deletions front/src/constants/Labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,7 @@ export default {
},
noData: "Aucunes données",
notification: {
essentialEvents: "Que les événements essentiels",
frequency: {
onceADay: "1 fois par jour",
question: "À quelle fréquence ?",
Expand Down
2 changes: 2 additions & 0 deletions front/src/constants/storageKeys.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const notifToggleArticles = "@notifToggleArticles";
export const notifToggleMoodboard = "@notifToggleMoodboard";
export const notifToggleMoodboardFrequency = "@notifToggleMoodboardFrequency";
export const notifToggleEvents = "@notifToggleEvents";
export const notifToggleEssentialEvents = "@notifToggleEssentialEvents";
export const eventsCalcFromBirthday = "@eventsCalcFromBirthday";
export const forceToScheduleEventsNotif = "@forceToScheduleEventsNotif";
export const osCalendarId = "@osCalendarId";
Expand Down Expand Up @@ -61,6 +62,7 @@ export const allStorageKeys = [
notifToggleMoodboard,
notifToggleMoodboardFrequency,
notifToggleEvents,
notifToggleEssentialEvents,
eventsCalcFromBirthday,
forceToScheduleEventsNotif,
osCalendarId,
Expand Down
5 changes: 5 additions & 0 deletions front/src/utils/events/event.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ export const formattedEvents = (
}));
else return [];
};

export const essentialEvents = (events: Event[]): Event[] =>
events.filter((event) => {
return event.important;
});
73 changes: 62 additions & 11 deletions front/src/utils/events/events.util.test.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,92 @@
import type { Event } from "../../types";
import { formattedEvents } from "./event.util";
import { EventUtils } from "..";

describe("Event utils", () => {
describe("formattedEvents function", () => {
const childBirthday = "2022-12-08";
const event1: Event = {
const events: Event[] = [
{
debut: 0,
fin: 8,
id: 1,
nom: "événement",
};
important: true,
nom: "événement1",
},
{
debut: 0,
fin: 8,
id: 2,
important: false,
nom: "événement2",
},
];

describe("formattedEvents function", () => {
const childBirthday = "2022-12-08";

it("should get events formatted with no events", () => {
const result = formattedEvents([], childBirthday);
const result = EventUtils.formattedEvents([], childBirthday);
const expected: Event[] = [];
expect(result).toEqual(expected);
});

it("should get events formatted witout childBirthday", () => {
// TODO: je renvoie an array vide, mais pas spur que ce soit la meilleure des solutions
const result = formattedEvents([event1], "");
// TODO: je renvoie an array vide, mais pas sûr que ce soit la meilleure des solutions
const result = EventUtils.formattedEvents(events, "");
const expected: Event[] = [];
expect(result).toEqual(expected);
});

it("should get events formatted with childBirthday", () => {
const result = formattedEvents([event1], childBirthday);
const result = EventUtils.formattedEvents(events, childBirthday);
const expected: Event[] = [
{
date: childBirthday,
debut: 0,
fin: 8,
id: 1,
nom: "événement",
important: true,
nom: "événement1",
},
{
date: childBirthday,
debut: 0,
fin: 8,
id: 2,
important: false,
nom: "événement2",
},
];
expect(result).toEqual(expected);
});
});

describe("essentialEvents", () => {
it("should return empty array when there are no events", () => {
expect(EventUtils.essentialEvents([])).toEqual([]);
});

it("should return empty array when there are no essential events", () => {
const noEssentialEvents: Event[] = [
{
debut: 0,
fin: 8,
id: 2,
important: false,
nom: "événement2",
},
];
expect(EventUtils.essentialEvents(noEssentialEvents)).toEqual([]);
});

it("should return essential events", () => {
expect(EventUtils.essentialEvents(events)).toEqual([
{
debut: 0,
fin: 8,
id: 1,
important: true,
nom: "événement1",
},
]);
});
});
});

0 comments on commit dcd39c9

Please sign in to comment.