Skip to content

Commit

Permalink
feat: [SDCOSMO-1974] implement scenario selector for scenario parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nborde-CSM committed Jul 5, 2024
1 parent 271ab5c commit f65dc29
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 12 deletions.
25 changes: 13 additions & 12 deletions doc/scenarioParametersConfiguration.md
Original file line number Diff line number Diff line change
Expand Up @@ -757,15 +757,16 @@ components, whose value depend on the **input component type** and the **paramet

Here is the list of `data-cy` patterns for all generic input components provided by the azure-sample-webapp:

| varType | subType | input component | `data-cy` pattern |
| ------------- | -------- | -------------------- | ---------------------------- |
| `bool` | | Switch boolean input | `toggle-input-<parameterId>` |
| `date` | | Date picker | `date-input-<parameterId>` |
| `enum` | | Dropdown list | `enum-input-<parameterId>` |
| `enum` | `RADIO` | Radio buttons | `radio-input-<parameterId>` |
| `int` | | Number input | `number-input-<parameterId>` |
| `number` | | Number input | `number-input-<parameterId>` |
| `number` | `SLIDER` | Slider | `slider-input-<parameterId>` |
| `string` | | Text field | `text-input-<parameterId>` |
| `%DATASETID%` | | File upload | `file-upload-<parameterId>` |
| `%DATASETID%` | `TABLE` | Editable table | `table-<parameterId>` |
| varType | subType | input component | `data-cy` pattern |
| ------------- | ----------- | -------------------- | ------------------------------- |
| `bool` | | Switch boolean input | `toggle-input-<parameterId>` |
| `date` | | Date picker | `date-input-<parameterId>` |
| `enum` | | Dropdown list | `enum-input-<parameterId>` |
| `enum` | `RADIO` | Radio buttons | `radio-input-<parameterId>` |
| `enum` | `SCENARIOS` | Scenarios select | `scenarios-input-<parameterId>` |
| `int` | | Number input | `number-input-<parameterId>` |
| `number` | | Number input | `number-input-<parameterId>` |
| `number` | `SLIDER` | Slider | `slider-input-<parameterId>` |
| `string` | | Text field | `text-input-<parameterId>` |
| `%DATASETID%` | | File upload | `file-upload-<parameterId>` |
| `%DATASETID%` | `TABLE` | Editable table | `table-<parameterId>` |
4 changes: 4 additions & 0 deletions public/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@
"noFileMessage": "None",
"fileNamePlaceholder": "{{fileExtension}} file"
},
"scenarioSelect": {
"noValues": "No scenario selected",
"noOptions": "No scenarios available"
},
"loading": {
"line": {
"workspace": {
Expand Down
4 changes: 4 additions & 0 deletions public/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,10 @@
"noFileMessage": "Aucun",
"fileNamePlaceholder": "fichier {{fileExtension}}"
},
"scenarioSelect": {
"noValues": "Aucun scénario sélectionné",
"noOptions": "Aucun scénario disponible"
},
"loading": {
"line": {
"workspace": {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Cosmo Tech.
// Licensed under the MIT license.
import React, { useMemo } from 'react';
import { useTranslation } from 'react-i18next';
import PropTypes from 'prop-types';
import { Grid } from '@mui/material';
import { SingleSelect } from '@cosmotech/ui';
import { useSortedScenarioList } from '../../../../hooks/ScenarioListHooks';
import { useCurrentScenarioId } from '../../../../state/hooks/ScenarioHooks';
import { TranslationUtils } from '../../../../utils';

export const ScenarioSelect = ({ parameterData, context, parameterValue, setParameterValue, isDirty }) => {
const { t } = useTranslation();
const scenarioList = useSortedScenarioList();
const currentScenarioId = useCurrentScenarioId();
const runTemplateFilter = parameterData.options?.runTemplateFilter;

const mappedScenarioList = useMemo(() => {
const filteredScenarioList =
runTemplateFilter == null || runTemplateFilter?.length === 0
? scenarioList
: scenarioList.filter((scenario) => {
return runTemplateFilter.includes(scenario.runTemplateId) && scenario.id !== currentScenarioId;
});

return filteredScenarioList.map((scenario) => ({ key: scenario.id, label: scenario.name }));
}, [runTemplateFilter, scenarioList, currentScenarioId]);

const labels = useMemo(() => {
return {
label: t(TranslationUtils.getParameterTranslationKey(parameterData.id), parameterData.id),
noValue: t('genericcomponent.scenarioSelect.noValues', 'No scenario selected'),
noOptions: t('genericcomponent.scenarioSelect.noOptions', 'No scenarios available'),
};
}, [t, parameterData.id]);

return (
<Grid item xs={3}>
<SingleSelect
id={parameterData.id}
labels={labels}
tooltipText={t(TranslationUtils.getParameterTooltipTranslationKey(parameterData.id), '')}
value={parameterValue}
options={mappedScenarioList}
onChange={(newValue) => setParameterValue(newValue ?? null)}
disabled={!context.editMode}
isDirty={isDirty}
/>
</Grid>
);
};

ScenarioSelect.propTypes = {
parameterData: PropTypes.object.isRequired,
context: PropTypes.object.isRequired,
parameterValue: PropTypes.any,
setParameterValue: PropTypes.func.isRequired,
isDirty: PropTypes.bool,
gridItemProps: PropTypes.object,
};

ScenarioSelect.defaultProps = {
isDirty: false,
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export { GenericToggleInput } from './GenericToggleInput';
export { GenericUploadFile } from './GenericUploadFile';
export { GenericTable } from './GenericTable';
export { GenericSliderInput } from './GenericSliderInput';
export { ScenarioSelect } from './ScenarioSelect';
1 change: 1 addition & 0 deletions src/services/config/SolutionSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ const basicParameterOptions = z.object({
dateFormat: z.string().optional().nullable(),
columns: z.array(basicColumnField).optional().nullable(),
shouldRenameFileOnUpload: z.string().optional().nullable(),
runTemplateFilter: z.array(z.string().optional().nullable()).optional().nullable(),
});

const basicParameterGroupOptions = z.object({
Expand Down
1 change: 1 addition & 0 deletions src/utils/ConfigUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const getParameterAttribute = (parameter, attributeName) => {
'subType',
'canChangeRowsNumber',
'shouldRenameFileOnUpload',
'runTemplateFilter',
];
if (!knownAttributesNames.includes(attributeName)) {
console.warn(`The attribute "${attributeName}" is not a known attribute in the scenario parameters configuration.`);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
GenericTextInput,
GenericToggleInput,
GenericUploadFile,
ScenarioSelect,
} from '../../../components/ScenarioParameters/components/ScenarioParametersInputs';
import { DATASET_ID_VARTYPE } from '../../../services/config/ApiConstants';

Expand All @@ -19,6 +20,7 @@ export const GENERIC_VAR_TYPES_COMPONENTS_MAPPING = {
date: GenericDateInput,
enum: GenericEnumInput,
'enum-RADIO': GenericRadioInput,
'enum-SCENARIOS': ScenarioSelect,
list: GenericMultiSelect,
int: GenericNumberInput,
'number-SLIDER': GenericSliderInput,
Expand Down

0 comments on commit f65dc29

Please sign in to comment.