-
-
Notifications
You must be signed in to change notification settings - Fork 255
Add frontend for digest settings #3344
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
Merged
MonkeyDo
merged 8 commits into
metabrainz:metabrainz-notifications
from
fettuccinae:frontend
Sep 30, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
20e1441
Add NotificationSettings page
fettuccinae 163e7a5
Dont allow null values to be set as digest_age
fettuccinae e77a6e8
Replace digest settings in log statements with notification settings
fettuccinae a66e92b
Add more info about notification options and minor refactor
fettuccinae b813f98
Fix typo
fettuccinae 085c520
Use RouteLoader for intial fetch and Fix edge case
fettuccinae 3b42a02
Change label to Notifications
fettuccinae 5b6bb9e
Use set-notification-settings endpoint to update notification settings
fettuccinae 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
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
148 changes: 148 additions & 0 deletions
148
frontend/js/src/settings/notification-settings/NotificationSettings.tsx
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,148 @@ | ||
import * as React from "react"; | ||
import { toast } from "react-toastify"; | ||
import { Helmet } from "react-helmet"; | ||
import { useLoaderData } from "react-router"; | ||
|
||
import Switch from "../../components/Switch"; | ||
|
||
type NotificationPreferenceLoaderData = { | ||
digest: boolean; | ||
digest_age: number; | ||
}; | ||
|
||
export default function NotificationSettings() { | ||
const loaderData = useLoaderData() as NotificationPreferenceLoaderData; | ||
|
||
const [digestEnabled, setDigestEnabled] = React.useState(loaderData.digest); | ||
const [digestAge, setDigestAge] = React.useState(loaderData.digest_age); | ||
// change this when notifications API is added. | ||
const [notificationsEnabled, setNotificationsEnabled] = React.useState( | ||
loaderData.digest | ||
); | ||
const [saving, setSaving] = React.useState(false); | ||
|
||
const updateNotificationSettings = async (e: React.FormEvent) => { | ||
e.preventDefault(); | ||
setSaving(true); | ||
try { | ||
const response = await fetch("/settings/set-notification-settings/", { | ||
method: "POST", | ||
headers: { | ||
"Content-Type": "application/json", | ||
}, | ||
body: JSON.stringify({ | ||
digest: digestEnabled && notificationsEnabled, | ||
digest_age: digestAge, | ||
}), | ||
}); | ||
const responseData = await response.json(); | ||
if (!response.ok) { | ||
throw new Error(responseData.error); | ||
} | ||
toast.success("Notification settings saved successfully"); | ||
} catch (error) { | ||
toast.error(`Failed to save notification settings.${error}`); | ||
} finally { | ||
setSaving(false); | ||
} | ||
}; | ||
|
||
const hasNoChanges = | ||
// change this when notifications API is added. | ||
notificationsEnabled === loaderData.digest && | ||
digestEnabled === loaderData.digest && | ||
digestAge === loaderData.digest_age; | ||
|
||
const isDigestAgeValid = | ||
!notificationsEnabled || | ||
!digestEnabled || | ||
(notificationsEnabled && digestEnabled && digestAge); | ||
|
||
return ( | ||
<> | ||
<Helmet> | ||
<title>Notification Settings</title> | ||
</Helmet> | ||
<h2 className="page-title">Notification settings</h2> | ||
<p> | ||
We will always email you right away about{" "} | ||
<b>important account issues</b> (like problems with connected services). | ||
<br /> | ||
For everything else, you can choose to turn off notifications or receive | ||
them in a periodic digest. | ||
</p> | ||
<form onSubmit={updateNotificationSettings}> | ||
<Switch | ||
id="enable-notifications" | ||
checked={notificationsEnabled} | ||
onChange={(e) => setNotificationsEnabled(!notificationsEnabled)} | ||
value="notifications" | ||
switchLabel={ | ||
<span | ||
className={`text-brand ${ | ||
!notificationsEnabled ? "text-muted" : "" | ||
}`} | ||
> | ||
<span>Enable e-mail notifications</span> | ||
</span> | ||
} | ||
/> | ||
<details open={notificationsEnabled}> | ||
<summary | ||
className="mt-4" | ||
onClick={(e) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I added this because clicking on the summary would toggle the details's state. (Same thing happens in BrainzPlayer settings.) |
||
e.preventDefault(); | ||
}} | ||
> | ||
{!notificationsEnabled && ( | ||
<p className="alert alert-info mb-0 d-inline-block"> | ||
You will still receive important e-mails that relate to your | ||
account | ||
</p> | ||
)} | ||
</summary> | ||
<h3 className="mt-4">Digest options</h3> | ||
<p>Receive your notifications grouped into an e-mail</p> | ||
<Switch | ||
id="enable-digest" | ||
value="digest" | ||
checked={digestEnabled} | ||
onChange={(e) => setDigestEnabled(!digestEnabled)} | ||
switchLabel={ | ||
<span className={`h4 ${!digestEnabled ? "text-muted" : ""}`}> | ||
<span>Enable digest</span> | ||
</span> | ||
} | ||
fettuccinae marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/> | ||
{digestEnabled && ( | ||
<div className="mb-4"> | ||
Send digest emails every | ||
<input | ||
type="number" | ||
className="d-inline form-control ms-2 me-2" | ||
id="digest-age" | ||
min={1} | ||
max={100} | ||
defaultValue={digestAge} | ||
style={{ maxWidth: "4em" }} | ||
onChange={(e) => { | ||
setDigestAge(Number(e.target.value)); | ||
}} | ||
/> | ||
days | ||
</div> | ||
MonkeyDo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
)} | ||
</details> | ||
<div className="mt-4"> | ||
<button | ||
className="btn btn-lg btn-info" | ||
type="submit" | ||
disabled={saving || hasNoChanges || !isDigestAgeValid} | ||
> | ||
Save notification settings | ||
</button> | ||
</div> | ||
</form> | ||
</> | ||
); | ||
} |
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.
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.
Uh oh!
There was an error while loading. Please reload this page.