-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat/notifications evenements essentiels 1413 (#1468)
* 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
Showing
9 changed files
with
191 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
106 changes: 106 additions & 0 deletions
106
front/src/components/notification/notificationsEssentialEvents.component.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}, | ||
]); | ||
}); | ||
}); | ||
}); |