From 43b00d6b1a52a5cfaaea5eb5fcf0c6a44032bfe6 Mon Sep 17 00:00:00 2001 From: "Chong Wang (cwg)" Date: Tue, 24 Jan 2023 13:40:46 +0000 Subject: [PATCH] [FIX] web: reload data when translation changes before this commit: model translation: when the user changes the translation for the current language in the translation dialog, the value in the form view is also updated and marked modified. But if the user wants to invalidate the translation and set the translation to `false`, the form view value is also updated to `false`, if the user saves by accident, all translations are lost. model_terms translation: when translations are modified in the translation dialog, the field value will not be changed after this commit: form view data will be reloaded after translations changed in the translation dialog closes odoo/odoo#111869 X-original-commit: 9aab8347a1a4cc74050dd2e5035cba838b93863c Signed-off-by: Wang Chong (cwg) --- .../src/views/fields/char/char_field.xml | 1 - .../src/views/fields/text/text_field.xml | 1 - .../src/views/fields/translation_button.js | 12 ++++--- .../src/views/fields/translation_dialog.js | 35 ++++++++----------- .../tests/views/fields/char_field_tests.js | 29 +++++++-------- .../static/src/js/backend/html_field.xml | 1 - 6 files changed, 37 insertions(+), 42 deletions(-) diff --git a/addons/web/static/src/views/fields/char/char_field.xml b/addons/web/static/src/views/fields/char/char_field.xml index 8496c353ff6..34e683d7cf9 100644 --- a/addons/web/static/src/views/fields/char/char_field.xml +++ b/addons/web/static/src/views/fields/char/char_field.xml @@ -20,7 +20,6 @@ diff --git a/addons/web/static/src/views/fields/text/text_field.xml b/addons/web/static/src/views/fields/text/text_field.xml index 6b19c1d0205..be000554325 100644 --- a/addons/web/static/src/views/fields/text/text_field.xml +++ b/addons/web/static/src/views/fields/text/text_field.xml @@ -20,7 +20,6 @@ diff --git a/addons/web/static/src/views/fields/translation_button.js b/addons/web/static/src/views/fields/translation_button.js index 68fc1c3c780..bfbb841d9c2 100644 --- a/addons/web/static/src/views/fields/translation_button.js +++ b/addons/web/static/src/views/fields/translation_button.js @@ -19,7 +19,7 @@ export function useTranslationDialog() { const addDialog = useOwnedDialogs(); const env = useEnv(); - async function openTranslationDialog({ record, fieldName, updateField }) { + async function openTranslationDialog({ record, fieldName }) { if (!record.resId) { let _continue = true; await new Promise((resolve) => { @@ -50,7 +50,10 @@ export function useTranslationDialog() { resModel: resModel, userLanguageValue: record.data[fieldName] || "", isComingFromTranslationAlert: false, - updateField, + onSave: async () => { + await record.load({}, { keepChanges: true }); + record.model.notify(); + }, }); } @@ -71,13 +74,12 @@ export class TranslationButton extends Component { } onClick() { - const { fieldName, record, updateField } = this.props; - this.translationDialog({ fieldName, record, updateField }); + const { fieldName, record } = this.props; + this.translationDialog({ fieldName, record }); } } TranslationButton.template = "web.TranslationButton"; TranslationButton.props = { fieldName: { type: String }, record: { type: Object }, - updateField: { type: Function }, }; diff --git a/addons/web/static/src/views/fields/translation_dialog.js b/addons/web/static/src/views/fields/translation_dialog.js index f648ab8ce07..9a9e0ea2d65 100644 --- a/addons/web/static/src/views/fields/translation_dialog.js +++ b/addons/web/static/src/views/fields/translation_dialog.js @@ -28,16 +28,23 @@ export class TranslationDialog extends Component { this.terms = translations.map((term) => { const relatedLanguage = languages.find((l) => l[0] === term.lang); - return { + const termInfo = { ...term, langName: relatedLanguage[1], - value: - term.lang === this.user.lang && - !this.props.showSource && - !this.props.isComingFromTranslationAlert - ? this.props.userLanguageValue - : term.value || "", + value: term.value || "", }; + // we set the translation value coming from the database, except for the language + // the user is currently utilizing. Then we set the translation value coming + // from the value of the field in the form + if ( + term.lang === this.user.lang && + !this.props.showSource && + !this.props.isComingFromTranslationAlert + ) { + this.updatedTerms[term.id] = this.props.userLanguageValue; + termInfo.value = this.props.userLanguageValue; + } + return termInfo; }); this.terms.sort((a, b) => a.langName.localeCompare(b.langName)); }); @@ -88,19 +95,7 @@ export class TranslationDialog extends Component { translations, ]); - // we might have to update the value of the field on the form - // view that opened the translation dialog - const currentTerm = this.terms.find( - (term) => term.lang === this.user.lang && !this.props.showSource - ); - if ( - currentTerm && - currentTerm.id in this.updatedTerms && - currentTerm.value !== this.updatedTerms[currentTerm.id] - ) { - this.props.updateField(this.updatedTerms[currentTerm.id]); - } - + await this.props.onSave(); this.props.close(); } } diff --git a/addons/web/static/tests/views/fields/char_field_tests.js b/addons/web/static/tests/views/fields/char_field_tests.js index 1bd5ac20875..e2399b2de67 100644 --- a/addons/web/static/tests/views/fields/char_field_tests.js +++ b/addons/web/static/tests/views/fields/char_field_tests.js @@ -310,7 +310,7 @@ QUnit.module("Fields", (hooks) => { return Promise.resolve([ [ { lang: "en_US", source: "yop", value: "yop" }, - { lang: "fr_BE", source: "yop", value: "valeur français" }, + { lang: "fr_BE", source: "yop", value: "yop français" }, { lang: "es_ES", source: "yop", value: "yop español" }, ], { translation_type: "char", translation_show_source: false }, @@ -319,9 +319,9 @@ QUnit.module("Fields", (hooks) => { if (call_get_field_translations === 1) { return Promise.resolve([ [ - { lang: "en_US", source: "english value", value: "english value" }, - { lang: "fr_BE", source: "english value", value: "valeur français" }, - { lang: "es_ES", source: "english value", value: "english value" }, + { lang: "en_US", source: "bar", value: "bar" }, + { lang: "fr_BE", source: "bar", value: "yop français" }, + { lang: "es_ES", source: "bar", value: "bar" }, ], { translation_type: "char", translation_show_source: false }, ]); @@ -330,9 +330,10 @@ QUnit.module("Fields", (hooks) => { if (route === "/web/dataset/call_kw/partner/update_field_translations") { assert.deepEqual( args[2], - { en_US: "english value", es_ES: false }, + { en_US: "bar", es_ES: false }, "the new translation value should be written and the value false voids the translation" ); + serverData.models.partner.records[0].foo = "bar"; return Promise.resolve(null); } }, @@ -366,7 +367,7 @@ QUnit.module("Fields", (hooks) => { assert.strictEqual(translations[0].value, "yop", "English translation should be filled"); assert.strictEqual( translations[1].value, - "valeur français", + "yop français", "French translation should be filled" ); assert.strictEqual( @@ -375,33 +376,33 @@ QUnit.module("Fields", (hooks) => { "Spanish translation should be filled" ); - await editInput(translations[0], null, "english value"); - await editInput(translations[2], null, ""); + await editInput(translations[0], null, "bar"); // set the en_US(user language) translation to "foo" + await editInput(translations[2], null, ""); // void the es_ES translation await click(target, ".modal button.btn-primary"); // save assert.strictEqual( target.querySelector(`.o_field_char input[type="text"]`).value, - "english value", + "bar", "the new translation was not transfered to modified record" ); - await editInput(target, `.o_field_char input[type="text"]`, "new english value"); + await editInput(target, `.o_field_char input[type="text"]`, "baz"); await click(target, ".o_field_char .btn.o_field_translate"); translations = target.querySelectorAll(".modal .o_translation_dialog .translation input"); assert.strictEqual( translations[0].value, - "new english value", + "baz", "Modified value should be used instead of translation" ); assert.strictEqual( translations[1].value, - "valeur français", - "French translation should be filled" + "yop français", + "French translation shouldn't be changed" ); assert.strictEqual( translations[2].value, - "english value", + "bar", "Spanish translation should fallback to the English translation" ); }); diff --git a/addons/web_editor/static/src/js/backend/html_field.xml b/addons/web_editor/static/src/js/backend/html_field.xml index e734db25b66..30ee9c958bc 100644 --- a/addons/web_editor/static/src/js/backend/html_field.xml +++ b/addons/web_editor/static/src/js/backend/html_field.xml @@ -30,7 +30,6 @@