import { SaveIcon } from '@heroicons/react/outline'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import { useRouter } from 'next/router'; 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, AvailableLocale, } from '../../../../context/LanguageContext'; import useLocale from '../../../../hooks/useLocale'; import useSettings from '../../../../hooks/useSettings'; import { Permission, UserType, useUser } from '../../../../hooks/useUser'; import globalMessages from '../../../../i18n/globalMessages'; import Error from '../../../../pages/_error'; import Badge from '../../../Common/Badge'; import Button from '../../../Common/Button'; import LoadingSpinner from '../../../Common/LoadingSpinner'; import PageTitle from '../../../Common/PageTitle'; import LanguageSelector from '../../../LanguageSelector'; import QuotaSelector from '../../../QuotaSelector'; import RegionSelector from '../../../RegionSelector'; const messages = defineMessages({ general: 'General', generalsettings: 'General Settings', displayName: 'Display Name', accounttype: 'Account Type', plexuser: 'Plex User', localuser: 'Local User', role: 'Role', owner: 'Owner', admin: 'Admin', user: 'User', toastSettingsSuccess: 'Settings saved successfully!', toastSettingsFailure: 'Something went wrong while saving settings.', region: 'Discover Region', regionTip: 'Filter content by regional availability', originallanguage: 'Discover Language', originallanguageTip: 'Filter content by original language', movierequestlimit: 'Movie Request Limit', seriesrequestlimit: 'Series Request Limit', enableOverride: 'Override Global Limit', applanguage: 'Display Language', languageDefault: 'Default ({language})', discordId: 'Discord User ID', discordIdTip: 'The multi-digit ID number associated with your Discord user account', validationDiscordId: 'You must provide a valid Discord user ID', }); const UserGeneralSettings: React.FC = () => { const intl = useIntl(); const { addToast } = useToasts(); const { locale, setLocale } = useLocale(); const [movieQuotaEnabled, setMovieQuotaEnabled] = useState(false); const [tvQuotaEnabled, setTvQuotaEnabled] = useState(false); const router = useRouter(); const { user, hasPermission, revalidate: revalidateUser, } = useUser({ id: Number(router.query.userId), }); const { user: currentUser, hasPermission: currentHasPermission } = useUser(); const { currentSettings } = useSettings(); const { data, error, mutate: revalidate, } = useSWR( 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 ); setTvQuotaEnabled( data?.tvQuotaLimit != undefined && data?.tvQuotaDays != undefined ); }, [data]); if (!data && !error) { return ; } if (!data) { return ; } return ( <> {intl.formatMessage(messages.generalsettings)} { 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 ? values.movieQuotaLimit : null, movieQuotaDays: movieQuotaEnabled ? values.movieQuotaDays : null, tvQuotaLimit: tvQuotaEnabled ? values.tvQuotaLimit : null, tvQuotaDays: tvQuotaEnabled ? values.tvQuotaDays : null, }); if (currentUser?.id === user?.id && setLocale) { setLocale( (values.locale ? values.locale : currentSettings.locale) as AvailableLocale ); } addToast(intl.formatMessage(messages.toastSettingsSuccess), { autoDismiss: true, appearance: 'success', }); } catch (e) { addToast(intl.formatMessage(messages.toastSettingsFailure), { autoDismiss: true, appearance: 'error', }); } finally { revalidate(); revalidateUser(); } }} > {({ errors, touched, isSubmitting, isValid, values, setFieldValue, }) => { return ( {intl.formatMessage(messages.accounttype)} {user?.userType === UserType.PLEX ? ( {intl.formatMessage(messages.plexuser)} ) : ( {intl.formatMessage(messages.localuser)} )} {intl.formatMessage(messages.role)} {user?.id === 1 ? intl.formatMessage(messages.owner) : hasPermission(Permission.ADMIN) ? intl.formatMessage(messages.admin) : intl.formatMessage(messages.user)} {intl.formatMessage(messages.displayName)} {errors.displayName && touched.displayName && ( {errors.displayName} )} {intl.formatMessage(messages.discordId)} {currentUser?.id === user?.id && ( {intl.formatMessage(messages.discordIdTip, { FindDiscordIdLink: function FindDiscordIdLink(msg) { return ( {msg} ); }, })} )} {errors.discordId && touched.discordId && ( {errors.discordId} )} {intl.formatMessage(messages.applanguage)} {intl.formatMessage(messages.languageDefault, { language: availableLanguages[currentSettings.locale].display, })} {( Object.keys( availableLanguages ) as (keyof typeof availableLanguages)[] ).map((key) => ( {availableLanguages[key].display} ))} {intl.formatMessage(messages.region)} {intl.formatMessage(messages.regionTip)} {intl.formatMessage(messages.originallanguage)} {intl.formatMessage(messages.originallanguageTip)} {currentHasPermission(Permission.MANAGE_USERS) && !hasPermission(Permission.MANAGE_USERS) && ( <> {intl.formatMessage(messages.movierequestlimit)} setMovieQuotaEnabled((s) => !s)} /> {intl.formatMessage(messages.enableOverride)} {intl.formatMessage(messages.seriesrequestlimit)} setTvQuotaEnabled((s) => !s)} /> {intl.formatMessage(messages.enableOverride)} > )} {isSubmitting ? intl.formatMessage(globalMessages.saving) : intl.formatMessage(globalMessages.save)} ); }} > ); }; export default UserGeneralSettings;