From 3cc2a820a491e57581cae3629a00e4afa0d937e8 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 1 Sep 2023 15:01:22 +0200 Subject: [PATCH 1/3] fix(predictor): update default unselected categoryoptioncombo selection --- .../DataElementCategoryOptionCombo.js | 52 ++++++++++++------- 1 file changed, 34 insertions(+), 18 deletions(-) diff --git a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js index 314b1e541..2f838bd11 100644 --- a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js +++ b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js @@ -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 }) => (

{ - 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 } }); }; @@ -79,23 +84,31 @@ 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 = - categoryOptionCombos.length === 1 && - categoryOptionCombos[0].name === DEFAULT; - const options = hasOnlyDefault - ? [] - : categoryOptionCombos.map(o => ({ - text: o.name, - value: o.id, - })); - - this.setState({ options, loading: false }); + + 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 }); } catch (error) { console.error(error); const msg = this.getTranslation('output_combo_error'); @@ -107,7 +120,7 @@ class DataElementCategoryOptionCombo extends Component { if (this.state.loading) { return ( ); } @@ -124,12 +137,15 @@ class DataElementCategoryOptionCombo extends Component { return null; } + const value = this.props.value && this.props.value.id || this.state.defaultCategoryOptionComboId + return ( ); } From 464821850bbdde8041c1da6964c387983b6e61aa Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Fri, 1 Sep 2023 16:00:22 +0200 Subject: [PATCH 2/3] fix: typo --- .../field-overrides/predictor/DataElementCategoryOptionCombo.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js index 2f838bd11..a9cbf8c7e 100644 --- a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js +++ b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js @@ -120,7 +120,7 @@ class DataElementCategoryOptionCombo extends Component { if (this.state.loading) { return ( ); } From ad11a366ff5d263f1a5895dbde3f8ee9d7b1bdd1 Mon Sep 17 00:00:00 2001 From: Birk Johansson Date: Thu, 2 Nov 2023 15:34:27 +0100 Subject: [PATCH 3/3] fix(predictor): fix hide when default catcombo --- .../predictor/DataElementCategoryOptionCombo.js | 17 +++++++++++++---- src/i18n/i18n_module_en.properties | 1 + 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js index a9cbf8c7e..047806c5f 100644 --- a/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js +++ b/src/config/field-overrides/predictor/DataElementCategoryOptionCombo.js @@ -23,7 +23,8 @@ class DataElementCategoryOptionCombo extends Component { options: [], loading: false, loadErrorText: '', - defaultCategoryOptionComboId: null + defaultCategoryOptionComboId: null, + hasOnlyDefaultCatCombo: false, }; getTranslation = this.context.d2.i18n.getTranslation.bind( this.context.d2.i18n @@ -86,7 +87,7 @@ class DataElementCategoryOptionCombo extends Component { try { const catComboResponse = this.context.d2.models.dataElements.get( this.props.model.output.id, - { fields: ['categoryCombo[isDefault,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({ @@ -99,6 +100,10 @@ class DataElementCategoryOptionCombo extends Component { const categoryOptionCombos = response.categoryCombo.categoryOptionCombos; + const hasOnlyDefaultCatCombo = defaultCategoryOptionCombo && + categoryOptionCombos.length === 1 && + 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( @@ -108,11 +113,11 @@ class DataElementCategoryOptionCombo extends Component { }) )); - this.setState({ options, loading: false, defaultCategoryOptionComboId: defaultCategoryOptionCombo.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 }); } } @@ -137,6 +142,10 @@ 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 ( diff --git a/src/i18n/i18n_module_en.properties b/src/i18n/i18n_module_en.properties index a5b8957c0..2dcf0e7e1 100644 --- a/src/i18n/i18n_module_en.properties +++ b/src/i18n/i18n_module_en.properties @@ -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