Skip to content

Commit

Permalink
Merge branch 'stage' into hide-app-logs
Browse files Browse the repository at this point in the history
  • Loading branch information
RabeeAbuBaker authored Oct 12, 2023
2 parents efb779a + 79f5aa8 commit 5e49a3b
Showing 1 changed file with 25 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,43 @@ export default function AppNotificationsAlert({
}: NotificationsAlertFormProps) {
const { notifications } = app
const fetcher = useFetcher()
useActionNotification(fetcher.data)

const notificationEvents = notifications[0]?.appNotification?.events

const getNotificationCheckedState = useCallback(
(level: NotificationLevel) => {
// @ts-ignore
return Object.keys(notifications).length > 0 && notifications[level]
? // @ts-ignore
(notifications[level] as boolean)
: DEFAULT_ALERT_PERCENTAGES[level]
},
[notifications],
(level: NotificationLevel) =>
Object.keys(notificationEvents).length > 0 &&
notificationEvents.hasOwnProperty(level)
? (notificationEvents[level] as boolean)
: DEFAULT_ALERT_PERCENTAGES[level],
[notificationEvents],
)

const updateNotification = (level: string, value: string) => {
const otherNotificationEventsValues = NOTIFICATIONS_ALERT_LEVELS.filter(
(notificationLevel) => notificationLevel !== level,
).reduce(
(result, notificationLevel) => ({
...result,
[notificationLevel]: getNotificationCheckedState(notificationLevel)
? "on"
: "off",
}),
{},
)

fetcher.submit(
{
[level]: value,
...otherNotificationEventsValues,
},
{
method: "PUT",
method: "POST",
},
)
}

useActionNotification(fetcher.data)

return (
<Stack pt={36}>
<Text pb={16}>
Expand All @@ -89,10 +101,10 @@ export default function AppNotificationsAlert({
</Text>
<Switch
defaultChecked={getNotificationCheckedState(level)}
disabled={userRole === "MEMBER"}
disabled={userRole === "MEMBER" || fetcher.state !== "idle"}
name={level}
onChange={(event) => {
updateNotification(level, event.currentTarget.value)
updateNotification(level, event.currentTarget.checked ? "on" : "off")
trackEvent({
category: AnalyticCategories.app,
action: AnalyticActions.app_notifications,
Expand Down

0 comments on commit 5e49a3b

Please sign in to comment.