Skip to content

Commit b26db07

Browse files
remove duplicate TABLES_TYPES
1 parent 0a54712 commit b26db07

File tree

2 files changed

+44
-61
lines changed

2 files changed

+44
-61
lines changed

src/components/spreadsheet-view/add-spreadsheet/dialogs/add-empty-spreadsheet-dialog.tsx

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,10 @@ import { useCallback, useEffect, useMemo } from 'react';
99
import { Grid } from '@mui/material';
1010
import {
1111
CustomFormProvider,
12-
EquipmentType,
1312
SelectInput,
1413
TextInput,
15-
UseStateBooleanReturn,
1614
useSnackMessage,
15+
UseStateBooleanReturn,
1716
} from '@gridsuite/commons-ui';
1817
import { useForm } from 'react-hook-form';
1918
import { yupResolver } from '@hookform/resolvers/yup';
@@ -22,45 +21,34 @@ import { EQUIPMENT_TYPE_FIELD } from 'components/utils/field-constants';
2221
import { AppState } from 'redux/reducer';
2322
import { UUID } from 'crypto';
2423
import { dialogStyles } from '../styles/styles';
25-
import { ModificationDialog } from 'components/dialogs/commons/modificationDialog';
26-
import { getEmptySpreadsheetFormSchema, initialEmptySpreadsheetForm, SPREADSHEET_NAME } from './add-spreadsheet-form';
24+
import { ModificationDialog, type ModificationDialogProps } from 'components/dialogs/commons/modificationDialog';
25+
import {
26+
type EmptySpreadsheetForm,
27+
getEmptySpreadsheetFormSchema,
28+
initialEmptySpreadsheetForm,
29+
SPREADSHEET_NAME,
30+
} from './add-spreadsheet-form';
2731
import { addNewSpreadsheet } from './add-spreadsheet-utils';
2832
import { COLUMN_TYPES } from 'components/custom-aggrid/custom-aggrid-header.type';
29-
import { ColumnDefinitionDto } from '../../types/spreadsheet.type';
33+
import { ColumnDefinitionDto, SpreadsheetEquipmentType } from '../../types/spreadsheet.type';
3034
import { v4 as uuid4 } from 'uuid';
3135

3236
interface AddEmptySpreadsheetDialogProps {
3337
open: UseStateBooleanReturn;
3438
}
3539

36-
const TABLES_TYPES = [
37-
EquipmentType.SUBSTATION,
38-
EquipmentType.VOLTAGE_LEVEL,
39-
EquipmentType.LINE,
40-
EquipmentType.TWO_WINDINGS_TRANSFORMER,
41-
EquipmentType.THREE_WINDINGS_TRANSFORMER,
42-
EquipmentType.GENERATOR,
43-
EquipmentType.LOAD,
44-
EquipmentType.SHUNT_COMPENSATOR,
45-
EquipmentType.STATIC_VAR_COMPENSATOR,
46-
EquipmentType.BATTERY,
47-
EquipmentType.HVDC_LINE,
48-
EquipmentType.LCC_CONVERTER_STATION,
49-
EquipmentType.VSC_CONVERTER_STATION,
50-
EquipmentType.TIE_LINE,
51-
EquipmentType.DANGLING_LINE,
52-
EquipmentType.BUS,
53-
EquipmentType.BUSBAR_SECTION,
54-
];
40+
const TABLES_OPTIONS = Object.values(SpreadsheetEquipmentType).map(
41+
(elementType) => ({ id: elementType, label: elementType }) as const
42+
);
5543

56-
const DEFAULT_ID_COLUMN: ColumnDefinitionDto = {
44+
const DEFAULT_ID_COLUMN = {
5745
uuid: uuid4() as UUID,
5846
name: 'ID',
5947
id: 'id',
6048
type: COLUMN_TYPES.TEXT,
6149
formula: 'id',
6250
visible: true,
63-
};
51+
} as const satisfies ColumnDefinitionDto;
6452

6553
/**
6654
* Dialog for creating an empty spreadsheet
@@ -88,21 +76,17 @@ export default function AddEmptySpreadsheetDialog({ open, ...dialogProps }: Read
8876
reset(initialEmptySpreadsheetForm);
8977
}, [open.value, reset]);
9078

91-
const onSubmit = useCallback(
92-
(formData: any) => {
79+
const onSubmit = useCallback<ModificationDialogProps<EmptySpreadsheetForm>['onSave']>(
80+
(formData) => {
9381
if (!studyUuid) {
9482
return;
9583
}
96-
const tabIndex = tablesDefinitions.length;
97-
const tabName = formData[SPREADSHEET_NAME];
98-
const equipmentType = formData.equipmentType;
99-
10084
addNewSpreadsheet({
10185
studyUuid,
10286
columns: [DEFAULT_ID_COLUMN],
103-
sheetType: equipmentType,
104-
tabIndex,
105-
tabName,
87+
sheetType: formData.equipmentType,
88+
tabIndex: tablesDefinitions.length,
89+
tabName: formData[SPREADSHEET_NAME],
10690
spreadsheetsCollectionUuid: spreadsheetsCollectionUuid as UUID,
10791
dispatch,
10892
snackError,
@@ -119,7 +103,7 @@ export default function AddEmptySpreadsheetDialog({ open, ...dialogProps }: Read
119103
open={open.value}
120104
onClose={open.setFalse}
121105
onSave={onSubmit}
122-
onClear={() => null}
106+
onClear={() => {}}
123107
PaperProps={{ sx: dialogStyles.dialogContent }}
124108
{...dialogProps}
125109
>
@@ -133,10 +117,7 @@ export default function AddEmptySpreadsheetDialog({ open, ...dialogProps }: Read
133117
</Grid>
134118
<Grid item xs>
135119
<SelectInput
136-
options={Object.values(TABLES_TYPES).map((elementType) => ({
137-
id: elementType,
138-
label: elementType,
139-
}))}
120+
options={TABLES_OPTIONS}
140121
name={EQUIPMENT_TYPE_FIELD}
141122
label="spreadsheet/create_new_spreadsheet/element_type"
142123
size="small"

src/components/spreadsheet-view/add-spreadsheet/dialogs/add-spreadsheet-form.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import { EQUIPMENT_TYPE_FIELD, ID, NAME } from 'components/utils/field-constants';
99
import yup from '../../../utils/yup-config';
10+
import { SpreadsheetEquipmentType } from '../../types/spreadsheet.type';
1011

1112
export const SPREADSHEET_NAME = 'spreadsheetName';
1213
export const SPREADSHEET_MODEL = 'spreadsheetModel';
@@ -15,13 +16,14 @@ export const SPREADSHEET_COLLECTION_IMPORT_MODE = 'spreadsheetCollectionMode';
1516

1617
export const initialEmptySpreadsheetForm: EmptySpreadsheetForm = {
1718
[SPREADSHEET_NAME]: '',
19+
//@ts-expect-error TS2418: Type of computed property's value is '', which is not assignable to type NonNullable<SpreadsheetEquipmentType | undefined>
1820
[EQUIPMENT_TYPE_FIELD]: '',
19-
};
21+
} as const;
2022

2123
export const initialSpreadsheetFromModelForm: SpreadsheetFromModelForm = {
2224
[SPREADSHEET_NAME]: '',
2325
[SPREADSHEET_MODEL]: [],
24-
};
26+
} as const;
2527

2628
export enum SpreadsheetCollectionImportMode {
2729
REPLACE = 'REPLACE',
@@ -33,28 +35,28 @@ export const initialSpreadsheetCollectionForm: SpreadsheetCollectionForm = {
3335
[SPREADSHEET_COLLECTION_IMPORT_MODE]: SpreadsheetCollectionImportMode.REPLACE,
3436
};
3537

36-
export const getEmptySpreadsheetFormSchema = (tablesNames: string[]) => {
38+
function schemaSpreadsheetName(tablesNames: string[]) {
39+
return yup
40+
.string()
41+
.required()
42+
.max(60, 'spreadsheet/spreadsheet_name_le_60')
43+
.test(
44+
'unique',
45+
'spreadsheet/create_new_spreadsheet/spreadsheet_name_already_exists',
46+
(value) => !tablesNames.includes(value || '')
47+
);
48+
}
49+
50+
export function getEmptySpreadsheetFormSchema(tablesNames: string[]) {
3751
return yup.object().shape({
38-
[SPREADSHEET_NAME]: yup
39-
.string()
40-
.required()
41-
.max(60, 'spreadsheet/spreadsheet_name_le_60')
42-
.test('unique', 'spreadsheet/create_new_spreadsheet/spreadsheet_name_already_exists', (value) => {
43-
return !tablesNames.includes(value || '');
44-
}),
45-
[EQUIPMENT_TYPE_FIELD]: yup.string().required(),
52+
[SPREADSHEET_NAME]: schemaSpreadsheetName(tablesNames),
53+
[EQUIPMENT_TYPE_FIELD]: yup.string().oneOf(Object.values(SpreadsheetEquipmentType)).required(),
4654
});
47-
};
55+
}
4856

49-
export const getSpreadsheetFromModelFormSchema = (tablesNames: string[]) => {
57+
export function getSpreadsheetFromModelFormSchema(tablesNames: string[]) {
5058
return yup.object().shape({
51-
[SPREADSHEET_NAME]: yup
52-
.string()
53-
.required()
54-
.max(60, 'spreadsheet/spreadsheet_name_le_60')
55-
.test('unique', 'spreadsheet/create_new_spreadsheet/spreadsheet_name_already_exists', (value) => {
56-
return !tablesNames.includes(value || '');
57-
}),
59+
[SPREADSHEET_NAME]: schemaSpreadsheetName(tablesNames),
5860
[SPREADSHEET_MODEL]: yup
5961
.array()
6062
.of(
@@ -67,7 +69,7 @@ export const getSpreadsheetFromModelFormSchema = (tablesNames: string[]) => {
6769
.min(1, 'spreadsheet/create_new_spreadsheet/must_select_spreadsheet_model')
6870
.max(1, 'spreadsheet/create_new_spreadsheet/must_select_only_one_spreadsheet_model'),
6971
});
70-
};
72+
}
7173

7274
export const getSpreadsheetCollectionFormSchema = () => {
7375
return yup.object().shape({

0 commit comments

Comments
 (0)