mirror of
https://github.com/morpheus65535/bazarr
synced 2025-02-23 22:41:34 +00:00
Added opensubtitlescom provider validation (#2770)
This commit is contained in:
parent
8dc686e902
commit
d79bc09b7c
2 changed files with 90 additions and 33 deletions
|
@ -1,4 +1,5 @@
|
||||||
import {
|
import {
|
||||||
|
Fragment,
|
||||||
FunctionComponent,
|
FunctionComponent,
|
||||||
useCallback,
|
useCallback,
|
||||||
useMemo,
|
useMemo,
|
||||||
|
@ -42,7 +43,7 @@ import {
|
||||||
} from "@/pages/Settings/utilities/SettingsProvider";
|
} from "@/pages/Settings/utilities/SettingsProvider";
|
||||||
import { BuildKey, useSelectorOptions } from "@/utilities";
|
import { BuildKey, useSelectorOptions } from "@/utilities";
|
||||||
import { ASSERT } from "@/utilities/console";
|
import { ASSERT } from "@/utilities/console";
|
||||||
import { ProviderInfo } from "./list";
|
import { ProviderInfo, ProviderList } from "./list";
|
||||||
|
|
||||||
type SettingsKey =
|
type SettingsKey =
|
||||||
| "settings-general-enabled_providers"
|
| "settings-general-enabled_providers"
|
||||||
|
@ -151,6 +152,27 @@ const SelectItem: AutocompleteProps["renderOption"] = ({ option }) => {
|
||||||
);
|
);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const validation = ProviderList.map((provider) => {
|
||||||
|
return provider.inputs
|
||||||
|
?.map((input) => {
|
||||||
|
if (input.validation === undefined) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
[`settings-${provider.key}-${input.key}`]: input.validation?.rule,
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.filter((input) => input && Object.keys(input).length > 0)
|
||||||
|
.reduce((acc, curr) => {
|
||||||
|
return { ...acc, ...curr };
|
||||||
|
}, {});
|
||||||
|
})
|
||||||
|
.filter((provider) => provider && Object.keys(provider).length > 0)
|
||||||
|
.reduce((acc, item) => {
|
||||||
|
return { ...acc, ...item };
|
||||||
|
}, {});
|
||||||
|
|
||||||
const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
payload,
|
payload,
|
||||||
enabledProviders,
|
enabledProviders,
|
||||||
|
@ -172,6 +194,9 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
settings: staged,
|
settings: staged,
|
||||||
hooks: {},
|
hooks: {},
|
||||||
},
|
},
|
||||||
|
validate: {
|
||||||
|
settings: validation!,
|
||||||
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
const deletePayload = useCallback(() => {
|
const deletePayload = useCallback(() => {
|
||||||
|
@ -188,6 +213,12 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
|
|
||||||
const submit = useCallback(
|
const submit = useCallback(
|
||||||
(values: FormValues) => {
|
(values: FormValues) => {
|
||||||
|
const result = form.validate();
|
||||||
|
|
||||||
|
if (result.hasErrors) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (info && enabledProviders) {
|
if (info && enabledProviders) {
|
||||||
const changes = { ...values.settings };
|
const changes = { ...values.settings };
|
||||||
const hooks = values.hooks;
|
const hooks = values.hooks;
|
||||||
|
@ -204,7 +235,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
modals.closeAll();
|
modals.closeAll();
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
[info, enabledProviders, modals, settingsKey],
|
[info, enabledProviders, modals, settingsKey, form],
|
||||||
);
|
);
|
||||||
|
|
||||||
const canSave = info !== null;
|
const canSave = info !== null;
|
||||||
|
@ -249,43 +280,57 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
const label = value.name ?? capitalize(value.key);
|
const label = value.name ?? capitalize(value.key);
|
||||||
const options = value.options ?? [];
|
const options = value.options ?? [];
|
||||||
|
|
||||||
|
const error = form.errors[`settings.settings-${itemKey}-${key}`] ? (
|
||||||
|
<MantineText c="red" component="span" size="xs">
|
||||||
|
{form.errors[`settings.settings-${itemKey}-${key}`]}
|
||||||
|
</MantineText>
|
||||||
|
) : null;
|
||||||
|
|
||||||
switch (value.type) {
|
switch (value.type) {
|
||||||
case "text":
|
case "text":
|
||||||
elements.push(
|
elements.push(
|
||||||
|
<Fragment key={BuildKey(itemKey, key)}>
|
||||||
<Text
|
<Text
|
||||||
key={BuildKey(itemKey, key)}
|
|
||||||
label={label}
|
label={label}
|
||||||
settingKey={`settings-${itemKey}-${key}`}
|
settingKey={`settings-${itemKey}-${key}`}
|
||||||
></Text>,
|
></Text>
|
||||||
|
{error}
|
||||||
|
</Fragment>,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
case "password":
|
case "password":
|
||||||
elements.push(
|
elements.push(
|
||||||
|
<Fragment key={BuildKey(itemKey, key)}>
|
||||||
<Password
|
<Password
|
||||||
key={BuildKey(itemKey, key)}
|
|
||||||
label={label}
|
label={label}
|
||||||
settingKey={`settings-${itemKey}-${key}`}
|
settingKey={`settings-${itemKey}-${key}`}
|
||||||
></Password>,
|
></Password>
|
||||||
|
{error}
|
||||||
|
</Fragment>,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
case "switch":
|
case "switch":
|
||||||
elements.push(
|
elements.push(
|
||||||
|
<Fragment key={BuildKey(itemKey, key)}>
|
||||||
<Check
|
<Check
|
||||||
key={key}
|
|
||||||
inline
|
inline
|
||||||
label={label}
|
label={label}
|
||||||
settingKey={`settings-${itemKey}-${key}`}
|
settingKey={`settings-${itemKey}-${key}`}
|
||||||
></Check>,
|
></Check>
|
||||||
|
{error}
|
||||||
|
</Fragment>,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
case "select":
|
case "select":
|
||||||
elements.push(
|
elements.push(
|
||||||
|
<Fragment key={BuildKey(itemKey, key)}>
|
||||||
<GlobalSelector
|
<GlobalSelector
|
||||||
key={key}
|
|
||||||
label={label}
|
label={label}
|
||||||
settingKey={`settings-${itemKey}-${key}`}
|
settingKey={`settings-${itemKey}-${key}`}
|
||||||
options={options}
|
options={options}
|
||||||
></GlobalSelector>,
|
></GlobalSelector>
|
||||||
|
{error}
|
||||||
|
</Fragment>,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
case "testbutton":
|
case "testbutton":
|
||||||
|
@ -295,11 +340,13 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
return;
|
return;
|
||||||
case "chips":
|
case "chips":
|
||||||
elements.push(
|
elements.push(
|
||||||
|
<Fragment key={BuildKey(itemKey, key)}>
|
||||||
<Chips
|
<Chips
|
||||||
key={key}
|
|
||||||
label={label}
|
label={label}
|
||||||
settingKey={`settings-${itemKey}-${key}`}
|
settingKey={`settings-${itemKey}-${key}`}
|
||||||
></Chips>,
|
></Chips>
|
||||||
|
{error}
|
||||||
|
</Fragment>,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
default:
|
default:
|
||||||
|
@ -308,7 +355,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
|
||||||
});
|
});
|
||||||
|
|
||||||
return <Stack gap="xs">{elements}</Stack>;
|
return <Stack gap="xs">{elements}</Stack>;
|
||||||
}, [info]);
|
}, [info, form]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<SettingsProvider value={settings}>
|
<SettingsProvider value={settings}>
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { ReactText } from "react";
|
|
||||||
import { SelectorOption } from "@/components";
|
import { SelectorOption } from "@/components";
|
||||||
|
|
||||||
|
type Text = string | number;
|
||||||
|
|
||||||
type Input<T, N> = {
|
type Input<T, N> = {
|
||||||
type: N;
|
type: N;
|
||||||
key: string;
|
key: string;
|
||||||
|
@ -8,15 +9,18 @@ type Input<T, N> = {
|
||||||
name?: string;
|
name?: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
options?: SelectorOption<string>[];
|
options?: SelectorOption<string>[];
|
||||||
|
validation?: {
|
||||||
|
rule: (value: string) => string | null;
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
type AvailableInput =
|
type AvailableInput =
|
||||||
| Input<ReactText, "text">
|
| Input<Text, "text">
|
||||||
| Input<string, "password">
|
| Input<string, "password">
|
||||||
| Input<boolean, "switch">
|
| Input<boolean, "switch">
|
||||||
| Input<string, "select">
|
| Input<string, "select">
|
||||||
| Input<string, "testbutton">
|
| Input<string, "testbutton">
|
||||||
| Input<ReactText[], "chips">;
|
| Input<Text[], "chips">;
|
||||||
|
|
||||||
export interface ProviderInfo {
|
export interface ProviderInfo {
|
||||||
key: string;
|
key: string;
|
||||||
|
@ -391,6 +395,12 @@ export const ProviderList: Readonly<ProviderInfo[]> = [
|
||||||
{
|
{
|
||||||
type: "text",
|
type: "text",
|
||||||
key: "username",
|
key: "username",
|
||||||
|
validation: {
|
||||||
|
rule: (value: string) =>
|
||||||
|
/^.\S+@\S+$/.test(value)
|
||||||
|
? "Invalid Username. Do not use your e-mail."
|
||||||
|
: null,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
type: "password",
|
type: "password",
|
||||||
|
|
Loading…
Reference in a new issue