-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathutils.ts
More file actions
180 lines (165 loc) · 6.75 KB
/
utils.ts
File metadata and controls
180 lines (165 loc) · 6.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/**
* Copyright (c) 2025, RTE (http://www.rte-france.com)
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
import { ObjectSchema } from 'yup';
import { SyntheticEvent, useCallback, useState } from 'react';
import { FieldErrors, UseFormReturn } from 'react-hook-form';
import {
isObjectEmpty,
ParameterType,
SpecificParameterInfos,
SpecificParametersDescription,
SpecificParametersValues,
} from '../../../utils';
import { SPECIFIC_PARAMETERS } from './constants';
import * as yup from 'yup';
export const getSpecificParametersFormSchema = (specificParameters: SpecificParameterInfos[] | undefined) => {
const shape: { [key: string]: yup.AnySchema } = {};
specificParameters?.forEach((param: SpecificParameterInfos) => {
switch (param.type) {
case ParameterType.STRING:
shape[param.name] = yup.string().required();
break;
case ParameterType.DOUBLE:
shape[param.name] = yup.number().required();
break;
case ParameterType.INTEGER:
shape[param.name] = yup.number().required();
break;
case ParameterType.BOOLEAN:
shape[param.name] = yup.boolean().required();
break;
case ParameterType.STRING_LIST:
shape[param.name] = yup.array().of(yup.string()).required();
break;
default:
shape[param.name] = yup.mixed().required();
}
});
return yup.object().shape({
[SPECIFIC_PARAMETERS]: yup.object().shape(shape),
});
};
export const getDefaultSpecificParamsValues = (
specificParametersDescriptionForProvider: SpecificParameterInfos[]
): SpecificParametersValues => {
return specificParametersDescriptionForProvider.reduce(
(acc: SpecificParametersValues, param: SpecificParameterInfos) => {
if (param.type === ParameterType.STRING_LIST && param.defaultValue === null) {
acc[param.name] = [];
} else if (
(param.type === ParameterType.DOUBLE || param.type === ParameterType.INTEGER) &&
Number.isNaN(Number(param.defaultValue))
) {
acc[param.name] = 0;
} else {
acc[param.name] = param.defaultValue;
}
return acc;
},
{}
);
};
export const formatSpecificParameters = (
specificParametersDescriptionForProvider: SpecificParameterInfos[],
specificParamsList: SpecificParametersValues
): SpecificParametersValues => {
return specificParametersDescriptionForProvider.reduce(
(acc: SpecificParametersValues, param: SpecificParameterInfos) => {
if (specificParamsList && Object.hasOwn(specificParamsList, param.name)) {
if (param.type === ParameterType.BOOLEAN) {
acc[param.name] = specificParamsList[param.name] === 'true';
} else if (param.type === ParameterType.STRING_LIST) {
acc[param.name] =
specificParamsList[param.name] === ''
? []
: (specificParamsList[param.name] as string).split(',');
} else {
acc[param.name] = specificParamsList[param.name];
}
} else {
acc[param.name] = getDefaultSpecificParamsValues([param])?.[param.name];
}
return acc;
},
{}
);
};
export const getAllSpecificParametersValues = (
formData: Record<string, any>,
_specificParametersValues: SpecificParametersValues
): SpecificParametersValues => {
return Object.keys(formData[SPECIFIC_PARAMETERS]).reduce((acc: SpecificParametersValues, key: string) => {
if (_specificParametersValues[key].toString() !== formData[SPECIFIC_PARAMETERS][key].toString()) {
acc[key] = formData[SPECIFIC_PARAMETERS][key].toString();
}
return acc;
}, {});
};
export const setSpecificParameters = (
provider: string,
specificParamsDescriptions: SpecificParametersDescription | null,
formMethods: UseFormReturn
) => {
const specificParams = provider ? (specificParamsDescriptions?.[provider] ?? []) : [];
const specificParamsValues = getDefaultSpecificParamsValues(specificParams);
formMethods.setValue(SPECIFIC_PARAMETERS, specificParamsValues);
};
type UseTabsReturn<TTabValue extends string> = {
selectedTab: TTabValue;
tabsWithError: TTabValue[];
onTabChange: (event: SyntheticEvent, newValue: TTabValue) => void;
onError: (errors: FieldErrors) => void;
};
export type UseComputationParametersFormReturn<TTabValue extends string> = UseTabsReturn<TTabValue> & {
formMethods: UseFormReturn;
formSchema: ObjectSchema<any>;
paramsLoaded: boolean;
formattedProviders: { id: string; label: string }[];
};
type UseTabsProps<TTabValue extends string> = {
defaultTab: TTabValue;
tabEnum: Record<string, TTabValue>;
};
export function useTabs<TTabValue extends string>({
defaultTab,
tabEnum,
}: Readonly<UseTabsProps<TTabValue>>): UseTabsReturn<TTabValue> {
const [tabValue, setTabValue] = useState<TTabValue>(defaultTab);
const [tabValuesWithError, setTabValuesWithError] = useState<TTabValue[]>([]);
const handleTabChange = useCallback((event: SyntheticEvent<Element, Event>, newValue: TTabValue) => {
setTabValue(newValue);
}, []);
const onError = useCallback(
(errors: FieldErrors) => {
if (!errors || isObjectEmpty(errors)) {
return;
}
const tabsInError: TTabValue[] = [];
// do not show error when being in the current tab
Object.values(tabEnum).forEach((tab) => {
if (errors?.[tab] && tab !== tabValue) {
tabsInError.push(tab);
}
});
if (tabsInError.includes(tabValue)) {
// error in current tab => do not change tab systematically but remove current tab in error list
setTabValuesWithError(tabsInError.filter((errorTab) => errorTab !== tabValue));
} else if (tabsInError.length > 0) {
// switch to the first tab in the list then remove the tab in the error list
setTabValue(tabsInError[0]);
setTabValuesWithError(tabsInError.filter((errorTab, index, arr) => errorTab !== arr[0]));
}
},
[tabValue, tabEnum]
);
return {
selectedTab: tabValue,
tabsWithError: tabValuesWithError,
onTabChange: handleTabChange,
onError,
};
}