Skip to content

Commit 4007df0

Browse files
authored
[GridDyna] Using expert filter for mapping (#485)
1 parent f75f1db commit 4007df0

File tree

6 files changed

+81
-44
lines changed

6 files changed

+81
-44
lines changed

src/components/filter/expert/expert-filter-constants.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ export const FIELDS_OPTIONS = {
243243
name: FieldType.VOLTAGE_REGULATOR_ON,
244244
label: 'voltageRegulatorOn',
245245
dataType: DataType.BOOLEAN,
246+
defaultValue: false,
246247
valueEditorType: 'switch',
247248
},
248249
PLANNED_ACTIVE_POWER_SET_POINT: {
@@ -255,6 +256,7 @@ export const FIELDS_OPTIONS = {
255256
name: FieldType.CONNECTED,
256257
label: 'connected',
257258
dataType: DataType.BOOLEAN,
259+
defaultValue: false,
258260
valueEditorType: 'switch',
259261
},
260262
RATED_S: {
@@ -376,12 +378,14 @@ export const FIELDS_OPTIONS = {
376378
name: FieldType.CONNECTED_1,
377379
label: 'terminal1Connected',
378380
dataType: DataType.BOOLEAN,
381+
defaultValue: false,
379382
valueEditorType: 'switch',
380383
},
381384
CONNECTED_2: {
382385
name: FieldType.CONNECTED_2,
383386
label: 'terminal2Connected',
384387
dataType: DataType.BOOLEAN,
388+
defaultValue: false,
385389
valueEditorType: 'switch',
386390
},
387391
VOLTAGE_LEVEL_ID_1: {
@@ -492,12 +496,14 @@ export const FIELDS_OPTIONS = {
492496
name: FieldType.HAS_RATIO_TAP_CHANGER,
493497
label: 'hasRatioTapChanger',
494498
dataType: DataType.BOOLEAN,
499+
defaultValue: false,
495500
valueEditorType: 'switch',
496501
},
497502
LOAD_TAP_CHANGING_CAPABILITIES: {
498503
name: FieldType.LOAD_TAP_CHANGING_CAPABILITIES,
499504
label: 'loadTapChangingCapabilities',
500505
dataType: DataType.BOOLEAN,
506+
defaultValue: false,
501507
valueEditorType: 'switch',
502508
},
503509
RATIO_REGULATION_MODE: {
@@ -518,6 +524,7 @@ export const FIELDS_OPTIONS = {
518524
name: FieldType.HAS_PHASE_TAP_CHANGER,
519525
label: 'hasPhaseTapChanger',
520526
dataType: DataType.BOOLEAN,
527+
defaultValue: false,
521528
valueEditorType: 'switch',
522529
},
523530
PHASE_REGULATION_MODE: {

src/components/filter/expert/expert-filter-form.tsx

Lines changed: 17 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -51,55 +51,29 @@ function isSupportedEquipmentType(equipmentType: string): boolean {
5151
.includes(equipmentType);
5252
}
5353

54+
export const rqbQuerySchemaValidator = (schema: yup.Schema) =>
55+
schema
56+
.test(RULES.EMPTY_GROUP, RULES.EMPTY_GROUP, (query: any) => {
57+
return testQuery(RULES.EMPTY_GROUP, query as RuleGroupTypeAny);
58+
})
59+
.test(RULES.EMPTY_RULE, RULES.EMPTY_RULE, (query: any) => {
60+
return testQuery(RULES.EMPTY_RULE, query as RuleGroupTypeAny);
61+
})
62+
.test(RULES.INCORRECT_RULE, RULES.INCORRECT_RULE, (query: any) => {
63+
return testQuery(RULES.INCORRECT_RULE, query as RuleGroupTypeAny);
64+
})
65+
.test(RULES.BETWEEN_RULE, RULES.BETWEEN_RULE, (query: any) => {
66+
return testQuery(RULES.BETWEEN_RULE, query as RuleGroupTypeAny);
67+
});
68+
5469
export const expertFilterSchema = {
5570
[EXPERT_FILTER_QUERY]: yup.object().when([FieldConstants.FILTER_TYPE], {
5671
is: FilterType.EXPERT.id,
57-
then: (schema: any) =>
72+
then: (schema: yup.Schema) =>
5873
schema.when([FieldConstants.EQUIPMENT_TYPE], {
5974
is: (equipmentType: string) =>
6075
isSupportedEquipmentType(equipmentType),
61-
then: (innerSchema: any) =>
62-
innerSchema
63-
.test(
64-
RULES.EMPTY_GROUP,
65-
RULES.EMPTY_GROUP,
66-
(query: any) => {
67-
return testQuery(
68-
RULES.EMPTY_GROUP,
69-
query as RuleGroupTypeAny
70-
);
71-
}
72-
)
73-
.test(
74-
RULES.EMPTY_RULE,
75-
RULES.EMPTY_RULE,
76-
(query: any) => {
77-
return testQuery(
78-
RULES.EMPTY_RULE,
79-
query as RuleGroupTypeAny
80-
);
81-
}
82-
)
83-
.test(
84-
RULES.INCORRECT_RULE,
85-
RULES.INCORRECT_RULE,
86-
(query: any) => {
87-
return testQuery(
88-
RULES.INCORRECT_RULE,
89-
query as RuleGroupTypeAny
90-
);
91-
}
92-
)
93-
.test(
94-
RULES.BETWEEN_RULE,
95-
RULES.BETWEEN_RULE,
96-
(query: any) => {
97-
return testQuery(
98-
RULES.BETWEEN_RULE,
99-
query as RuleGroupTypeAny
100-
);
101-
}
102-
),
76+
then: rqbQuerySchemaValidator,
10377
}),
10478
}),
10579
};

src/components/filter/expert/expert-filter-utils.ts

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
} from 'react-querybuilder';
1919
import { IntlShape } from 'react-intl';
2020
import { validate as uuidValidate } from 'uuid';
21+
import { UUID } from 'crypto';
2122
import {
2223
CombinatorType,
2324
CompositeField,
@@ -251,6 +252,7 @@ export function exportExpertRules(query: RuleGroupType): RuleGroupTypeExport {
251252

252253
// a single rule
253254
return {
255+
id: rule.id as UUID,
254256
field: rule.field as FieldType,
255257
operator:
256258
dataType !== DataType.PROPERTY
@@ -296,6 +298,7 @@ export function exportExpertRules(query: RuleGroupType): RuleGroupTypeExport {
296298
);
297299

298300
return {
301+
id: compositeGroup.id as UUID,
299302
combinator: compositeGroup.combinator as CombinatorType,
300303
dataType: DataType.COMBINATOR,
301304
rules: transformedRules,
@@ -317,6 +320,7 @@ export function exportExpertRules(query: RuleGroupType): RuleGroupTypeExport {
317320
});
318321

319322
return {
323+
id: group.id as UUID,
320324
combinator: group.combinator as CombinatorType,
321325
dataType: DataType.COMBINATOR,
322326
rules: transformedRules,
@@ -356,6 +360,7 @@ export function importExpertRules(query: RuleGroupTypeExport): RuleGroupType {
356360

357361
function transformRule(rule: RuleTypeExport): RuleType {
358362
return {
363+
id: rule.id,
359364
field: rule.field,
360365
operator:
361366
rule.dataType !== DataType.PROPERTY
@@ -382,6 +387,7 @@ export function importExpertRules(query: RuleGroupTypeExport): RuleGroupType {
382387
);
383388

384389
return {
390+
id: group.id,
385391
field: group.field as FieldType,
386392
operator: Object.values(OPERATOR_OPTIONS).find(
387393
(operator) => operator.customName === group.operator
@@ -408,6 +414,7 @@ export function importExpertRules(query: RuleGroupTypeExport): RuleGroupType {
408414
});
409415

410416
return {
417+
id: group.id,
411418
combinator: group.combinator,
412419
rules: transformedRules,
413420
};
@@ -417,6 +424,10 @@ export function importExpertRules(query: RuleGroupTypeExport): RuleGroupType {
417424
}
418425

419426
export function countRules(query: RuleGroupTypeAny): number {
427+
if (!query) {
428+
return 0;
429+
}
430+
420431
if ('rules' in query) {
421432
const group = query as RuleGroupType;
422433
return group.rules.reduce(
@@ -432,6 +443,10 @@ export function countRules(query: RuleGroupTypeAny): number {
432443
export const queryValidator: QueryValidator = (query) => {
433444
const result: ValidationMap = {};
434445

446+
if (!query) {
447+
return result;
448+
}
449+
435450
const validateRule = (rule: RuleType) => {
436451
const isValueAnArray = Array.isArray(rule.value);
437452
const dataType = getDataType(rule.field, rule.operator);
@@ -579,7 +594,7 @@ export function getNumberOfSiblings(path: number[], query: RuleGroupTypeAny) {
579594
// Find the parent group object in the query
580595
const parentGroup = findPath(parentPath, query) as RuleGroupType;
581596
// Return the number of siblings
582-
return parentGroup.rules.length;
597+
return parentGroup?.rules?.length;
583598
}
584599

585600
// Remove a rule or group and its parents if they become empty

src/components/filter/expert/expert-filter.type.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
66
*/
77
import { FullField } from 'react-querybuilder';
8+
import { UUID } from 'crypto';
89

910
export enum OperatorType {
1011
EQUALS = 'EQUALS',
@@ -130,6 +131,7 @@ export type OperatorOption = {
130131
// This type is equivalent to a (partial) union type of BooleanExpertRule,
131132
// NumberExpertRule, StringExpertRule, PropertiesExpertRule in filter library
132133
export interface RuleTypeExport {
134+
id?: UUID;
133135
field: FieldType;
134136
operator: OperatorType;
135137
value: string | number | undefined;
@@ -141,6 +143,7 @@ export interface RuleTypeExport {
141143

142144
// This type is equivalent to CombinatorExpertRule in filter library
143145
export interface RuleGroupTypeExport {
146+
id?: UUID;
144147
combinator: CombinatorType;
145148
dataType: DataType;
146149
field?: FieldType; // used in case of composite rule
@@ -156,6 +159,7 @@ export interface CompositeField extends FullField {
156159

157160
// typing composite rule value
158161
export interface CompositeGroup {
162+
id?: string;
159163
combinator: string;
160164
rules: {
161165
[field: string]: CompositeRule;

src/hooks/usePrevious.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* Copyright (c) 2024, RTE (http://www.rte-france.com)
3+
* This Source Code Form is subject to the terms of the Mozilla Public
4+
* License, v. 2.0. If a copy of the MPL was not distributed with this
5+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
6+
*/
7+
import { useEffect, useRef } from 'react';
8+
9+
export function usePrevious<T = undefined>(value: T) {
10+
const ref = useRef<T>();
11+
useEffect(() => {
12+
ref.current = value;
13+
}, [value]);
14+
return ref.current;
15+
}
16+
17+
export default usePrevious;

src/index.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,25 @@ export {
5858

5959
export { default as FieldConstants } from './utils/field-constants';
6060

61+
export { fields as EXPERT_FILTER_FIELDS } from './components/filter/expert/expert-filter-constants';
62+
export { default as CustomReactQueryBuilder } from './components/inputs/react-query-builder/custom-react-query-builder';
63+
export {
64+
EXPERT_FILTER_QUERY,
65+
rqbQuerySchemaValidator,
66+
getExpertFilterEmptyFormData,
67+
} from './components/filter/expert/expert-filter-form';
68+
export {
69+
importExpertRules,
70+
exportExpertRules,
71+
} from './components/filter/expert/expert-filter-utils';
72+
export type {
73+
RuleTypeExport,
74+
RuleGroupTypeExport,
75+
} from './components/filter/expert/expert-filter.type';
76+
export { formatQuery } from 'react-querybuilder';
77+
78+
export { default as yup } from './utils/yup-config';
79+
6180
export type { TreeViewFinderNodeProps } from './components/TreeViewFinder/TreeViewFinder';
6281

6382
export {
@@ -166,6 +185,7 @@ export { default as CardErrorBoundary } from './components/CardErrorBoundary';
166185
export { default as useIntlRef } from './hooks/useIntlRef';
167186
export { useSnackMessage } from './hooks/useSnackMessage';
168187
export { default as useDebounce } from './hooks/useDebounce';
188+
export { default as usePrevious } from './hooks/usePrevious';
169189
export { default as SelectClearable } from './components/inputs/select-clearable';
170190
export { default as useCustomFormContext } from './components/inputs/react-hook-form/provider/use-custom-form-context';
171191
export { default as CustomFormProvider } from './components/inputs/react-hook-form/provider/custom-form-provider';

0 commit comments

Comments
 (0)