-
Notifications
You must be signed in to change notification settings - Fork 3
feat(settings): add course notification preferences to dashboard settings #3960
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
Open
AhtishamShahid
wants to merge
2
commits into
main
Choose a base branch
from
feat/learn-notification-preferences
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
65 changes: 65 additions & 0 deletions
65
frontends/api/src/mitxonline/hooks/notificationPreferences/index.ts
This file contains hidden or 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,65 @@ | ||
| import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query" | ||
|
|
||
| import type { MutationHookOptions } from "../../../mutations/mutationMeta" | ||
| import mitxAxios from "../../axios" | ||
| import { | ||
| NOTIFICATION_PREFERENCES_URL, | ||
| notificationPreferencesKeys, | ||
| notificationPreferencesQueries, | ||
| } from "./queries" | ||
| import type { | ||
| NotificationPreferences, | ||
| NotificationPreferenceUpdate, | ||
| PreferenceConfig, | ||
| PreferenceGroup, | ||
| } from "./queries" | ||
|
|
||
| type UseNotificationPreferencesOptions = { | ||
| enabled?: boolean | ||
| } | ||
|
|
||
| const useNotificationPreferences = ({ | ||
| enabled = true, | ||
| }: UseNotificationPreferencesOptions = {}) => { | ||
| return useQuery({ | ||
| ...notificationPreferencesQueries.detail(), | ||
| enabled, | ||
| }) | ||
| } | ||
|
|
||
| /** | ||
| * Open edX updates one channel per request, so each toggle or cadence change is | ||
| * its own mutation. | ||
| * | ||
| * The response body is not enough to render from — the LMS fans a change to a | ||
| * grouped type out to several types — so we re-read on success rather than | ||
| * patching the cache. | ||
| */ | ||
| const useUpdateNotificationPreference = ({ | ||
| meta, | ||
| }: MutationHookOptions = {}) => { | ||
| const queryClient = useQueryClient() | ||
| return useMutation({ | ||
| mutationFn: (update: NotificationPreferenceUpdate) => | ||
| mitxAxios.put(NOTIFICATION_PREFERENCES_URL, update), | ||
| onSuccess: () => { | ||
| queryClient.invalidateQueries({ | ||
| queryKey: notificationPreferencesKeys.detail(), | ||
| }) | ||
| }, | ||
| meta, | ||
| }) | ||
| } | ||
|
|
||
| export { | ||
| useNotificationPreferences, | ||
| useUpdateNotificationPreference, | ||
| notificationPreferencesQueries, | ||
| notificationPreferencesKeys, | ||
| } | ||
| export type { | ||
| NotificationPreferences, | ||
| NotificationPreferenceUpdate, | ||
| PreferenceConfig, | ||
| PreferenceGroup, | ||
| } | ||
80 changes: 80 additions & 0 deletions
80
frontends/api/src/mitxonline/hooks/notificationPreferences/queries.ts
This file contains hidden or 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,80 @@ | ||
| import { queryOptions } from "@tanstack/react-query" | ||
|
|
||
| import mitxAxios from "../../axios" | ||
|
|
||
| /** | ||
| * MITx Online proxies these straight through from Open edX, which owns the | ||
| * state. The endpoint is excluded from MITx Online's OpenAPI schema, so there | ||
| * is no generated client for it and we call it through the configured axios | ||
| * instance instead. | ||
| */ | ||
| const NOTIFICATION_PREFERENCES_URL = "/api/notification-preferences/" | ||
|
|
||
| type NotificationChannel = "web" | "email" | "email_cadence" | ||
|
|
||
| type PreferenceConfig = { | ||
| web: boolean | ||
| push: boolean | ||
| email: boolean | ||
| email_cadence: string | ||
| info: string | ||
| } | ||
|
|
||
| type PreferenceGroup = { | ||
| enabled: boolean | ||
| /** | ||
| * Keyed by notification type -> the channels that type locks, e.g. | ||
| * `{ new_discussion_post: ["push"] }`. Not a flat list. | ||
| */ | ||
| non_editable: Record<string, string[]> | string[] | null | ||
| notification_types: Record<string, PreferenceConfig> | ||
| } | ||
|
|
||
| type NotificationPreferences = { | ||
| data?: Record<string, PreferenceGroup> | null | ||
| /** The LMS gates the whole feature with this. */ | ||
| show_preferences?: boolean | ||
| show_email_preferences?: boolean | ||
| } | ||
|
|
||
| type NotificationPreferenceUpdate = { | ||
| notification_app: string | ||
| notification_type: string | ||
| notification_channel: NotificationChannel | ||
| } & ({ value: boolean } | { email_cadence: string }) | ||
|
|
||
| const notificationPreferencesKeys = { | ||
| root: ["mitxonline", "notificationPreferences"], | ||
| detail: () => [...notificationPreferencesKeys.root, "detail"], | ||
| } | ||
|
|
||
| const notificationPreferencesQueries = { | ||
| detail: () => | ||
| queryOptions({ | ||
| queryKey: notificationPreferencesKeys.detail(), | ||
| queryFn: async (): Promise<NotificationPreferences> => { | ||
| return mitxAxios | ||
| .get(NOTIFICATION_PREFERENCES_URL) | ||
| .then((res) => res.data) | ||
| }, | ||
| /** | ||
| * A 409 means the learner has no Open edX account yet, and a 4xx will not | ||
| * start working by asking again. Retrying only delays the notice the | ||
| * section shows in its place. | ||
| */ | ||
| retry: false, | ||
| }), | ||
| } | ||
|
|
||
| export { | ||
| notificationPreferencesQueries, | ||
| notificationPreferencesKeys, | ||
| NOTIFICATION_PREFERENCES_URL, | ||
| } | ||
| export type { | ||
| NotificationChannel, | ||
| NotificationPreferences, | ||
| NotificationPreferenceUpdate, | ||
| PreferenceConfig, | ||
| PreferenceGroup, | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see that
onSuccesscallsinvalidateQuerieswithout returning its promise, so the mutation leavesisPendingas soon as the PUT returns, before the refetch lands. For that gap the row is enabled again but still shows the oldconfig. A second click then sends the same inverted value, which is the bug the pending guard was meant to prevent. The comment inNotificationPreferences.tsx:171-175says "disable them until the refetch lands", which isn't what happens yet. Returning the promise fixes it:onSuccess: () => queryClient.invalidateQueries({ queryKey: ... }). TanStack waits on a promise returned fromonSuccess, soisPendingthen covers the refetch. The existing test can't catch this because its PUT never resolves. A test where the PUT resolves but the GET is held would.