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
70 changes: 44 additions & 26 deletions src/components/inputs/reactHookForm/DirectoryItemsInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,22 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

import { Chip, FormControl, Grid, IconButton, Theme, Tooltip } from '@mui/material';
import { Box, Chip, FormControl, FormHelperText, Grid, IconButton, Theme, Tooltip } from '@mui/material';
import { Folder as FolderIcon } from '@mui/icons-material';
import { useCallback, useMemo, useState } from 'react';
import { FieldValues, useController, useFieldArray } from 'react-hook-form';
import { useIntl } from 'react-intl';
import { FormattedMessage, useIntl } from 'react-intl';
import { UUID } from 'crypto';
import { RawReadOnlyInput } from './RawReadOnlyInput';
import { FieldLabel } from './utils/FieldLabel';
import { useCustomFormContext } from './provider/useCustomFormContext';
import { isFieldRequired } from './utils/functions';
import { ErrorInput } from './errorManagement/ErrorInput';
import { useSnackMessage } from '../../../hooks/useSnackMessage';
import { FieldLabel, isFieldRequired } from './utils';
import { useCustomFormContext } from './provider';
import { ErrorInput, MidFormError } from './errorManagement';
import { useSnackMessage } from '../../../hooks';
import { TreeViewFinderNodeProps } from '../../treeViewFinder';
import { mergeSx } from '../../../utils/styles';
import { OverflowableText } from '../../overflowableText';
import { MidFormError } from './errorManagement/MidFormError';
import { DirectoryItemSelector } from '../../directoryItemSelector/DirectoryItemSelector';
import { DirectoryItemSelector } from '../../directoryItemSelector';
import { fetchDirectoryElementPath } from '../../../services';
import { ElementAttributes } from '../../../utils';
import { getBasicEquipmentLabel, ElementAttributes, mergeSx } from '../../../utils';
import { NAME } from './constants';

const styles = {
Expand Down Expand Up @@ -67,6 +64,7 @@ export interface DirectoryItemsInputProps {
disable?: boolean;
allowMultiSelect?: boolean;
labelRequiredFromContext?: boolean;
equipmentColorsMap?: Map<string, string>;
}

export function DirectoryItemsInput({
Expand All @@ -82,6 +80,7 @@ export function DirectoryItemsInput({
disable = false,
allowMultiSelect = true,
labelRequiredFromContext = true,
equipmentColorsMap,
}: Readonly<DirectoryItemsInputProps>) {
const { snackError } = useSnackMessage();
const intl = useIntl();
Expand All @@ -97,6 +96,7 @@ export function DirectoryItemsInput({
} = useFieldArray({
Copy link
Contributor

Choose a reason for hiding this comment

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

À mon avis il n'y a plus besoin de renommer fields ici vu qu'il est déjà renommé en bas, autant garder le nom d'origine ici plutôt que de le changer 2 fois

name,
});
const elementsWithMetaData: TreeViewFinderNodeProps[] = elements as unknown as TreeViewFinderNodeProps[];
Copy link
Contributor

Choose a reason for hiding this comment

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

On est sûr que les champs correspondent toujours à ce type TreeViewFinderNodeProps ?
J'ai essayé de remonter pour voir où se composant était utilisé et franchement j'ai toujours aucune idée d'à quoi ressemble ce formulaire.
En tout cas rien que le nom est bizarre, d'après le nom c'est censé être utilisé uniquement dans l'arbre mais je vois qu'il est un peu partout...


const formContext = useCustomFormContext();
const { getValues, validationSchema } = formContext;
Expand Down Expand Up @@ -180,27 +180,45 @@ export function DirectoryItemsInput({
)}
error={!!error?.message}
>
{elements?.length === 0 && label && (
{elementsWithMetaData?.length === 0 && label && (
<FieldLabel
label={label}
optional={labelRequiredFromContext && !isFieldRequired(name, validationSchema, getValues())}
/>
)}
{elements?.length > 0 && (
{elementsWithMetaData?.length > 0 && (
<FormControl sx={styles.formDirectoryElements2}>
{elements.map((item, index) => (
<Chip
key={item.id}
size="small"
onDelete={() => removeElements(index)}
onClick={() => handleChipClick(index)}
label={
<OverflowableText
text={<RawReadOnlyInput name={`${name}.${index}.${NAME}`} />}
sx={{ width: '100%' }}
/>
}
/>
{elementsWithMetaData.map((item, index) => (
<Box key={`Box${item.id}`} sx={{ display: 'flex', flexDirection: 'column', gap: 1 }}>
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 pas quelque chose dont on risque d'avoir besoin ailleurs ? il faudrait peut-être sortir ce code et en faire un composant ?

Copy link
Contributor

Choose a reason for hiding this comment

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

Petite question, pourquoi mettre box devant l'id ?
Je pense que c'est moins performant que de mettre directement l'id.

<Chip
key={item.id}
Copy link
Contributor

Choose a reason for hiding this comment

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

Il y a toujours besoin de la prop key vu qu'il a été ajouté sur la box au-dessus ?

size="small"
sx={{
Copy link
Contributor

Choose a reason for hiding this comment

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

Au lieu d'utiliser sx et la variable css backgroundcolor, pas possible d'utiliser tout simplement color de Chip ?
J'ai pas testé mais à mon avis ça doit être la même chose.
https://mui.com/material-ui/api/chip/#props

backgroundColor:
item.specificMetadata?.equipmentType &&
equipmentColorsMap?.has(item.specificMetadata?.equipmentType)
? equipmentColorsMap.get(item.specificMetadata.equipmentType)
: undefined,
Comment on lines +198 to +201
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
item.specificMetadata?.equipmentType &&
equipmentColorsMap?.has(item.specificMetadata?.equipmentType)
? equipmentColorsMap.get(item.specificMetadata.equipmentType)
: undefined,
equipmentColorsMap?.get(item.specificMetadata?.equipmentType)

Corrigez moi si je me trompe mais je crois que du coup ça revient exactement a la même chose

}}
onDelete={() => removeElements(index)}
onClick={() => handleChipClick(index)}
label={
<OverflowableText
text={<RawReadOnlyInput name={`${name}.${index}.${NAME}`} />}
sx={{ width: '100%' }}
/>
}
/>
<FormHelperText>
{item?.specificMetadata?.equipmentType ? (
<FormattedMessage
id={getBasicEquipmentLabel(item.specificMetadata.equipmentType)}
/>
) : (
''
)}
</FormHelperText>
</Box>
))}
</FormControl>
)}
Expand Down
3 changes: 3 additions & 0 deletions src/components/treeViewFinder/TreeViewFinder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ export interface TreeViewFinderNodeProps {
childrenCount?: number;
children?: TreeViewFinderNodeProps[];
parents?: TreeViewFinderNodeProps[];
specificMetadata?: {
equipmentType: string;
};
}

interface TreeViewFinderNodeMapProps {
Expand Down
1 change: 1 addition & 0 deletions src/translations/en/equipmentsEn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export const equipmentsEn = {
LccConverterStations: 'LCC converter stations',
VscConverterStations: 'VSC converter stations',
StaticVarCompensators: 'Static var compensators',
HvdcLines: 'HVDC lines',
};
1 change: 1 addition & 0 deletions src/translations/fr/equipmentsFr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,5 @@ export const equipmentsFr = {
LccConverterStations: 'Stations de conversion LCC',
VscConverterStations: 'Stations de conversion VSC',
StaticVarCompensators: 'CSPR',
HvdcLines: 'lignes HVDC',
};
2 changes: 2 additions & 0 deletions src/utils/constants/fieldConstants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export enum FieldConstants {
EQUIPMENT_TABLE = 'equipmentTable',
EQUIPMENT_TYPE = 'equipmentType',
FILTER_TYPE = 'filterType',
FILTERS = 'filters',
FOLDER_ID = 'folderId',
FOLDER_NAME = 'folderName',
FORMATTED_CASE_PARAMETERS = 'formattedCaseParameters',
Expand All @@ -37,6 +38,7 @@ export enum FieldConstants {
NOMINAL_VOLTAGE_3 = 'nominalVoltage3',
NOMINAL_VOLTAGE = 'nominalVoltage',
OPERATION_TYPE = 'type',
TYPE = 'type',
PROPERTY_NAME = 'propertyName',
PROPERTY_OPERATOR = 'propertyOperator',
PROPERTY = 'PROPERTY',
Expand Down
9 changes: 9 additions & 0 deletions src/utils/types/equipmentType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ export const BASE_EQUIPMENTS: Partial<Record<EquipmentType, { id: EquipmentType;
},
};

export function getBasicEquipmentLabel(equipmentType: string | undefined): string {
if (!equipmentType) {
return '';
}
return equipmentType !== EquipmentType.HVDC_LINE
? (BASE_EQUIPMENTS[equipmentType as EquipmentType]?.label ?? '')
Copy link
Contributor

Choose a reason for hiding this comment

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

Pourquoi ne pas rajouter ce type d'équipement dans BASE_EQUIPMENTS ?

: 'HvdcLines';
}

export const EQUIPMENT_TYPE: Partial<
Record<
EquipmentType | ExtendedEquipmentType,
Expand Down
Loading