From 45dbf84d7e97aaf1771a5d8b5f12ce3a8fdfafd6 Mon Sep 17 00:00:00 2001 From: fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Thu, 14 Mar 2024 01:18:03 +0500 Subject: [PATCH] refactor: use enums for serverType and rename selectedservice to serverType --- server/constants/server.ts | 5 +++ src/components/Login/JellyfinLogin.tsx | 60 +++++++++++++------------- src/components/Setup/SetupLogin.tsx | 14 +++--- 3 files changed, 43 insertions(+), 36 deletions(-) diff --git a/server/constants/server.ts b/server/constants/server.ts index 7b2f9f1f..eed1939f 100644 --- a/server/constants/server.ts +++ b/server/constants/server.ts @@ -4,3 +4,8 @@ export enum MediaServerType { EMBY, NOT_CONFIGURED, } + +export enum ServerType { + JELLYFIN = 'Jellyfin', + EMBY = 'Emby', +} diff --git a/src/components/Login/JellyfinLogin.tsx b/src/components/Login/JellyfinLogin.tsx index e246fb28..30eabe39 100644 --- a/src/components/Login/JellyfinLogin.tsx +++ b/src/components/Login/JellyfinLogin.tsx @@ -6,6 +6,7 @@ import Button from '@app/components/Common/Button'; import Tooltip from '@app/components/Common/Tooltip'; import useSettings from '@app/hooks/useSettings'; import { InformationCircleIcon } from '@heroicons/react/24/solid'; +import { MediaServerType, ServerType } from '@server/constants/server'; import axios from 'axios'; import { Field, Form, Formik } from 'formik'; import type React from 'react'; @@ -26,7 +27,7 @@ const messages = defineMessages({ validationemailformat: 'Valid email required', validationusernamerequired: 'Username required', validationpasswordrequired: 'Password required', - validationselectedservicerequired: 'Please select a server type', + validationservertyperequired: 'Please select a server type', loginerror: 'Something went wrong while trying to sign in.', credentialerror: 'The username or password is incorrect.', signingin: 'Signing in…', @@ -41,14 +42,14 @@ interface JellyfinLoginProps { revalidate: () => void; initial?: boolean; onToggle?: (option: string) => void; - selectedService?: string; + serverType?: string; } const JellyfinLogin: React.FC = ({ revalidate, initial, onToggle, - selectedService, + serverType, }) => { const toasts = useToasts(); const intl = useIntl(); @@ -63,7 +64,7 @@ const JellyfinLogin: React.FC = ({ ) .required( intl.formatMessage(messages.validationhostrequired, { - mediaServerName: selectedService, + mediaServerName: serverType, }) ), email: Yup.string() @@ -73,17 +74,17 @@ const JellyfinLogin: React.FC = ({ intl.formatMessage(messages.validationusernamerequired) ), password: Yup.string(), - selectedservice: Yup.string().required( - intl.formatMessage(messages.validationselectedservicerequired) + serverType: Yup.string().required( + intl.formatMessage(messages.validationservertyperequired) ), }); const mediaServerFormatValues = { mediaServerName: - selectedService === 'Jellyfin' - ? 'Jellyfin' - : selectedService === 'Emby' - ? 'Emby' + serverType === ServerType.JELLYFIN + ? ServerType.JELLYFIN + : serverType === ServerType.EMBY + ? ServerType.EMBY : 'Media Server', }; return ( @@ -93,16 +94,16 @@ const JellyfinLogin: React.FC = ({ password: '', host: '', email: '', - selectedservice: '', + serverType: '', }} - initialErrors={{ selectedservice: 'Please select a server type' }} // Initialize errors with an empty object - initialTouched={{ selectedservice: true }} + initialErrors={{ serverType: 'Please select a server type' }} // Initialize errors with an empty object + initialTouched={{ serverType: true }} validationSchema={LoginSchema} onSubmit={async (values) => { try { - // Check if selectedService is either 'Jellyfin' or 'Emby' - // if (selectedService !== 'Jellyfin' && selectedService !== 'Emby') { - // throw new Error('Invalid selectedService'); // You can customize the error message + // Check if serverType is either 'Jellyfin' or 'Emby' + // if (serverType !== 'Jellyfin' && serverType !== 'Emby') { + // throw new Error('Invalid serverType'); // You can customize the error message // } await axios.post('/api/v1/auth/jellyfin', { @@ -110,7 +111,7 @@ const JellyfinLogin: React.FC = ({ password: values.password, hostname: values.host, email: values.email, - selectedservice: selectedService, + serverType: serverType, }); } catch (e) { toasts.addToast( @@ -147,18 +148,18 @@ const JellyfinLogin: React.FC = ({ {/* Hidden field */} - - {!values.selectedservice && errors.selectedservice && ( -
{errors.selectedservice}
+ + {!values.serverType && errors.serverType && ( +
{errors.serverType}
)}