A platform for community discussion. Free, open, simple.
FIX: Always show the translation post menu (#34927)
Natalie Taya56ba08c8725
diff --git a/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs b/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjsindex 683087d2f32..1051e660c9d 100644--- a/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs+++ b/app/assets/javascripts/discourse/app/components/post/menu/buttons/add-translation.gjs@@ -30,13 +30,13 @@ export default class PostMenuAddTranslationButton extends Component {}get addTranslationsLabel() {- if (this.showAsMenu) {- return i18n("post.localizations.manage", {- count: this.args.post.post_localizations_count,- });- }+ return i18n("post.localizations.manage", {+ count: this.args.post.post_localizations_count,+ });+ }- return i18n("post.localizations.add");+ get showViewTranslations() {+ return this.args.post.post_localizations_count > 0;}get viewTranslationLabel() {@@ -45,10 +45,6 @@ export default class PostMenuAddTranslationButton extends Component {});}- get showAsMenu() {- return this.args.post.post_localizations_count > 0;- }-@actionviewTranslations() {this.modal.show(PostTranslationsModal, { model: { post: this.args.post } });@@ -88,22 +84,22 @@ export default class PostMenuAddTranslationButton extends Component {<template>{{#if this.showTranslationButton}}- {{#if this.showAsMenu}}- <DMenu- ...attributes- @identifier="post-action-menu-edit-translations"- class="update-translations-menu"- @title={{this.addTranslationsLabel}}- @icon="discourse-add-translation"- @onRegisterApi={{this.onRegisterApi}}- @arrow={{false}}- >- <:content>- <DropdownMenu as |dropdown|>- <PluginOutlet- @name="post-menu-translations-dropdown"- @outletArgs={{lazyHash dropdown=dropdown post=@post}}- >+ <DMenu+ ...attributes+ @identifier="post-action-menu-edit-translations"+ class="update-translations-menu"+ @title={{this.addTranslationsLabel}}+ @icon="discourse-add-translation"+ @onRegisterApi={{this.onRegisterApi}}+ @arrow={{false}}+ >+ <:content>+ <DropdownMenu as |dropdown|>+ <PluginOutlet+ @name="post-menu-translations-dropdown"+ @outletArgs={{lazyHash dropdown=dropdown post=@post}}+ >+ {{#if this.showViewTranslations}}<dropdown.item class="update-translations-menu__view"><DButtonclass="post-action-menu__view-translation"@@ -112,27 +108,19 @@ export default class PostMenuAddTranslationButton extends Component {@action={{this.viewTranslations}}/></dropdown.item>- <dropdown.item class="update-translations-menu__add">- <DButton- class="post-action-menu__add-translation"- @label="post.localizations.add"- @icon="plus"- @action={{this.addTranslation}}- />- </dropdown.item>- </PluginOutlet>- </DropdownMenu>- </:content>- </DMenu>- {{else}}- <DButton- class="post-action-menu__add-translation"- @title="post.localizations.add"- @icon="discourse-add-translation"- @action={{this.addTranslation}}- ...attributes- />- {{/if}}+ {{/if}}+ <dropdown.item class="update-translations-menu__add">+ <DButton+ class="post-action-menu__add-translation"+ @label="post.localizations.add"+ @icon="plus"+ @action={{this.addTranslation}}+ />+ </dropdown.item>+ </PluginOutlet>+ </DropdownMenu>+ </:content>+ </DMenu>{{/if}}</template>}diff --git a/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjs b/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjsnew file mode 100644index 00000000000..d94c68054c4--- /dev/null+++ b/app/assets/javascripts/discourse/tests/integration/components/post/menu/buttons/add-translation-test.gjs@@ -0,0 +1,106 @@+import { getOwner } from "@ember/owner";+import { click, render } from "@ember/test-helpers";+import { module, test } from "qunit";+import AddTranslation from "discourse/components/post/menu/buttons/add-translation";+import { setupRenderingTest } from "discourse/tests/helpers/component-test";+import pretender from "discourse/tests/helpers/create-pretender";+import { i18n } from "discourse-i18n";++module(+ "Integration | Component | Post Menu Add Translation Button",+ function (hooks) {+ setupRenderingTest(hooks);++ hooks.beforeEach(function () {+ const store = getOwner(this).lookup("service:store");+ const post = store.createRecord("post", {+ id: 1,+ topic_id: 1,+ post_localizations_count: 0,+ });++ this.post = post;+ this.set("post", post);++ // positive case for menu to always show+ this.siteSettings.content_localization_enabled = true;+ this.currentUser.admin = true;+ this.currentUser.can_localize_content = true;++ pretender.get("/posts/1.json", () => {+ return [200, {}, { raw: "Test post content" }];+ });+ pretender.get("/t/1.json", () => {+ return [200, {}, { raw: "Test post content" }];+ });+ });++ test("renders menu button when user can localize", async function (assert) {+ await render(<template><AddTranslation @post={{this.post}} /></template>);++ assert+ .dom(".update-translations-menu")+ .exists("Menu button rendered when user can localize");+ });++ test("does not render when user cannot localize", async function (assert) {+ this.currentUser.can_localize_content = false;++ await render(<template><AddTranslation @post={{this.post}} /></template>);++ assert+ .dom(".update-translations-menu")+ .doesNotExist("No menu when user cannot localize");+ });++ test("does not render when localization is disabled", async function (assert) {+ this.siteSettings.content_localization_enabled = false;++ await render(<template><AddTranslation @post={{this.post}} /></template>);++ assert+ .dom(".update-translations-menu")+ .doesNotExist("No menu when localization disabled");+ });++ test("shows both view and add buttons when localizations exist", async function (assert) {+ this.post.post_localizations_count = 2;++ await render(<template><AddTranslation @post={{this.post}} /></template>);++ await click(".update-translations-menu");++ assert+ .dom(".update-translations-menu__view")+ .exists("View button shown when localizations exist");+ assert+ .dom(".update-translations-menu__add")+ .exists("Add button always shown");+ });++ test("shows only add button when no localizations", async function (assert) {+ this.post.post_localizations_count = 0;+ await render(<template><AddTranslation @post={{this.post}} /></template>);++ await click(".update-translations-menu");++ assert+ .dom(".update-translations-menu__view")+ .doesNotExist("View button hidden when no localizations");+ assert+ .dom(".update-translations-menu__add")+ .exists("Add button always shown");+ });++ test("view translation label includes count", async function (assert) {+ this.post.post_localizations_count = 5;+ await render(<template><AddTranslation @post={{this.post}} /></template>);++ await click(".update-translations-menu");++ assert+ .dom(".post-action-menu__view-translation")+ .hasText(i18n("post.localizations.view", { count: 5 }));+ });+ }+);diff --git a/spec/system/post_translation_spec.rb b/spec/system/post_translation_spec.rbindex b684a5b5f66..249ea86250d 100644--- a/spec/system/post_translation_spec.rb+++ b/spec/system/post_translation_spec.rb@@ -30,7 +30,8 @@ describe "Post translations", type: :system dopost.update!(locale: "en")topic_page.visit_topic(topic)- find("#post_#{post.post_number} .post-action-menu__add-translation").click+ find("#post_1 .post-action-menu-edit-translations-trigger").click+ find(".update-translations-menu__add .post-action-menu__add-translation").clicktranslation_selector.expandexpect(all(".translation-selector-dropdown .select-kit-collection li").count).to eq(3)expect(translation_selector).to have_option_value("fr")@@ -41,7 +42,8 @@ describe "Post translations", type: :system doit "allows a user to translate a post" dotopic_page.visit_topic(topic)- find("#post_#{post.post_number} .post-action-menu__add-translation").click+ find("#post_1 .post-action-menu-edit-translations-trigger").click+ find(".update-translations-menu__add .post-action-menu__add-translation").clickexpect(composer).to be_openedtranslation_selector.expandtranslation_selector.select_row_by_value("fr")