Add regex matching for add shelf item

Resolves not fetching when the users didn't put a / at the end of the service url
This commit is contained in:
Aj - Thomas
2022-05-11 08:55:38 +02:00
parent a7f4187a82
commit d58465142c

View File

@@ -10,9 +10,9 @@ import {
AspectRatio, AspectRatio,
Text, Text,
Card, Card,
LoadingOverlay,
} from '@mantine/core'; } from '@mantine/core';
import { useForm } from '@mantine/hooks'; import { useForm } from '@mantine/form';
import { UseForm } from '@mantine/hooks/lib/use-form/use-form';
import { motion } from 'framer-motion'; import { motion } from 'framer-motion';
import { useState } from 'react'; import { useState } from 'react';
import { Apps } from 'tabler-icons-react'; import { Apps } from 'tabler-icons-react';
@@ -72,16 +72,7 @@ export default function AddItemShelfItem(props: any) {
); );
} }
function MatchIcon( function MatchIcon(name: string, form: any) {
name: string,
form: UseForm<{
type: any;
name: any;
icon: any;
url: any;
apiKey: any;
}>
) {
// TODO: In order to avoid all the requests, we could fetch // TODO: In order to avoid all the requests, we could fetch
// https://data.jsdelivr.com/v1/package/gh/IceWhaleTech/AppIcon@main // https://data.jsdelivr.com/v1/package/gh/IceWhaleTech/AppIcon@main
// and then iterate over the files -> files -> name and then remove the extension (.png) // and then iterate over the files -> files -> name and then remove the extension (.png)
@@ -102,6 +93,8 @@ function MatchIcon(
export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) { export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & any) {
const { setOpened } = props; const { setOpened } = props;
const { addService, config, setConfig } = useConfig(); const { addService, config, setConfig } = useConfig();
const [isLoading, setLoading] = useState(false);
const form = useForm({ const form = useForm({
initialValues: { initialValues: {
type: props.type ?? 'Other', type: props.type ?? 'Other',
@@ -110,6 +103,23 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
url: props.url ?? '', url: props.url ?? '',
apiKey: props.apiKey ?? (undefined as unknown as string), apiKey: props.apiKey ?? (undefined as unknown as string),
}, },
validate: {
apiKey: (value: string) => null,
// Validate icon with a regex
icon: (value: string) => {
if (!value.match(/^https?:\/\/.+\.(png|jpg|jpeg|gif)$/)) {
return 'Please enter a valid icon URL';
}
return null;
},
// Validate url with a regex http/https
url: (value: string) => {
if (!value.match(/^https?:\/\/.+\/$/)) {
return 'Please enter a valid URL (that ends with a /)';
}
return null;
},
},
}); });
return ( return (
@@ -152,44 +162,40 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
form.setFieldValue('icon', match); form.setFieldValue('icon', match);
} }
}} }}
error={form.errors.name && 'Invalid name'} error={form.errors.name && 'Invalid icon url'}
/> />
<TextInput <TextInput
required required
label="Icon url" label="Icon url"
placeholder="https://i.gifer.com/ANPC.gif" placeholder="https://i.gifer.com/ANPC.gif"
value={form.values.icon} {...form.getInputProps('icon')}
onChange={(event) => {
form.setFieldValue('icon', event.currentTarget.value);
}}
error={form.errors.icon && 'Icon url is invalid'}
/> />
<TextInput <TextInput
required required
label="Service url" label="Service url"
placeholder="http://localhost:8989" placeholder="http://localhost:8989"
value={form.values.url} {...form.getInputProps('url')}
onChange={(event) => form.setFieldValue('url', event.currentTarget.value)}
error={form.errors.url && 'Service url is invalid'}
/> />
<Select <Select
label="Select the type of service (used for API calls)" label="Select the type of service (used for API calls)"
defaultValue="Other" defaultValue="Other"
placeholder="Pick one" placeholder="Pick one"
value={form.values.type}
required required
searchable searchable
onChange={(value) => form.setFieldValue('type', value ?? 'Other')}
data={ServiceTypeList} data={ServiceTypeList}
{...form.getInputProps('type')}
/> />
<LoadingOverlay visible={isLoading} />
{(form.values.type === 'Sonarr' || form.values.type === 'Radarr') && ( {(form.values.type === 'Sonarr' || form.values.type === 'Radarr') && (
<TextInput <TextInput
required required
label="API key" label="API key"
placeholder="Your API key" placeholder="Your API key"
value={form.values.apiKey} value={form.values.apiKey}
onChange={(event) => form.setFieldValue('apiKey', event.currentTarget.value)} onChange={(event) => {
form.setFieldValue('apiKey', event.currentTarget.value);
}}
error={form.errors.apiKey && 'Invalid API key'} error={form.errors.apiKey && 'Invalid API key'}
/> />
)} )}