🐛 Fix nullable integration type

This commit is contained in:
Manuel Ruwe
2022-12-12 21:53:30 +01:00
parent 1861a8d9d2
commit 4840548946
2 changed files with 37 additions and 35 deletions

View File

@@ -3,7 +3,6 @@ import { Group, Select, SelectItem, Text } from '@mantine/core';
import { UseFormReturnType } from '@mantine/form'; import { UseFormReturnType } from '@mantine/form';
import { useTranslation } from 'next-i18next'; import { useTranslation } from 'next-i18next';
import { forwardRef } from 'react'; import { forwardRef } from 'react';
import { IntegrationsType } from '../../../../../../../../types/integration';
import { import {
IntegrationField, IntegrationField,
integrationFieldDefinitions, integrationFieldDefinitions,
@@ -54,12 +53,15 @@ export const IntegrationSelector = ({ form }: IntegrationSelectorProps) => {
}, },
].filter((x) => Object.keys(integrationFieldProperties).includes(x.value)); ].filter((x) => Object.keys(integrationFieldProperties).includes(x.value));
const inputProps = form.getInputProps('integration.type');
const getNewProperties = (value: string | null): ServiceIntegrationPropertyType[] => { const getNewProperties = (value: string | null): ServiceIntegrationPropertyType[] => {
if (!value) return []; if (!value) return [];
const integrationType = value as ServiceIntegrationType['type'];
if (integrationType === null) {
return [];
}
const requiredProperties = Object.entries(integrationFieldDefinitions).filter(([k, v]) => { const requiredProperties = Object.entries(integrationFieldDefinitions).filter(([k, v]) => {
const val = integrationFieldProperties[value as ServiceIntegrationType['type']]; const val = integrationFieldProperties[integrationType['type']];
return val.includes(k as IntegrationField); return val.includes(k as IntegrationField);
})!; })!;
return requiredProperties.map(([k, value]) => ({ return requiredProperties.map(([k, value]) => ({
@@ -70,8 +72,9 @@ export const IntegrationSelector = ({ form }: IntegrationSelectorProps) => {
})); }));
}; };
const inputProps = form.getInputProps('integration.type');
return ( return (
<>
<Select <Select
label="Integration configuration" label="Integration configuration"
description="Treats this service as the selected integration and provides you with per-service configuration" description="Treats this service as the selected integration and provides you with per-service configuration"
@@ -92,14 +95,13 @@ export const IntegrationSelector = ({ form }: IntegrationSelectorProps) => {
/> />
) )
} }
{...inputProps}
onChange={(value) => { onChange={(value) => {
form.setFieldValue('integration.properties', getNewProperties(value)); form.setFieldValue('integration.properties', getNewProperties(value));
console.log(`changed to value ${value}`); console.log(`changed to value ${value}`);
inputProps.onChange(value); inputProps.onChange(value);
}} }}
{...inputProps}
/> />
</>
); );
}; };

View File

@@ -16,7 +16,7 @@ export interface ServiceType extends TileBaseType {
behaviour: ServiceBehaviourType; behaviour: ServiceBehaviourType;
network: ServiceNetworkType; network: ServiceNetworkType;
appearance: ServiceAppearanceType; appearance: ServiceAppearanceType;
integration?: ServiceIntegrationType | null; integration: ServiceIntegrationType;
} }
export type ConfigServiceType = Omit<ServiceType, 'integration'> & { export type ConfigServiceType = Omit<ServiceType, 'integration'> & {
@@ -51,7 +51,7 @@ export type IntegrationType =
| 'nzbGet'; | 'nzbGet';
export type ServiceIntegrationType = { export type ServiceIntegrationType = {
type: IntegrationType; type: IntegrationType | null;
properties: ServiceIntegrationPropertyType[]; properties: ServiceIntegrationPropertyType[];
}; };