Skip to content

Commit

Permalink
handle comboSection and Decimal type for form
Browse files Browse the repository at this point in the history
  • Loading branch information
oben01 committed May 27, 2023
1 parent ce86012 commit bafd0cd
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ export enum PluginConfigurationSchemaType {
String = 'string',
Integer = 'int',
Boolean = 'bool',
Decimal = 'decimal',
Section = 'section',
CustomTime = 'time',
Enum = 'enum',
ComboSection = 'comboSection',
RadioSection = 'radioSection',
CheckboxSection = 'checkboxSection',
CustomTime = 'time',
Enum = 'enum',
Decimal = 'decimal',
MultiSelectSection = 'multiSelectSection',
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ export function PluginConfigurationModal(props: PluginConfigurationModalProps) {
const newInitialValues = getFromInitialValues(
props.selectedPluginConfigurationSchema
);
console.log(
'props.selectedPluginConfigurationSchema',
props.selectedPluginConfigurationSchema
);
console.log('newInitialValues', newInitialValues);
setInitialValues(newInitialValues);
}, [props.selectedPluginConfigurationSchema]);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getFromInitialValues } from './plugins-configuration-forms';
import {
PluginConfigurationSchema,
PluginConfigurationSchemaType,
PluginSectionConfigurationSchema,
} from '@yadoms/domain/plugins';

describe('Plugin configuration forms', () => {
Expand Down Expand Up @@ -76,5 +77,68 @@ describe('Plugin configuration forms', () => {
});
});
});
describe(`for decimal type`, () => {
test(`should return 0.0 when defaultValue does not exist`, () => {
const configurationSchema: PluginConfigurationSchema = {
APIKey: {
type: PluginConfigurationSchemaType.Decimal,
},
};
expect(getFromInitialValues(configurationSchema)).toStrictEqual({
APIKey: 0.0,
});
});
test(`should return default value when the value exist`, () => {
const configurationSchema: PluginConfigurationSchema = {
APIKey: {
type: PluginConfigurationSchemaType.Decimal,
defaultValue: 1.2,
},
};
expect(getFromInitialValues(configurationSchema)).toStrictEqual({
APIKey: 1.2,
});
});
});
describe(`for ComboSection type`, () => {
test(`should return empty when ComboSection content is empty`, () => {
const configurationSchema: PluginSectionConfigurationSchema = {
emptySectionContent: {
type: PluginConfigurationSchemaType.ComboSection,
content: {},
},
};
const actualFormInitialValues =
getFromInitialValues(configurationSchema);
expect(actualFormInitialValues['emptySectionContent'].content).toEqual(
{}
);
});
test(`should return active comboSection`, () => {
const configurationSchema: PluginSectionConfigurationSchema = {
emptySectionContent: {
type: PluginConfigurationSchemaType.ComboSection,
content: {
activeSection: {
type: PluginConfigurationSchemaType.Section,
content: {},
},
inactiveSection: {
type: PluginConfigurationSchemaType.Section,
content: {},
},
},
},
};
const actualFormInitialValues =
getFromInitialValues(configurationSchema);
expect(
actualFormInitialValues['emptySectionContent'].activeSection
).toEqual('activeSection');
expect(
actualFormInitialValues['emptySectionContent'].activeSectionText
).toEqual('activeSection');
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import {
PluginConfigurationSchema,
PluginConfigurationSchemaField,
PluginConfigurationSchemaType,
} from '@yadoms/domain/plugins';

export type InitialValues = {
[key: string]: string | number | boolean;
};
export function getFromInitialValues(
PluginConfigurationSchema: PluginConfigurationSchema
): InitialValues {
const newInitialValues: InitialValues = {};
for (const [key, field] of Object.entries(PluginConfigurationSchema)) {
configurationSchema: PluginConfigurationSchema
): Record<string, unknown> {
const newInitialValues: Record<string, unknown> = {};
for (const [key, field] of Object.entries(configurationSchema)) {
let sectionKeys: string[] = [];
let firstSectionKey: string | undefined;

switch (field.type) {
case PluginConfigurationSchemaType.String:
newInitialValues[key] = field.defaultValue ?? '';
Expand All @@ -22,18 +21,29 @@ export function getFromInitialValues(
case PluginConfigurationSchemaType.Boolean:
newInitialValues[key] = field.defaultValue ?? false;
break;
case PluginConfigurationSchemaType.Section:
for (const [subKey, subField] of Object.entries(field.content || {})) {
if (subField.defaultValue !== undefined) {
newInitialValues[`${key}.${subKey}`] = subField.defaultValue;
}
case PluginConfigurationSchemaType.Decimal:
newInitialValues[key] = field.defaultValue ?? 0.0;
break;
case PluginConfigurationSchemaType.ComboSection:
sectionKeys = Object.keys(field.content || {});
if (sectionKeys.length > 0) {
firstSectionKey = sectionKeys[0];
newInitialValues[key] = {
content: getFromInitialValues(field.content || {}),
activeSection: firstSectionKey,
activeSectionText: firstSectionKey,
};
} else {
newInitialValues[key] = {
content: getFromInitialValues(field.content || {}),
};
}
break;
default:
break;
}
}
return newInitialValues;
return newInitialValues as Record<string, unknown>;
}

export const validateForm = (
Expand Down
194 changes: 93 additions & 101 deletions note_technique.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,116 +11,108 @@ queryParam "locale=fr"

```json
{
"id": 6,
"displayName": "LametricTime",
"type": "LametricTime",
"version": "1.0.2",
"author": "Oussama DAHMAZ",
"url": "",
"supportManuallyCreatedDevice": false,
"supportDeviceRemovedNotification": false,
"package": {
"type": "LametricTime",
"version": "1.0.2",
"author": "Oussama DAHMAZ",
"credits": "",
"supportedPlatforms": "all",
"dependencies": {
"yadoms": {
"minimumVersion": "2.4.0-beta.1"
}
},
"configurationSchema": {
"APIKey": {
"type": "string",
"required": "true",
"regex": "[a-zA-Z0-9]{64}"
},
"PairingMode": {
"type": "comboSection",
"content": {
"Automatic": {
"name": "Auto",
"type": "section",
"content": {
"Port": {
"type": "enum",
"values": {
"Http": "8080",
"Https": "4343"
},
"defaultValue": "Https"
}
}
},
"Manual": {
"name": "Manual",
"type": "section",
"content": {
"IPAddress": {
"type": "string",
"regex": "^(?:[0-9]{1,3}\\.){3}[0-9]{1,3}$",
"required": "true"
},
"Port": {
"type": "enum",
"values": {
"Http": "8080",
"Https": "4343"
},
"defaultValue": "Https"
}
}
"configuration": {
"APIKey": "cb6860047451c15e28b4bad6a96f0dec5022d625233846909525a90b585b1483",
"PairingMode": {
"content": {
"Automatic": {
"content": {
"Port": "Https"
}
},
"Manual": {
"content": {
"IPAddress": "",
"Port": "Https"
}
}
}
},
"activeSection": "Automatic",
"activeSectionText": "Auto"
}
},
"locales": {
"name": "LametricTime",
"description": "Support de l'équipement Lametric Time (voir [site web Lametric](https://lametric.com/en-US/time/overview), comment utiliser le [plugin LametricTime](https://github.com/Yadoms/yadoms/wiki/LametricTime))",
"configurationSchema": {
"APIKey": {
"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"
"autoStart": true,
"category": "User"
}
```

```json
{
"id": 7,
"displayName": "LametricTime",
"type": "LametricTime",
"configuration": {
"APIKey": "cb6860047451c15e28b4bad6a96f0dec5022d625233846909525a90b585b1483",
"PairingMode": {
"content": {
"Automatic": {
"content": {
"Port": "Https"
}
},
"Manual": {
"content": {
"IPAddress": "123.12.12.1",
"Port": "Https"
}
}
},
"PairingMode": {
"name": "Mode d'appairage",
"type": "comboSection",
"content": {
"Automatic": {
"name": "Appairage automatique",
"description": "Appairage automatique en utilisant le protocole UPNP",
"content": {
"Port": {
"name": "Port",
"description": "Le port de communication de Lametric (exemple Http: 8080, Https: 4343)"
}
}
},
"Manual": {
"name": "Appairage manuel",
"description": "Appairage manuel",
"content": {
"IPAddress": {
"name": "Adresse IP",
"description": "L'adresse IP du module",
"regexErrorMessage": "Ce n'est pas une adresse IP valide"
},
"Port": {
"name": "Port",
"description": "Le port de communication de Lametric (exemple Http: 8080, Https: 4343)"
}
}
"activeSection": "Manual",
"activeSectionText": "Manual"
}
},
"autoStart": true,
"category": "User"
}
```

```json
{
"id": 8,
"displayName": "linky",
"type": "Linky",
"configuration": {
"SerialPort": "/dev/tty.Bluetooth-Incoming-Port",
"EquipmentType": {
"content": {
"first": {
"content": {}
},
"second": {
"content": {
"portEnabled": "AllInputsEnabled"
}
}
}
},
"customLabels": {
"pluginState": {
"failedToConnect": "La connexion a échoué...",
"initializationError": "Erreur d'initialisation"
},
"activeSection": "first",
"activeSectionText": "1xTIC"
}
},
"autoStart": true,
"category": "User"
}
```

```json
{
"id": 9,
"displayName": "Sigfox",
"type": "Sigfox",
"configuration": {
"port": 8090,
"advancedConfiguration": {
"content": {
"rssiMin": -150,
"rssiMax": -100,
"tensionMin": 2.4,
"tensionMax": 3.6
}
}
}
},
"autoStart": true,
"category": "User"
}
```

0 comments on commit bafd0cd

Please sign in to comment.