refactor(settings): replace tvdb object to boolean type

This commit is contained in:
TOomaAh
2024-10-29 23:10:35 +01:00
parent 85aeeb084e
commit 3f16176667
3 changed files with 27 additions and 28 deletions

View File

@@ -81,10 +81,6 @@ export interface DVRSettings {
tagRequests: boolean; tagRequests: boolean;
} }
export interface TvdbSettings {
use: boolean;
}
export interface RadarrSettings extends DVRSettings { export interface RadarrSettings extends DVRSettings {
minimumAvailability: string; minimumAvailability: string;
} }
@@ -293,12 +289,12 @@ export interface AllSettings {
plex: PlexSettings; plex: PlexSettings;
jellyfin: JellyfinSettings; jellyfin: JellyfinSettings;
tautulli: TautulliSettings; tautulli: TautulliSettings;
tvdb: TvdbSettings;
radarr: RadarrSettings[]; radarr: RadarrSettings[];
sonarr: SonarrSettings[]; sonarr: SonarrSettings[];
public: PublicSettings; public: PublicSettings;
notifications: NotificationSettings; notifications: NotificationSettings;
jobs: Record<JobId, JobSettings>; jobs: Record<JobId, JobSettings>;
tvdb: boolean;
} }
const SETTINGS_PATH = process.env.CONFIG_DIRECTORY const SETTINGS_PATH = process.env.CONFIG_DIRECTORY
@@ -354,7 +350,7 @@ class Settings {
apiKey: '', apiKey: '',
}, },
tautulli: {}, tautulli: {},
tvdb: { use: false }, tvdb: false,
radarr: [], radarr: [],
sonarr: [], sonarr: [],
public: { public: {
@@ -523,11 +519,11 @@ class Settings {
this.data.tautulli = data; this.data.tautulli = data;
} }
get tvdb(): TvdbSettings { get tvdb(): boolean {
return this.data.tvdb; return this.data.tvdb;
} }
set tvdb(data: TvdbSettings) { set tvdb(data: boolean) {
this.data.tvdb = data; this.data.tvdb = data;
} }
@@ -697,7 +693,7 @@ export const getSettings = (initialSettings?: AllSettings): Settings => {
export const getIndexer = (): TvShowIndexer => { export const getIndexer = (): TvShowIndexer => {
const settings = getSettings(); const settings = getSettings();
if (settings.tvdb?.use) { if (settings.tvdb) {
return new Tvdb(); return new Tvdb();
} else { } else {
return new TheMovieDb(); return new TheMovieDb();

View File

@@ -1,35 +1,33 @@
import Tvdb from '@server/api/indexer/tvdb'; import Tvdb from '@server/api/indexer/tvdb';
import type { TvdbSettings } from '@server/lib/settings';
import { getSettings } from '@server/lib/settings'; import { getSettings } from '@server/lib/settings';
import logger from '@server/logger'; import logger from '@server/logger';
import { Router } from 'express'; import { Router } from 'express';
const tvdbRoutes = Router(); const tvdbRoutes = Router();
export interface TvdbSettings {
tvdb: boolean;
}
tvdbRoutes.get('/', (_req, res) => { tvdbRoutes.get('/', (_req, res) => {
const settings = getSettings(); const settings = getSettings();
res.status(200).json(settings.tvdb); res.status(200).json({
tvdb: settings.tvdb,
});
}); });
tvdbRoutes.put('/', (req, res) => { tvdbRoutes.put('/', (req, res) => {
const settings = getSettings(); const settings = getSettings();
if (!settings.tvdb) { const body = req.body as TvdbSettings;
settings.tvdb = {
use: false,
};
}
const newTvdb = req.body as TvdbSettings; settings.tvdb = body.tvdb ?? settings.tvdb ?? false;
const tvdb = settings.tvdb;
tvdb.use = newTvdb.use;
settings.tvdb = tvdb;
settings.save(); settings.save();
return res.status(200).json(newTvdb); return res.status(200).json({
tvdb: settings.tvdb,
});
}); });
tvdbRoutes.post('/test', async (req, res, next) => { tvdbRoutes.post('/test', async (req, res, next) => {

View File

@@ -5,7 +5,7 @@ import SettingsBadge from '@app/components/Settings/SettingsBadge';
import globalMessages from '@app/i18n/globalMessages'; import globalMessages from '@app/i18n/globalMessages';
import defineMessages from '@app/utils/defineMessages'; import defineMessages from '@app/utils/defineMessages';
import { ArrowDownOnSquareIcon, BeakerIcon } from '@heroicons/react/24/outline'; import { ArrowDownOnSquareIcon, BeakerIcon } from '@heroicons/react/24/outline';
import type { TvdbSettings } from '@server/lib/settings'; import type { TvdbSettings } from '@server/routes/settings/tvdb';
import { Field, Form, Formik } from 'formik'; import { Field, Form, Formik } from 'formik';
import { useState } from 'react'; import { useState } from 'react';
import { useIntl } from 'react-intl'; import { useIntl } from 'react-intl';
@@ -42,13 +42,15 @@ const SettingsTvdb = () => {
} }
}; };
const saveSettings = async (values: TvdbSettings) => { const saveSettings = async (value: TvdbSettings) => {
const response = await fetch('/api/v1/settings/tvdb', { const response = await fetch('/api/v1/settings/tvdb', {
method: 'PUT', method: 'PUT',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
}, },
body: JSON.stringify(values), body: JSON.stringify({
tvdb: value.tvdb,
}),
}); });
if (!response.ok) { if (!response.ok) {
@@ -77,7 +79,7 @@ const SettingsTvdb = () => {
<div className="section"> <div className="section">
<Formik <Formik
initialValues={{ initialValues={{
enable: data?.use, enable: data?.tvdb ?? false,
}} }}
onSubmit={async (values) => { onSubmit={async (values) => {
try { try {
@@ -93,8 +95,11 @@ const SettingsTvdb = () => {
try { try {
await saveSettings({ await saveSettings({
use: values.enable || false, tvdb: values.enable ?? false,
}); });
if (data) {
data.tvdb = values.enable;
}
} catch (e) { } catch (e) {
addToast('Failed to save Tvdb settings', { appearance: 'error' }); addToast('Failed to save Tvdb settings', { appearance: 'error' });
return; return;