From 1aac3c5f09b868e8e225561d4b39b01f6e6a8049 Mon Sep 17 00:00:00 2001 From: fallenbagel <98979876+Fallenbagel@users.noreply.github.com> Date: Thu, 14 Mar 2024 03:44:34 +0500 Subject: [PATCH] refactor: cleaner way of handling serverType change using MediaServerType instead of strings instead of using strings now it will use MediaServerType enums for serverType --- overseerr-api.yml | 2 +- server/routes/auth.ts | 16 +++++++------- src/components/Login/JellyfinLogin.tsx | 29 +++++++++++++------------- src/components/Setup/SetupLogin.tsx | 17 +++++++++------ 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/overseerr-api.yml b/overseerr-api.yml index 5f4c3dd86..8e417d2ac 100644 --- a/overseerr-api.yml +++ b/overseerr-api.yml @@ -3574,7 +3574,7 @@ paths: email: type: string serverType: - type: string + type: number required: - username - password diff --git a/server/routes/auth.ts b/server/routes/auth.ts index 93020b82c..071aad7b9 100644 --- a/server/routes/auth.ts +++ b/server/routes/auth.ts @@ -1,6 +1,6 @@ import JellyfinAPI from '@server/api/jellyfin'; import PlexTvAPI from '@server/api/plextv'; -import { MediaServerType } from '@server/constants/server'; +import { MediaServerType, ServerType } from '@server/constants/server'; import { UserType } from '@server/constants/user'; import { getRepository } from '@server/datasource'; import { User } from '@server/entity/User'; @@ -220,7 +220,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { password?: string; hostname?: string; email?: string; - serverType?: string; + serverType?: number; }; //Make sure jellyfin login is enabled, but only if jellyfin && Emby is not already configured @@ -296,7 +296,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { // User doesn't exist, and there are no users in the database, we'll create the user // with admin permissions switch (body.serverType) { - case 'Emby': + case MediaServerType.EMBY: settings.main.mediaServerType = MediaServerType.EMBY; user = new User({ email: body.email, @@ -311,7 +311,7 @@ authRoutes.post('/jellyfin', async (req, res, next) => { userType: UserType.EMBY, }); break; - case 'Jellyfin': + case MediaServerType.JELLYFIN: settings.main.mediaServerType = MediaServerType.JELLYFIN; user = new User({ email: body.email, @@ -342,12 +342,12 @@ authRoutes.post('/jellyfin', async (req, res, next) => { logger.info( `Found matching ${ settings.main.mediaServerType === MediaServerType.JELLYFIN - ? 'Jellyfin' - : 'Emby' + ? ServerType.JELLYFIN + : ServerType.EMBY } user; updating user with ${ settings.main.mediaServerType === MediaServerType.JELLYFIN - ? 'Jellyfin' - : 'Emby' + ? ServerType.JELLYFIN + : ServerType.EMBY }`, { label: 'API', diff --git a/src/components/Login/JellyfinLogin.tsx b/src/components/Login/JellyfinLogin.tsx index 30eabe39e..ddb8871e9 100644 --- a/src/components/Login/JellyfinLogin.tsx +++ b/src/components/Login/JellyfinLogin.tsx @@ -41,15 +41,15 @@ const messages = defineMessages({ interface JellyfinLoginProps { revalidate: () => void; initial?: boolean; - onToggle?: (option: string) => void; - serverType?: string; + serverType?: MediaServerType; + onServerTypeChange?: (type: MediaServerType) => void; } const JellyfinLogin: React.FC = ({ revalidate, initial, - onToggle, serverType, + onServerTypeChange, }) => { const toasts = useToasts(); const intl = useIntl(); @@ -81,12 +81,13 @@ const JellyfinLogin: React.FC = ({ const mediaServerFormatValues = { mediaServerName: - serverType === ServerType.JELLYFIN + serverType === MediaServerType.JELLYFIN ? ServerType.JELLYFIN - : serverType === ServerType.EMBY + : serverType === MediaServerType.EMBY ? ServerType.EMBY : 'Media Server', }; + return ( = ({ @@ -115,7 +120,7 @@ const SetupLogin: React.FC = ({ onComplete }) => { initial={true} revalidate={revalidate} serverType={serverType} - onToggle={handleToggle} + onServerTypeChange={handleServerTypeChange} />