From 09a8dd7db8b0bd0a3e5e81ef4fe06b1c98ba36b7 Mon Sep 17 00:00:00 2001 From: ajnart Date: Tue, 2 Aug 2022 00:20:04 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20add=20or=20modify=20servic?= =?UTF-8?q?e=20undefined=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/components/AppShelf/AddAppShelfItem.tsx | 41 +++++++++++---------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/src/components/AppShelf/AddAppShelfItem.tsx b/src/components/AppShelf/AddAppShelfItem.tsx index 39f966cea..d0c0d832a 100644 --- a/src/components/AppShelf/AddAppShelfItem.tsx +++ b/src/components/AppShelf/AddAppShelfItem.tsx @@ -85,25 +85,26 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & const [isLoading, setLoading] = useState(false); // Extract all the categories from the services in config - const categoryList = config.services.reduce((acc, cur) => { + const InitialCategories = config.services.reduce((acc, cur) => { if (cur.category && !acc.includes(cur.category)) { acc.push(cur.category); } return acc; }, [] as string[]); + const [categories, setCategories] = useState(InitialCategories); const form = useForm({ initialValues: { id: props.id ?? uuidv4(), type: props.type ?? 'Other', - category: props.category ?? undefined, + category: props.category ?? null, name: props.name ?? '', icon: props.icon ?? DEFAULT_ICON, url: props.url ?? '', - apiKey: props.apiKey ?? (undefined as unknown as string), - username: props.username ?? (undefined as unknown as string), - password: props.password ?? (undefined as unknown as string), - openedUrl: props.openedUrl ?? (undefined as unknown as string), + apiKey: props.apiKey ?? undefined, + username: props.username ?? undefined, + password: props.password ?? undefined, + openedUrl: props.openedUrl ?? undefined, status: props.status ?? ['200'], newTab: props.newTab ?? true, }, @@ -162,21 +163,21 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } &
{ - if (JSON.stringify(form.values.status) === JSON.stringify(['200'])) { - form.values.status = undefined; - } - if (form.values.newTab === true) { - form.values.newTab = undefined; + const newForm = { ...form.values }; + if (newForm.newTab === true) newForm.newTab = undefined; + if (newForm.category === null) newForm.category = undefined; + if (newForm.status.length === 1 && newForm.status[0] === '200') { + delete newForm.status; } // If service already exists, update it. - if (config.services && config.services.find((s) => s.id === form.values.id)) { + if (config.services && config.services.find((s) => s.id === newForm.id)) { setConfig({ ...config, // replace the found item by matching ID services: config.services.map((s) => { - if (s.id === form.values.id) { + if (s.id === newForm.id) { return { - ...form.values, + ...newForm, }; } return s; @@ -185,7 +186,7 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & } else { setConfig({ ...config, - services: [...config.services, form.values], + services: [...config.services, newForm], }); } setOpened(false); @@ -205,7 +206,6 @@ export function AddAppShelfItemForm(props: { setOpened: (b: boolean) => void } & placeholder="Plex" {...form.getInputProps('name')} /> - void } & />