Skip to content

Commit

Permalink
fix: add Discord ID setting to general user settings page (#2406)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCatLady committed Feb 10, 2022
1 parent 9f4ae34 commit eff665e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 23 deletions.
1 change: 1 addition & 0 deletions server/interfaces/api/userSettingsInterfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NotificationAgentKey } from '../../lib/settings';

export interface UserSettingsGeneralResponse {
username?: string;
discordId?: string;
locale?: string;
region?: string;
originalLanguage?: string;
Expand Down
6 changes: 5 additions & 1 deletion server/routes/user/usersettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ userSettingsRoutes.get<{ id: string }, UserSettingsGeneralResponse>(

return res.status(200).json({
username: user.username,
discordId: user.settings?.discordId,
locale: user.settings?.locale,
region: user.settings?.region,
originalLanguage: user.settings?.originalLanguage,
Expand Down Expand Up @@ -109,11 +110,13 @@ userSettingsRoutes.post<
if (!user.settings) {
user.settings = new UserSettings({
user: req.user,
discordId: req.body.discordId,
locale: req.body.locale,
region: req.body.region,
originalLanguage: req.body.originalLanguage,
});
} else {
user.settings.discordId = req.body.discordId;
user.settings.locale = req.body.locale;
user.settings.region = req.body.region;
user.settings.originalLanguage = req.body.originalLanguage;
Expand All @@ -123,8 +126,9 @@ userSettingsRoutes.post<

return res.status(200).json({
username: user.username,
region: user.settings.region,
discordId: user.settings.discordId,
locale: user.settings.locale,
region: user.settings.region,
originalLanguage: user.settings.originalLanguage,
});
} catch (e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import React, { useEffect, useState } from 'react';
import { defineMessages, useIntl } from 'react-intl';
import { useToasts } from 'react-toast-notifications';
import useSWR from 'swr';
import * as Yup from 'yup';
import { UserSettingsGeneralResponse } from '../../../../../server/interfaces/api/userSettingsInterfaces';
import {
availableLanguages,
Expand Down Expand Up @@ -46,6 +47,10 @@ const messages = defineMessages({
enableOverride: 'Override Global Limit',
applanguage: 'Display Language',
languageDefault: 'Default ({language})',
discordId: 'Discord User ID',
discordIdTip:
'The <FindDiscordIdLink>multi-digit ID number</FindDiscordIdLink> associated with your Discord user account',
validationDiscordId: 'You must provide a valid Discord user ID',
});

const UserGeneralSettings: React.FC = () => {
Expand All @@ -72,6 +77,12 @@ const UserGeneralSettings: React.FC = () => {
user ? `/api/v1/user/${user?.id}/settings/main` : null
);

const UserGeneralSettingsSchema = Yup.object().shape({
discordId: Yup.string()
.nullable()
.matches(/^\d{17,18}$/, intl.formatMessage(messages.validationDiscordId)),
});

useEffect(() => {
setMovieQuotaEnabled(
data?.movieQuotaLimit != undefined && data?.movieQuotaDays != undefined
Expand Down Expand Up @@ -104,20 +115,24 @@ const UserGeneralSettings: React.FC = () => {
</div>
<Formik
initialValues={{
locale: data?.locale,
displayName: data?.username,
discordId: data?.discordId,
locale: data?.locale,
region: data?.region,
originalLanguage: data?.originalLanguage,
movieQuotaLimit: data?.movieQuotaLimit,
movieQuotaDays: data?.movieQuotaDays,
tvQuotaLimit: data?.tvQuotaLimit,
tvQuotaDays: data?.tvQuotaDays,
}}
validationSchema={UserGeneralSettingsSchema}
enableReinitialize
onSubmit={async (values) => {
try {
await axios.post(`/api/v1/user/${user?.id}/settings/main`, {
username: values.displayName,
discordId: values.discordId,
locale: values.locale,
region: values.region,
originalLanguage: values.originalLanguage,
movieQuotaLimit: movieQuotaEnabled
Expand All @@ -126,7 +141,6 @@ const UserGeneralSettings: React.FC = () => {
movieQuotaDays: movieQuotaEnabled ? values.movieQuotaDays : null,
tvQuotaLimit: tvQuotaEnabled ? values.tvQuotaLimit : null,
tvQuotaDays: tvQuotaEnabled ? values.tvQuotaDays : null,
locale: values.locale,
});

if (currentUser?.id === user?.id && setLocale) {
Expand All @@ -152,7 +166,14 @@ const UserGeneralSettings: React.FC = () => {
}
}}
>
{({ errors, touched, isSubmitting, values, setFieldValue }) => {
{({
errors,
touched,
isSubmitting,
isValid,
values,
setFieldValue,
}) => {
return (
<Form className="section">
<div className="form-row">
Expand Down Expand Up @@ -207,6 +228,36 @@ const UserGeneralSettings: React.FC = () => {
)}
</div>
</div>
<div className="form-row">
<label htmlFor="discordId" className="text-label">
{intl.formatMessage(messages.discordId)}
{currentUser?.id === user?.id && (
<span className="label-tip">
{intl.formatMessage(messages.discordIdTip, {
FindDiscordIdLink: function FindDiscordIdLink(msg) {
return (
<a
href="https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-"
target="_blank"
rel="noreferrer"
>
{msg}
</a>
);
},
})}
</span>
)}
</label>
<div className="form-input">
<div className="form-input-field">
<Field id="discordId" name="discordId" type="text" />
</div>
{errors.discordId && touched.discordId && (
<div className="error">{errors.discordId}</div>
)}
</div>
</div>
<div className="form-row">
<label htmlFor="locale" className="text-label">
{intl.formatMessage(messages.applanguage)}
Expand Down Expand Up @@ -364,7 +415,7 @@ const UserGeneralSettings: React.FC = () => {
<Button
buttonType="primary"
type="submit"
disabled={isSubmitting}
disabled={isSubmitting || !isValid}
>
<SaveIcon />
<span>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const messages = defineMessages({
discordsettingsfailed: 'Discord notification settings failed to save.',
discordId: 'User ID',
discordIdTip:
'The <FindDiscordIdLink>ID number</FindDiscordIdLink> for your user account',
'The <FindDiscordIdLink>multi-digit ID number</FindDiscordIdLink> associated with your user account',
validationDiscordId: 'You must provide a valid user ID',
});

Expand All @@ -28,6 +28,7 @@ const UserNotificationsDiscord: React.FC = () => {
const { addToast } = useToasts();
const router = useRouter();
const { user } = useUser({ id: Number(router.query.userId) });
const { user: currentUser } = useUser();
const {
data,
error,
Expand Down Expand Up @@ -107,21 +108,23 @@ const UserNotificationsDiscord: React.FC = () => {
{!!data?.discordEnabledTypes && (
<span className="label-required">*</span>
)}
<span className="label-tip">
{intl.formatMessage(messages.discordIdTip, {
FindDiscordIdLink: function FindDiscordIdLink(msg) {
return (
<a
href="https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-"
target="_blank"
rel="noreferrer"
>
{msg}
</a>
);
},
})}
</span>
{currentUser?.id === user?.id && (
<span className="label-tip">
{intl.formatMessage(messages.discordIdTip, {
FindDiscordIdLink: function FindDiscordIdLink(msg) {
return (
<a
href="https://support.discord.com/hc/en-us/articles/206346498-Where-can-I-find-my-User-Server-Message-ID-"
target="_blank"
rel="noreferrer"
>
{msg}
</a>
);
},
})}
</span>
)}
</label>
<div className="form-input">
<div className="form-input-field">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const UserNotificationSettings: React.FC = ({ children }) => {
),
route: '/settings/notifications/discord',
regex: /\/settings\/notifications\/discord/,
hidden: !data?.discordEnabled,
},
{
text: 'Pushbullet',
Expand Down
4 changes: 3 additions & 1 deletion src/components/UserProfile/UserSettings/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ const UserSettings: React.FC = ({ children }) => {
? '/settings/notifications/email'
: data?.webPushEnabled
? '/settings/notifications/webpush'
: '/settings/notifications/discord',
: data?.discordEnabled
? '/settings/notifications/discord'
: '/settings/notifications/pushbullet',
regex: /\/settings\/notifications/,
},
{
Expand Down
5 changes: 4 additions & 1 deletion src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,8 @@
"components.UserProfile.UserSettings.UserGeneralSettings.accounttype": "Account Type",
"components.UserProfile.UserSettings.UserGeneralSettings.admin": "Admin",
"components.UserProfile.UserSettings.UserGeneralSettings.applanguage": "Display Language",
"components.UserProfile.UserSettings.UserGeneralSettings.discordId": "Discord User ID",
"components.UserProfile.UserSettings.UserGeneralSettings.discordIdTip": "The <FindDiscordIdLink>multi-digit ID number</FindDiscordIdLink> associated with your Discord user account",
"components.UserProfile.UserSettings.UserGeneralSettings.displayName": "Display Name",
"components.UserProfile.UserSettings.UserGeneralSettings.enableOverride": "Override Global Limit",
"components.UserProfile.UserSettings.UserGeneralSettings.general": "General",
Expand All @@ -909,8 +911,9 @@
"components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsFailure": "Something went wrong while saving settings.",
"components.UserProfile.UserSettings.UserGeneralSettings.toastSettingsSuccess": "Settings saved successfully!",
"components.UserProfile.UserSettings.UserGeneralSettings.user": "User",
"components.UserProfile.UserSettings.UserGeneralSettings.validationDiscordId": "You must provide a valid Discord user ID",
"components.UserProfile.UserSettings.UserNotificationSettings.discordId": "User ID",
"components.UserProfile.UserSettings.UserNotificationSettings.discordIdTip": "The <FindDiscordIdLink>ID number</FindDiscordIdLink> for your user account",
"components.UserProfile.UserSettings.UserNotificationSettings.discordIdTip": "The <FindDiscordIdLink>multi-digit ID number</FindDiscordIdLink> associated with your user account",
"components.UserProfile.UserSettings.UserNotificationSettings.discordsettingsfailed": "Discord notification settings failed to save.",
"components.UserProfile.UserSettings.UserNotificationSettings.discordsettingssaved": "Discord notification settings saved successfully!",
"components.UserProfile.UserSettings.UserNotificationSettings.email": "Email",
Expand Down

0 comments on commit eff665e

Please sign in to comment.