Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App notifications fix #512

Merged
merged 3 commits into from
Oct 12, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -48,31 +48,45 @@ 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)
console.log(notificationEvents[level])
return Object.keys(notificationEvents).length > 0 &&
notificationEvents.hasOwnProperty(level)
? (notificationEvents[level] as boolean)
: DEFAULT_ALERT_PERCENTAGES[level]
},
[notifications],
[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 +103,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