Skip to content

Commit

Permalink
refactor: cleaner way of handling serverType change using MediaServer…
Browse files Browse the repository at this point in the history
…Type instead of strings

instead of using strings now it will use MediaServerType enums for serverType
  • Loading branch information
fallenbagel committed Mar 13, 2024
1 parent 1bc4bd3 commit 1aac3c5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 30 deletions.
2 changes: 1 addition & 1 deletion overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3574,7 +3574,7 @@ paths:
email:
type: string
serverType:
type: string
type: number
required:
- username
- password
Expand Down
16 changes: 8 additions & 8 deletions server/routes/auth.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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',
Expand Down
29 changes: 14 additions & 15 deletions src/components/Login/JellyfinLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<JellyfinLoginProps> = ({
revalidate,
initial,
onToggle,
serverType,
onServerTypeChange,
}) => {
const toasts = useToasts();
const intl = useIntl();
Expand Down Expand Up @@ -81,12 +81,13 @@ const JellyfinLogin: React.FC<JellyfinLoginProps> = ({

const mediaServerFormatValues = {
mediaServerName:
serverType === ServerType.JELLYFIN
serverType === MediaServerType.JELLYFIN
? ServerType.JELLYFIN
: serverType === ServerType.EMBY
: serverType === MediaServerType.EMBY
? ServerType.EMBY
: 'Media Server',
};

return (
<Formik
initialValues={{
Expand Down Expand Up @@ -148,18 +149,17 @@ const JellyfinLogin: React.FC<JellyfinLoginProps> = ({
<button
type="button"
className={`server-type-button jellyfin-server ${
serverType === ServerType.JELLYFIN
serverType === MediaServerType.JELLYFIN
? 'bg-gradient-to-r from-[#AA5CC3] to-[#00A4DC]'
: ''
}`}
onClick={() => {
if (onToggle) {
onToggle(ServerType.JELLYFIN);
}
onServerTypeChange &&
onServerTypeChange(MediaServerType.JELLYFIN);
setFieldValue('serverType', ServerType.JELLYFIN);
}}
>
{serverType === ServerType.JELLYFIN ? (
{serverType === MediaServerType.JELLYFIN ? (
<JellyfinLogoInverted />
) : (
<JellyfinLogo />
Expand All @@ -168,16 +168,15 @@ const JellyfinLogin: React.FC<JellyfinLoginProps> = ({
<button
type="button"
className={`server-type-button emby-server ${
serverType === ServerType.EMBY ? 'bg-[#51B44A]' : ''
serverType === MediaServerType.EMBY ? 'bg-[#51B44A]' : ''
}`}
onClick={() => {
if (onToggle) {
onToggle(ServerType.EMBY);
}
onServerTypeChange &&
onServerTypeChange(MediaServerType.EMBY);
setFieldValue('serverType', ServerType.EMBY);
}}
>
{serverType === ServerType.EMBY ? (
{serverType === MediaServerType.EMBY ? (
<EmbyLogoInverted />
) : (
<EmbyLogo />
Expand Down
17 changes: 11 additions & 6 deletions src/components/Setup/SetupLogin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ const SetupLogin: React.FC<LoginWithMediaServerProps> = ({ onComplete }) => {
);
const { user, revalidate } = useUser();
const intl = useIntl();
const [serverType, setserverType] = useState<string | undefined>(undefined);
const [serverType, setserverType] = useState<MediaServerType | undefined>(
undefined
);

// Function to handle toggle changes
const handleToggle = (option: string) => {
const handleServerTypeChange = (type: MediaServerType) => {
// Toggle between 'emby' and 'jellyfin'
setserverType(option);
setserverType(type);
};

// Effect that is triggered when the `authToken` comes back from the Plex OAuth
Expand Down Expand Up @@ -102,8 +104,11 @@ const SetupLogin: React.FC<LoginWithMediaServerProps> = ({ onComplete }) => {
>
{intl.formatMessage(messages.signinWithJellyfin, {
mediaServerName:
serverType ??
`${ServerType.JELLYFIN} or ${ServerType.EMBY}`,
serverType === MediaServerType.JELLYFIN
? ServerType.JELLYFIN
: serverType === MediaServerType.EMBY
? ServerType.EMBY
: `${ServerType.JELLYFIN} or ${ServerType.EMBY}`,
})}
</button>
<AccordionContent isOpen={openIndexes.includes(1)}>
Expand All @@ -115,7 +120,7 @@ const SetupLogin: React.FC<LoginWithMediaServerProps> = ({ onComplete }) => {
initial={true}
revalidate={revalidate}
serverType={serverType}
onToggle={handleToggle}
onServerTypeChange={handleServerTypeChange}
/>
</div>
</AccordionContent>
Expand Down

0 comments on commit 1aac3c5

Please sign in to comment.