Skip to content
Open
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
10 changes: 10 additions & 0 deletions src/components/dialogs/contingency-list/contingency-list-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import type { SetRequired } from 'type-fest';
import { prepareContingencyListForBackend } from '../contingency-list-helper';
import { ContingencyListType } from '../../../utils/elementType';
import { FilterAttributes } from '../../../utils/contingency-list-types';

export interface Identifier {
type: 'ID_BASED';
Expand Down Expand Up @@ -71,6 +72,15 @@
...getCriteriaBasedFormData(response),
});

export const getFilterBasedFormDataFromFetchedElement = (response: any, name: string, description: string) => ({
[FieldConstants.NAME]: name,
[FieldConstants.DESCRIPTION]: description,
[FieldConstants.CONTINGENCY_LIST_TYPE]: ContingencyListType.FILTERS.id,
[FieldConstants.FILTERS]: response.filters.map((filter: FilterAttributes) => {

Check failure on line 79 in src/components/dialogs/contingency-list/contingency-list-utils.ts

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.
return { id: filter.id, name: filter.name, specificMetadata: { equipmentType: filter.equipmentType } };
}),
});

export const getExplicitNamingFormDataFromFetchedElement = (response: any, name: string, description: string) => {
let result;
if (response.identifierContingencyList?.identifiers?.length) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function ContingencyListCreationForm() {
const contingencyListTypeField = (
<RadioInput
name={FieldConstants.CONTINGENCY_LIST_TYPE}
options={Object.values(ContingencyListType)}
options={[ContingencyListType.CRITERIA_BASED, ContingencyListType.EXPLICIT_NAMING]}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C'est exactement la même chose non , pourquoi avoir changé ?

formProps={{ onChange: handleChange }} // need to override this in order to do not activate the validate button when changing the filter type
/>
);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
/**
* 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 {
CustomMuiDialog,
FieldConstants,
MAX_CHAR_DESCRIPTION,
useSnackMessage,
yupConfig as yup,
TreeViewFinderNodeProps,
} from '@gridsuite/commons-ui';
import { useForm } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
import { useSelector } from 'react-redux';
import { useCallback, useEffect, useState } from 'react';
import { UUID } from 'crypto';
import ContingencyListFilterBasedFrom from './contingency-list-filter-based-from';
import { AppState } from '../../../../redux/types';
import {
createFilterBasedContingency,
getContingencyList,
saveFilterBasedContingencyList,
} from '../../../../utils/rest-api';
import { handleNotAllowedError } from '../../../utils/rest-errors';
import { ContingencyListType } from '../../../../utils/elementType';
import { getFilterBasedFormDataFromFetchedElement } from '../contingency-list-utils';

export interface FilterBasedContingencyListProps {
titleId: string;
open: boolean;
onClose: () => void;
name?: string;
description?: string;
id?: UUID;
}

const schema: any = yup.object().shape({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Que fait le any ici ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Si c'est parce que tu as un problème type script, vu qu'il suffit d'importer la bonne version de common ui pour régler le problème, tu peux l'enlever

[FieldConstants.NAME]: yup.string().required(),
[FieldConstants.DESCRIPTION]: yup.string().max(MAX_CHAR_DESCRIPTION),
[FieldConstants.FILTERS]: yup.array().required(),

Check failure on line 44 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.
});

export interface ContingencyListFilterBasedFormData {
[FieldConstants.NAME]: string;
[FieldConstants.DESCRIPTION]?: string;
[FieldConstants.FILTERS]: TreeViewFinderNodeProps[];

Check failure on line 50 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.

Check failure on line 50 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

A computed property name in an interface must refer to an expression whose type is a literal type or a 'unique symbol' type.
}

const getContingencyListEmptyFormData = (name = '') => ({
[FieldConstants.NAME]: name,
[FieldConstants.DESCRIPTION]: '',
[FieldConstants.FILTERS]: [],

Check failure on line 56 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.
});

const emptyFormData = (name?: string) => getContingencyListEmptyFormData(name);

export default function FilterBasedContingencyListDialog({
titleId,
open,
onClose,
name,
description,
id,
}: Readonly<FilterBasedContingencyListProps>) {
const activeDirectory = useSelector((state: AppState) => state.activeDirectory);
const { snackError } = useSnackMessage();
const [isFetching, setIsFetching] = useState(!!id);

const methods = useForm<ContingencyListFilterBasedFormData>({
defaultValues: emptyFormData(),
resolver: yupResolver(schema),
});
const {
reset,
formState: { errors },
} = methods;

useEffect(() => {
if (id) {
setIsFetching(true);
getContingencyList(ContingencyListType.FILTERS.id, id?.toString())
.then((response) => {
const formData: ContingencyListFilterBasedFormData = getFilterBasedFormDataFromFetchedElement(
response,
name ?? '',
description ?? ''
);
reset({ ...formData });
})
.catch((error) => {
snackError({
messageTxt: error.message,
headerId: 'cannotRetrieveContingencyList',
});
})
.finally(() => setIsFetching(false));
}
}, [id, name, reset, snackError, description]);

const closeAndClear = useCallback(() => {
reset(emptyFormData());
onClose();
}, [onClose, reset]);

const onSubmit = useCallback(
(data: ContingencyListFilterBasedFormData) => {
if (id) {
saveFilterBasedContingencyList(
id,
data[FieldConstants.NAME],
data[FieldConstants.DESCRIPTION] ?? '',
data[FieldConstants.FILTERS].map((item) => {

Check failure on line 116 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Parameter 'item' implicitly has an 'any' type.

Check failure on line 116 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.

Check failure on line 116 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'ContingencyListFilterBasedFormData'.
return {
id: item.id,
};
})
)
.then(() => closeAndClear())
.catch((error) => {
if (handleNotAllowedError(error, snackError)) {
return;
}
snackError({
messageTxt: error.message,
headerId: 'contingencyListEditingError',
headerValues: { name: data[FieldConstants.NAME] },
});
});
} else {
createFilterBasedContingency(
data[FieldConstants.NAME],
data[FieldConstants.DESCRIPTION] ?? '',
data[FieldConstants.FILTERS]?.map((item: TreeViewFinderNodeProps) => {

Check failure on line 137 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Property 'FILTERS' does not exist on type 'typeof FieldConstants'.

Check failure on line 137 in src/components/dialogs/contingency-list/filter-based/contingency-list-filter-based-dialog.tsx

View workflow job for this annotation

GitHub Actions / build / build

Element implicitly has an 'any' type because expression of type 'any' can't be used to index type 'ContingencyListFilterBasedFormData'.
return {
id: item.id,
};
}),
activeDirectory
)
.then(() => closeAndClear())
.catch((error) => {
if (handleNotAllowedError(error, snackError)) {
return;
}
snackError({
messageTxt: error.message,
headerId: 'contingencyListCreationError',
headerValues: { name: data[FieldConstants.NAME] },
});
});
}
},
[activeDirectory, closeAndClear, id, snackError]
);

const nameError = errors[FieldConstants.NAME];
const isValidating = errors.root?.isValidating;

return (
<CustomMuiDialog
titleId={titleId}
open={open}
onClose={closeAndClear}
onSave={onSubmit}
formSchema={schema}
formMethods={methods}
unscrollableFullHeight
disabledSave={Boolean(!!nameError || isValidating)}
isDataFetching={isFetching}
>
<ContingencyListFilterBasedFrom />
</CustomMuiDialog>
);
}
Loading
Loading