Skip to content

A platform for community discussion. Free, open, simple.

FIX: Prevent translation editor auto discard (#35887)

Keegan Georgee5cf7c8a317b
diff --git a/frontend/discourse/app/components/post-translation-editor.gjs b/frontend/discourse/app/components/post-translation-editor.gjs
index 75578c78cc7..1965fe20d06 100644
--- a/frontend/discourse/app/components/post-translation-editor.gjs
+++ b/frontend/discourse/app/components/post-translation-editor.gjs
@@ -24,14 +24,22 @@ export default class PostTranslationEditor extends Component {
if (this.composer.selectedTranslationLocale && !this.composer.model.reply) {
const localization = await this.findCurrentLocalization();
if (localization) {
- this.composer.model.set("reply", localization.raw);
+ this.composer.model.setProperties({
+ reply: localization.raw,
+ originalText: localization.raw,
+ });
if (localization?.topic_localization) {
- this.composer.model.set(
- "title",
- localization.topic_localization.title
- );
+ this.composer.model.setProperties({
+ title: localization.topic_localization.title,
+ originalTitle: localization.topic_localization.title,
+ });
}
+ } else {
+ this.composer.model.setProperties({
+ originalText: "",
+ originalTitle: "",
+ });
}
}
}
@@ -74,18 +82,23 @@ export default class PostTranslationEditor extends Component {
const currentLocalization = await this.findCurrentLocalization();
if (currentLocalization) {
- this.composer.model.set("reply", currentLocalization.raw);
+ this.composer.model.setProperties({
+ reply: currentLocalization.raw,
+ originalText: currentLocalization.raw,
+ });
if (currentLocalization?.topic_localization) {
- this.composer.model.set(
- "title",
- currentLocalization.topic_localization.title
- );
+ this.composer.model.setProperties({
+ title: currentLocalization.topic_localization.title,
+ originalTitle: currentLocalization.topic_localization.title,
+ });
}
} else {
this.composer.model.setProperties({
reply: "",
title: "",
+ originalText: "",
+ originalTitle: "",
});
}
}
diff --git a/frontend/discourse/app/services/composer.js b/frontend/discourse/app/services/composer.js
index 4fc1d56020a..85cb4da9268 100644
--- a/frontend/discourse/app/services/composer.js
+++ b/frontend/discourse/app/services/composer.js
@@ -1419,6 +1419,14 @@ export default class ComposerService extends Service {
opts.draftKey !== composerModel.draftKey &&
composerModel.composeState === Composer.DRAFT
) {
+ // Check if content is dirty before auto-closing
+ if (composerModel.anyDirty) {
+ const retry = await this.cancelComposer(opts);
+ if (retry) {
+ await this.open(opts);
+ }
+ return;
+ }
this.close();
composerModel = null;
}
diff --git a/spec/system/post_translation_spec.rb b/spec/system/post_translation_spec.rb
index 18a2914c6ca..7a143643a85 100644
--- a/spec/system/post_translation_spec.rb
+++ b/spec/system/post_translation_spec.rb
@@ -130,6 +130,46 @@ describe "Post translations", type: :system do
expect(PostLocalization.exists?(post_id: post.id, locale: "fr")).to be false
end
+
+ it "prompts to discard changes when abandoning modified translation" do
+ discard_modal = PageObjects::Modals::DiscardDraft.new
+
+ topic_page.visit_topic(topic)
+ find("#post_#{post.post_number} .post-action-menu-edit-translations-trigger").click
+ find(".update-translations-menu__add .post-action-menu__add-translation").click
+ expect(composer).to be_opened
+
+ translation_selector.expand
+ translation_selector.select_row_by_value("fr")
+
+ composer.fill_content("Salut le monde")
+ composer.minimize
+ expect(composer).to be_minimized
+
+ find("#post_#{post.post_number} .post-action-menu__reply").click
+
+ expect(discard_modal).to be_open
+ end
+
+ it "auto-closes when abandoning unchanged translation" do
+ discard_modal = PageObjects::Modals::DiscardDraft.new
+
+ topic_page.visit_topic(topic)
+ find("#post_#{post.post_number} .post-action-menu-edit-translations-trigger").click
+ find(".update-translations-menu__add .post-action-menu__add-translation").click
+ expect(composer).to be_opened
+
+ translation_selector.expand
+ translation_selector.select_row_by_value("fr")
+
+ composer.minimize
+ expect(composer).to be_minimized
+
+ find("#post_#{post.post_number} .post-action-menu__reply").click
+
+ expect(discard_modal).to be_closed
+ expect(composer).to be_opened
+ end
end
context "when creating a new post in a different locale" do