mirror of
https://github.com/outline/outline.git
synced 2026-12-02 17:52:07 +03:00
feat: Add toggle for all notifications (#11713)
* feat: Add toggle for all notifications * tidy
This commit is contained in:
1 parent
fee9791cc9
commit
2e50fb0344
4 files changed
+64
-29
No files matched your search
@@ -1,4 +1,5 @@
|
||||
import debounce from "lodash/debounce";
|
||||
import { runInAction } from "mobx";
|
||||
import { observer } from "mobx-react";
|
||||
import {
|
||||
AcademicCapIcon,
|
||||
@@ -24,19 +25,14 @@ import Notice from "~/components/Notice";
|
||||
import Scene from "~/components/Scene";
|
||||
import Switch from "~/components/Switch";
|
||||
import Text from "~/components/Text";
|
||||
import { HStack } from "~/components/primitives/HStack";
|
||||
import env from "~/env";
|
||||
import useCurrentTeam from "~/hooks/useCurrentTeam";
|
||||
import useCurrentUser from "~/hooks/useCurrentUser";
|
||||
import usePolicy from "~/hooks/usePolicy";
|
||||
import { client } from "~/utils/ApiClient";
|
||||
import isCloudHosted from "~/utils/isCloudHosted";
|
||||
import SettingRow from "./components/SettingRow";
|
||||
|
||||
function Notifications() {
|
||||
const user = useCurrentUser();
|
||||
const team = useCurrentTeam();
|
||||
const { t } = useTranslation();
|
||||
const can = usePolicy(team.id);
|
||||
|
||||
const options = [
|
||||
{
|
||||
@@ -151,6 +147,8 @@ function Notifications() {
|
||||
},
|
||||
];
|
||||
|
||||
const visibleOptions = options.filter((o) => o.visible !== false);
|
||||
|
||||
const showSuccessMessage = debounce(() => {
|
||||
toast.success(t("Notifications saved"));
|
||||
}, 500);
|
||||
@@ -163,6 +161,29 @@ function Notifications() {
|
||||
[user, showSuccessMessage]
|
||||
);
|
||||
|
||||
const handleToggleAll = React.useCallback(
|
||||
async (checked: boolean) => {
|
||||
runInAction(() => {
|
||||
const updated = { ...user.notificationSettings };
|
||||
for (const option of visibleOptions) {
|
||||
updated[option.event] = checked;
|
||||
}
|
||||
user.notificationSettings = updated;
|
||||
});
|
||||
await client.post(
|
||||
checked
|
||||
? `/users.notificationsSubscribe`
|
||||
: `/users.notificationsUnsubscribe`
|
||||
);
|
||||
showSuccessMessage();
|
||||
},
|
||||
[user, visibleOptions, showSuccessMessage]
|
||||
);
|
||||
|
||||
const allEnabled = visibleOptions.every((o) =>
|
||||
user.subscribedToEventType(o.event)
|
||||
);
|
||||
|
||||
const showSuccessNotice = window.location.search === "?success";
|
||||
|
||||
return (
|
||||
@@ -180,17 +201,18 @@ function Notifications() {
|
||||
<Trans>Manage when and where you receive email notifications.</Trans>
|
||||
</Text>
|
||||
|
||||
{env.EMAIL_ENABLED && can.manage && (
|
||||
<Notice>
|
||||
<Trans>
|
||||
The email integration is currently disabled. Please set the
|
||||
associated environment variables and restart the server to enable
|
||||
notifications.
|
||||
</Trans>
|
||||
</Notice>
|
||||
)}
|
||||
|
||||
<h2>{t("Notifications")}</h2>
|
||||
<SettingRow
|
||||
name="allNotifications"
|
||||
label={t("All notifications")}
|
||||
compact
|
||||
border={false}
|
||||
>
|
||||
<Switch
|
||||
id="allNotifications"
|
||||
checked={allEnabled}
|
||||
onChange={handleToggleAll}
|
||||
/>
|
||||
</SettingRow>
|
||||
|
||||
{options.map((option) => {
|
||||
const setting = user.subscribedToEventType(option.event);
|
||||
@@ -199,13 +221,14 @@ function Notifications() {
|
||||
<SettingRow
|
||||
key={option.event}
|
||||
visible={option.visible}
|
||||
label={
|
||||
<HStack spacing={4}>
|
||||
{option.icon} {option.title}
|
||||
</HStack>
|
||||
}
|
||||
label={option.title}
|
||||
name={option.event}
|
||||
description={option.description}
|
||||
description={
|
||||
<Text size="small" type="secondary">
|
||||
{option.description}
|
||||
</Text>
|
||||
}
|
||||
compact
|
||||
>
|
||||
<Switch
|
||||
key={option.event}
|
||||
|
||||
@@ -66,7 +66,7 @@ export type UsersListReq = z.infer<typeof UsersListSchema>;
|
||||
|
||||
export const UsersNotificationsSubscribeSchema = z.object({
|
||||
body: z.object({
|
||||
eventType: z.enum(NotificationEventType),
|
||||
eventType: z.enum(NotificationEventType).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
@@ -76,7 +76,7 @@ export type UsersNotificationsSubscribeReq = z.infer<
|
||||
|
||||
export const UsersNotificationsUnsubscribeSchema = z.object({
|
||||
body: z.object({
|
||||
eventType: z.enum(NotificationEventType),
|
||||
eventType: z.enum(NotificationEventType).optional(),
|
||||
}),
|
||||
});
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@ import Router from "koa-router";
|
||||
import type { WhereOptions } from "sequelize";
|
||||
import { Op, Sequelize } from "sequelize";
|
||||
import type { UserPreferences } from "@shared/types";
|
||||
import { UserRole } from "@shared/types";
|
||||
import { NotificationEventType, UserRole } from "@shared/types";
|
||||
import { UserRoleHelper } from "@shared/utils/UserRoleHelper";
|
||||
import { settingsPath } from "@shared/utils/routeHelpers";
|
||||
import { UserValidation } from "@shared/validations";
|
||||
@@ -680,7 +680,13 @@ router.post(
|
||||
async (ctx: APIContext<T.UsersNotificationsSubscribeReq>) => {
|
||||
const { eventType } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
user.setNotificationEventType(eventType, true);
|
||||
const eventTypes = eventType
|
||||
? [eventType]
|
||||
: Object.values(NotificationEventType);
|
||||
|
||||
for (const type of eventTypes) {
|
||||
user.setNotificationEventType(type, true);
|
||||
}
|
||||
|
||||
await user.saveWithCtx(ctx);
|
||||
|
||||
@@ -698,7 +704,13 @@ router.post(
|
||||
async (ctx: APIContext<T.UsersNotificationsUnsubscribeReq>) => {
|
||||
const { eventType } = ctx.input.body;
|
||||
const { user } = ctx.state.auth;
|
||||
user.setNotificationEventType(eventType, false);
|
||||
const eventTypes = eventType
|
||||
? [eventType]
|
||||
: Object.values(NotificationEventType);
|
||||
|
||||
for (const type of eventTypes) {
|
||||
user.setNotificationEventType(type, false);
|
||||
}
|
||||
|
||||
await user.saveWithCtx(ctx);
|
||||
|
||||
|
||||
@@ -1277,7 +1277,7 @@
|
||||
"Notifications saved": "Notifications saved",
|
||||
"Unsubscription successful. Your notification settings were updated": "Unsubscription successful. Your notification settings were updated",
|
||||
"Manage when and where you receive email notifications.": "Manage when and where you receive email notifications.",
|
||||
"The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.": "The email integration is currently disabled. Please set the associated environment variables and restart the server to enable notifications.",
|
||||
"All notifications": "All notifications",
|
||||
"Preferences saved": "Preferences saved",
|
||||
"Unread count": "Unread count",
|
||||
"Unread indicator": "Unread indicator",
|
||||
|
||||
Reference in new issue
Block a user