Skip to content

Commit

Permalink
Added opensubtitlescom provider validation
Browse files Browse the repository at this point in the history
  • Loading branch information
anderson-oki committed Nov 22, 2024
1 parent 6fbc912 commit 3ea4ec5
Showing 1 changed file with 64 additions and 29 deletions.
93 changes: 64 additions & 29 deletions frontend/src/pages/Settings/Providers/components.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,14 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
settings: staged,
hooks: {},
},
validate: {
settings: {
"settings-opensubtitlescom-username": (value) =>
/^.\S+@\S+$/.test(value)
? "Invalid Username. Do not use your e-mail."
: null,
},
},
});

const deletePayload = useCallback(() => {
Expand All @@ -188,6 +196,12 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({

const submit = useCallback(
(values: FormValues) => {
const result = form.validate();

if (result.hasErrors) {
return;
}

if (info && enabledProviders) {
const changes = { ...values.settings };
const hooks = values.hooks;
Expand All @@ -204,7 +218,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
modals.closeAll();
}
},
[info, enabledProviders, modals, settingsKey],
[info, enabledProviders, modals, settingsKey, form],
);

const canSave = info !== null;
Expand Down Expand Up @@ -249,43 +263,61 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
const label = value.name ?? capitalize(value.key);
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) {
case "text":
elements.push(
<Text
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Text>,
<>
<Text
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Text>
{error}
</>,
);
return;
case "password":
elements.push(
<Password
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Password>,
<>
<Password
key={BuildKey(itemKey, key)}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Password>
{error}
</>,
);
return;
case "switch":
elements.push(
<Check
key={key}
inline
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Check>,
<>
<Check
key={key}
inline
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Check>
{error}
</>,
);
return;
case "select":
elements.push(
<GlobalSelector
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
options={options}
></GlobalSelector>,
<>
<GlobalSelector
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
options={options}
></GlobalSelector>
{error}
</>,
);
return;
case "testbutton":
Expand All @@ -295,11 +327,14 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
return;
case "chips":
elements.push(
<Chips
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Chips>,
<>
<Chips
key={key}
label={label}
settingKey={`settings-${itemKey}-${key}`}
></Chips>
{error}
</>,
);
return;
default:
Expand All @@ -308,7 +343,7 @@ const ProviderTool: FunctionComponent<ProviderToolProps> = ({
});

return <Stack gap="xs">{elements}</Stack>;
}, [info]);
}, [info, form]);

return (
<SettingsProvider value={settings}>
Expand Down

0 comments on commit 3ea4ec5

Please sign in to comment.