Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/plugin forms #21

Merged
merged 12 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions apps/yadoms-cs/src/app/app-shell/main-app-shell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,13 @@
display: 'none',
},
},
logoBorder: {
borderBottom: '0.0625rem solid #2C2E33',
},
}));

interface NavbarLinkProps {
icon: React.FC<any>;

Check warning on line 101 in apps/yadoms-cs/src/app/app-shell/main-app-shell.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Unexpected any. Specify a different type
label: string;
active?: boolean;

Expand Down Expand Up @@ -153,8 +156,8 @@

const [active, setActive] = useState(activeIndex);

const { classes, cx } = useStyles();

Check warning on line 159 in apps/yadoms-cs/src/app/app-shell/main-app-shell.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

'cx' is assigned a value but never used
const [opened, { toggle, close }] = useDisclosure(false);

Check warning on line 160 in apps/yadoms-cs/src/app/app-shell/main-app-shell.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

'close' is assigned a value but never used

const links = linksData.map((link, index) => (
<NavbarLink
Expand All @@ -170,10 +173,12 @@
<Navbar
height={'100vh'}
width={{ base: 80 }}
p="md"
px="md"
pb="md"
pt="sm"
position={{ top: 0 }}
>
<Center>
<Center className={classes.logoBorder}>
<Logo colorScheme={colorScheme} />
</Center>
<Navbar.Section grow mt={50}>
Expand Down
85 changes: 84 additions & 1 deletion libs/domain/plugins/src/lib/service/plugin-form.service.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { PluginConfigurationSchemaType } from '../model/plugin-configuration-schema.model';
import { getInitialValues, InitialValues } from './plugin-form.service';
import {
getInitialValues,
getInitialValuesFromSectionFields,
InitialValues,
} from './plugin-form.service';

describe('Plugin form store', () => {
describe(`setup the correct form`, () => {
Expand Down Expand Up @@ -274,3 +278,82 @@ describe('Plugin form store', () => {
});
});
});
describe(`getInitialValuesFromSectionFields`, () => {
// TODO: combo section with one content
test(`for comboSection`, () => {
getInitialValuesFromSectionFields(
[
{
key: 'APIKey',
path: 'configuration.APIKey',
field: {
type: 'string',
required: true,
regex: '[a-zA-Z0-9]{64}',
name: "Clé d'API",
description:
'Cette clé est nécessaire pour le fonctionnement de votre plugin. Elle peut être obtenue sur simple inscription (gratuite) au [Lametric](https://developer.lametric.com/user/devices).',
regexErrorMessage: "Ce n'est pas une API KEY valide",
},
},
{
key: 'PairingMode',
path: 'configuration.PairingMode',
field: {
type: 'comboSection',
content: {
Automatic: {
name: 'Appairage automatique',
type: 'section',
content: {
Port: {
type: 'enum',
values: {
Http: 8080,
Https: 4343,
},
defaultValue: 'Https',
name: 'Port',
description:
'Le port de communication de Lametric (exemple Http: 8080, Https: 4343)',
},
},
description:
'Appairage automatique en utilisant le protocole UPNP',
},
Manual: {
name: 'Appairage manuel',
type: 'section',
content: {
IPAddress: {
type: 'string',
regex: '^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$',
required: true,
name: 'Adresse IP',
description: "L'adresse IP du module",
regexErrorMessage: "Ce n'est pas une adresse IP valide",
},
Port: {
type: 'enum',
values: {
Http: 8080,
Https: 4343,
},
defaultValue: 'Https',
name: 'Port',
description:
'Le port de communication de Lametric (exemple Http: 8080, Https: 4343)',
},
},
description: 'Appairage manuel',
},
},
name: "Mode d'appairage",
},
},
],
'',
''
);
});
});
121 changes: 118 additions & 3 deletions libs/domain/plugins/src/lib/service/plugin-form.service.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
import {
EnumField,
PluginConfigurationSchema,
PluginConfigurationSchemaField,
PluginConfigurationSchemaType,
PluginMultiSelectSectionConfigurationSchema,
PluginSectionConfigurationSchema,
} from '../model/plugin-configuration-schema.model';

export interface InitialValues {
type: string;
displayName: string;
configurationSchema: PluginConfigurationSchema;
}

export const getInitialValues = (initialValues: InitialValues) => {
return {
type: initialValues.type,
displayName: initialValues.displayName,
configuration: getFromInitialValues(initialValues.configurationSchema),
};
};
const getFromInitialValues = (
configurationSchema: PluginConfigurationSchema
export const getFromInitialValues = (
configurationSchema:
| PluginConfigurationSchema
| PluginMultiSelectSectionConfigurationSchema
): Record<string, unknown> => {
const newInitialValues: Record<string, unknown> = {};

Expand All @@ -38,7 +45,7 @@ const getFromInitialValues = (
newInitialValues[key] = field.defaultValue ?? 0.0;
break;
case PluginConfigurationSchemaType.Enum:
newInitialValues[key] = field.defaultValue;
newInitialValues[key] = getEnumDefaultValue(field);
break;
case PluginConfigurationSchemaType.Section:
newInitialValues[key] = {
Expand All @@ -59,10 +66,118 @@ const getFromInitialValues = (
};
}
break;
case PluginConfigurationSchemaType.CheckboxSection:
sectionKeys = Object.keys(field.content || {});
if (sectionKeys.length > 0) {
newInitialValues[key] = {
content: getFromInitialValues(field.content || {}),
checkbox: field.defaultValue,
};
} else {
newInitialValues[key] = {
content: getFromInitialValues(field.content || {}),
};
}
break;
case PluginConfigurationSchemaType.MultiSelectSection:
newInitialValues[key] = {
content: getFromInitialValues(field.content || {}),
};
break;
default:
break;
}
}
return newInitialValues;
};

export const getFromInitialValuesTest = (
configurationSchema:
| PluginConfigurationSchema
| PluginSectionConfigurationSchema
| PluginMultiSelectSectionConfigurationSchema,
parentKey = 'configuration'
): Array<{
key: string;
path: string;
field: PluginConfigurationSchemaField;
}> => {
const newInitialValues: Array<{
key: string;
path: string;
field: PluginConfigurationSchemaField;
}> = [];
for (const [key, field] of Object.entries(configurationSchema)) {
const path = parentKey ? `${parentKey}.${key}` : key;

switch (field?.type) {
case PluginConfigurationSchemaType.Enum:
case PluginConfigurationSchemaType.String:
case PluginConfigurationSchemaType.CustomTime:
case PluginConfigurationSchemaType.Boolean:
case PluginConfigurationSchemaType.Decimal:
case PluginConfigurationSchemaType.Integer:
newInitialValues.push({ key: key, path: path, field: field });
break;
case PluginConfigurationSchemaType.Section:
case PluginConfigurationSchemaType.ComboSection:
case PluginConfigurationSchemaType.RadioSection:
case PluginConfigurationSchemaType.CheckboxSection:
case PluginConfigurationSchemaType.MultiSelectSection:
newInitialValues.push({ key: key, path: path, field: field });
break;
default:
}
}
return newInitialValues;
};

export const getInitialValuesFromSectionFields = (
configurationSchema:
| PluginSectionConfigurationSchema
| PluginMultiSelectSectionConfigurationSchema,
parentKey = '',
selectedKey: string
): Array<{
key: string;
path: string;
field: PluginConfigurationSchemaField;
}> => {
const newInitialValues: Array<{
key: string;
path: string;
field: PluginConfigurationSchemaField;
}> = [];
for (const [key, field] of Object.entries(configurationSchema)) {
switch (field?.type) {
case PluginConfigurationSchemaType.CheckboxSection:
newInitialValues.push({
key: key,
path: `${parentKey}.content.${key}`,
field: field,
});
break;
case PluginConfigurationSchemaType.ComboSection:
newInitialValues.push({
key: key,
path: `${parentKey}.${selectedKey}.content.${key}`,
field: field,
});
break;
default:
newInitialValues.push({
key: key,
path: `${parentKey}.${selectedKey}.content.${key}`,
field: field,
});
}
}
return newInitialValues;
};

function getEnumDefaultValue(field: EnumField): string {
if (!field.defaultValue) {
return Object.keys(field.values)[0];
}
return field.defaultValue.toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export interface CustomBoolCheckboxProps {
pluginKey: string;
field: BooleanField;
form: FormReturnType;
path: string;
}

export function CustomBoolCheckbox(props: CustomBoolCheckboxProps) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { CheckboxSectionField } from '@yadoms/domain/plugins';
import {
CheckboxSectionField,
getInitialValuesFromSectionFields,
} from '@yadoms/domain/plugins';
import { Box, Checkbox } from '@mantine/core';
import React, { useState } from 'react';
import React, { useEffect, useState } from 'react';
import renderPluginField from '../../render-plugin-field/render-plugin-field';
import LinkifyText from '../../linkify-text/linkify-text';
import { FormReturnType } from '../../FormReturnType';
Expand All @@ -9,12 +12,19 @@ export interface CustomCheckboxSectionProps {
pluginKey: string;
field: CheckboxSectionField;
form: FormReturnType;
path: string;
}

export function CustomCheckboxSection(props: CustomCheckboxSectionProps) {
const [checked, setChecked] = useState<boolean | undefined>(
!!props.field.defaultValue
);
const CHECKBOX_PATH = `${props.path}.checkbox`;
const CHECKBOX_VALUE = props.form.getInputProps(CHECKBOX_PATH).value;

const [checked, setChecked] = useState<boolean>(!!props.field.defaultValue);

useEffect(() => {
setChecked(CHECKBOX_VALUE);
}, [CHECKBOX_VALUE]);

return (
<Box
sx={(theme) => ({
Expand All @@ -34,15 +44,20 @@ export function CustomCheckboxSection(props: CustomCheckboxSectionProps) {
label={props.field.name}
description={<LinkifyText text={props.field.description} />}
checked={checked}
onChange={(event) => setChecked(event.currentTarget.checked)}
{...props.form.getInputProps(CHECKBOX_PATH, { type: 'checkbox' })}
/>

{checked && (
<div>
{Object.entries(props.field.content).map(([key, value]) =>
{getInitialValuesFromSectionFields(
props.field.content,
props.path,
''
).map(({ key, path, field }) =>
renderPluginField({
field: value,
field: field,
form: props.form,
path: path,
pluginKey: key,
})
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
import React, { forwardRef, useState } from 'react';
import { ItemProps } from '../../plugin-configuration-modal/plugin-configuration-modal';
import renderPluginField from '../../render-plugin-field/render-plugin-field';
import { ComboSectionField } from '@yadoms/domain/plugins';
import {
ComboSectionField,
getInitialValuesFromSectionFields,
} from '@yadoms/domain/plugins';
import LinkifyText from '../../linkify-text/linkify-text';
import { FormReturnType } from '../../FormReturnType';

export interface CustomComboSectionProps {
pluginKey: string;
field: ComboSectionField;
form: FormReturnType;
path: string;
}

const SelectItem = forwardRef<HTMLDivElement, ItemProps>(
Expand All @@ -34,7 +38,8 @@
});
// TODO : to be removed when seb added empty content to Linky plugin
const selectedComboSectionContent =
props.field.content[selectedComboSection]?.content;
props.field.content[selectedComboSection].content;

return (
<Box
sx={(theme) => ({
Expand Down Expand Up @@ -68,10 +73,15 @@
/>
{selectedComboSectionContent && (
<div>
{Object.entries(selectedComboSectionContent).map(([key, value]) =>
{getInitialValuesFromSectionFields(
selectedComboSectionContent,
props.path,
selectedComboSection
).map(({ key, path, field }) =>
renderPluginField({
field: value,
field: field,
form: props.form,
path: path,
pluginKey: key,
})
)}
Expand All @@ -94,7 +104,7 @@
function getComboSectionData(field: ComboSectionField) {
const data: ItemProps[] = [];
if (field.content) {
Object.entries(field.content).map(([key, value]) => {

Check warning on line 107 in libs/pages/plugins/src/lib/ui/custom-plugin-components/custom-combo-section/custom-combo-section.tsx

View workflow job for this annotation

GitHub Actions / build (20.x)

Array.prototype.map() expects a return value from arrow function
data.push({
value: key,
description: value.description,
Expand Down
Loading
Loading