Skip to content

Commit

Permalink
Merge pull request #2721 from dhis2/v40-backport-DHIS2-12203/predicto…
Browse files Browse the repository at this point in the history
…rs-update-outputcombo-for-default

fix(predictor): update default unselected categoryoptioncombo selection (v40)
  • Loading branch information
kabaros authored Nov 2, 2023
2 parents ff07156 + ad11a36 commit 3aee36e
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import DropDown from '../../../forms/form-fields/drop-down.js';
import { red500, grey400 } from 'material-ui/styles/colors';
import { LinearProgress } from 'material-ui';

const DEFAULT = 'default';

const DropDownLoader = ({ msg }) => (
<p
style={{
Expand All @@ -25,6 +23,8 @@ class DataElementCategoryOptionCombo extends Component {
options: [],
loading: false,
loadErrorText: '',
defaultCategoryOptionComboId: null,
hasOnlyDefaultCatCombo: false,
};
getTranslation = this.context.d2.i18n.getTranslation.bind(
this.context.d2.i18n
Expand All @@ -37,7 +37,13 @@ class DataElementCategoryOptionCombo extends Component {
prevOutputId = null;

onChange = ({ target }) => {
const value = target.value ? { id: target.value } : null;
const selectedId = target.value;
let value = selectedId ? { id: selectedId } : null;

if(selectedId === this.state.defaultCategoryOptionComboId) {
value = null
}

this.props.onChange({ target: { value } });
};

Expand Down Expand Up @@ -79,27 +85,39 @@ class DataElementCategoryOptionCombo extends Component {
this.setState({ loading: true, loadErrorText: '' });

try {
const response = await this.context.d2.models.dataElements.get(
const catComboResponse = this.context.d2.models.dataElements.get(
this.props.model.output.id,
{ fields: ['categoryCombo[categoryOptionCombos[id,name]]'] }
{ fields: ['categoryCombo[isDefault,categoryOptionCombos[id,name]]']}
);
// need to get the default categoryCombo - to be able to show the correct label in the dropdown
const defaultCatComboResponse = this.context.d2.models.categoryCombos.list({
fields: ['id', 'name', 'isDefault', 'categoryOptionCombos[id,name]'],
filter: `isDefault:eq:true`
})
const [response, defaultCatCombo] = await Promise.all([catComboResponse, defaultCatComboResponse])

const defaultCategoryOptionCombo = defaultCatCombo.toArray()[0].categoryOptionCombos.toArray()[0]
const categoryOptionCombos =
response.categoryCombo.categoryOptionCombos;
const hasOnlyDefault =

const hasOnlyDefaultCatCombo = defaultCategoryOptionCombo &&
categoryOptionCombos.length === 1 &&
categoryOptionCombos[0].name === DEFAULT;
const options = hasOnlyDefault
? []
: categoryOptionCombos.map(o => ({
text: o.name,
value: o.id,
}));

this.setState({ options, loading: false });
categoryOptionCombos[0].id === defaultCategoryOptionCombo.id;

let options = [{ text: this.getTranslation('predict_according_to_input_category_option_combo'), value: defaultCategoryOptionCombo.id }];

options = options.concat(categoryOptionCombos.map(
o => ({
text: o.name,
value: o.id,
})
));

this.setState({ options, loading: false, defaultCategoryOptionComboId: defaultCategoryOptionCombo.id, hasOnlyDefaultCatCombo });
} catch (error) {
console.error(error);
const msg = this.getTranslation('output_combo_error');
this.setState({ loading: false, loadErrorText: msg });
this.setState({ loading: false, loadErrorText: msg, hasOnlyDefaultCatCombo: false });
}
}

Expand All @@ -124,12 +142,19 @@ class DataElementCategoryOptionCombo extends Component {
return null;
}

if(this.state.hasOnlyDefaultCatCombo) {
return null;
}

const value = this.props.value && this.props.value.id || this.state.defaultCategoryOptionComboId

return (
<DropDown
labelText={this.getTranslation('output_combo')}
onChange={this.onChange}
value={this.props.value && this.props.value.id}
value={value}
options={this.state.options}
isRequired
/>
);
}
Expand Down
1 change: 1 addition & 0 deletions src/i18n/i18n_module_en.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2327,3 +2327,4 @@ owner_at_end_organisation_unit=Owner at end organisation unit
registration_organisation_unit=Registration organisation unit
organisation_unit_field=Organisation unit field
prevent_adding_new_events_to_stage=Prevent adding new events to stage
predict_according_to_input_category_option_combo=Predict according to input category option combo

0 comments on commit 3aee36e

Please sign in to comment.