From 0085365459eacd165093265174f47a3d6ebfda05 Mon Sep 17 00:00:00 2001 From: Krzysztof Kotlarek Date: Tue, 9 Apr 2024 09:58:30 +1000 Subject: [PATCH] FEATURE: confirmation when a public section is updated (#26546) Display additional confirmation when: - The public section is going to be updated; - The public section is going to be deleted; - The public section is going to be marked as private. --- .../components/modal/sidebar-section-form.js | 32 +++++++++++++++- config/locales/client.en.yml | 3 ++ spec/system/custom_sidebar_sections_spec.rb | 38 +++++++++++++++++++ .../editing_sidebar_community_section_spec.rb | 1 + .../modals/sidebar_section_form.rb | 7 +++- 5 files changed, 79 insertions(+), 2 deletions(-) diff --git a/app/assets/javascripts/discourse/app/components/modal/sidebar-section-form.js b/app/assets/javascripts/discourse/app/components/modal/sidebar-section-form.js index a91f4a2b4a2..9bdb22e6f75 100644 --- a/app/assets/javascripts/discourse/app/components/modal/sidebar-section-form.js +++ b/app/assets/javascripts/discourse/app/components/modal/sidebar-section-form.js @@ -298,6 +298,23 @@ export default class SidebarSectionForm extends Component { } update() { + this.wasPublic || this.isPublic + ? this.#updateWithConfirm() + : this.#updateCall(); + } + + #updateWithConfirm() { + return this.dialog.yesNoConfirm({ + message: this.isPublic + ? I18n.t("sidebar.sections.custom.update_public_confirm") + : I18n.t("sidebar.sections.custom.mark_as_private_confirm"), + didConfirm: () => { + return this.#updateCall(); + }, + }); + } + + #updateCall() { return ajax(`/sidebar_sections/${this.transformedModel.id}`, { type: "PUT", contentType: "application/json", @@ -353,6 +370,14 @@ export default class SidebarSectionForm extends Component { : "sidebar.sections.custom.add"; } + get isPublic() { + return this.transformedModel.public; + } + + get wasPublic() { + return this.model?.section?.public; + } + @afterRender focusNewRowInput(id) { document @@ -460,6 +485,9 @@ export default class SidebarSectionForm extends Component { this.flashType = "error"; }); }, + didCancel: () => { + this.closeModal(); + }, }); } @@ -471,7 +499,9 @@ export default class SidebarSectionForm extends Component { @action delete() { return this.dialog.yesNoConfirm({ - message: I18n.t("sidebar.sections.custom.delete_confirm"), + message: this.model.section.public + ? I18n.t("sidebar.sections.custom.delete_public_confirm") + : I18n.t("sidebar.sections.custom.delete_confirm"), didConfirm: () => { return ajax(`/sidebar_sections/${this.transformedModel.id}`, { type: "DELETE", diff --git a/config/locales/client.en.yml b/config/locales/client.en.yml index cad642d88c6..c7abeca864c 100644 --- a/config/locales/client.en.yml +++ b/config/locales/client.en.yml @@ -4635,6 +4635,9 @@ en: save: "Save" delete: "Delete" delete_confirm: "Are you sure you want to delete this section?" + delete_public_confirm: "This section is visible to everyone, are you sure you want to delete it?" + update_public_confirm: "Changes will be visible to everyone on this site. Are you sure?" + mark_as_private_confirm: "This section is visible to everyone. After the update, it will be visible only to you. Are you sure?" reset_confirm: "Are you sure you want to reset this section to default?" public: "Visible to everyone" always_public: "Content in this section is always public" diff --git a/spec/system/custom_sidebar_sections_spec.rb b/spec/system/custom_sidebar_sections_spec.rb index 09de7465289..810825c2ae5 100644 --- a/spec/system/custom_sidebar_sections_spec.rb +++ b/spec/system/custom_sidebar_sections_spec.rb @@ -231,6 +231,7 @@ describe "Custom sidebar sections", type: :system do latest_link = find(".draggable[data-link-name='Sidebar Latest']") tags_link.drag_to(latest_link, html5: true, delay: 0.4) section_modal.save + expect(section_modal).to be_closed expect(sidebar.primary_section_links("my-section")).to eq( ["Sidebar Categories", "Sidebar Tags", "Sidebar Latest"], @@ -292,6 +293,7 @@ describe "Custom sidebar sections", type: :system do sidebar.edit_custom_section("My section") section_modal.delete + expect(section_modal).to have_text("Are you sure you want to delete this section?") section_modal.confirm_delete expect(sidebar).to have_no_section("My section") @@ -315,15 +317,51 @@ describe "Custom sidebar sections", type: :system do section_modal.fill_name("Edited public section") section_modal.save + expect(section_modal).to have_text( + "Changes will be visible to everyone on this site. Are you sure?", + ) + + section_modal.confirm_update + expect(sidebar).to have_section("Edited public section") sidebar.edit_custom_section("Edited public section") section_modal.delete + expect(section_modal).to have_text( + "This section is visible to everyone, are you sure you want to delete it?", + ) section_modal.confirm_delete expect(sidebar).to have_no_section("Edited public section") end + it "displays warning when public section is marked as private" do + sign_in admin + visit("/latest") + sidebar.click_add_section_button + + section_modal.fill_name("Public section") + section_modal.fill_link("Sidebar Tags", "/tags") + section_modal.mark_as_public + section_modal.save + + sidebar.edit_custom_section("Public section") + section_modal.fill_name("Edited public section") + section_modal.mark_as_public + section_modal.save + + expect(section_modal).to have_text( + "This section is visible to everyone. After the update, it will be visible only to you. Are you sure?", + ) + + section_modal.confirm_update + + expect(sidebar).to have_section("Edited public section") + expect(page).not_to have_css( + ".sidebar-section[data-section-name='edited-public-section'] .d-icon-globe", + ) + end + it "shows anonymous public sections" do sidebar_section = Fabricate(:sidebar_section, title: "Public section", public: true) sidebar_url_1 = Fabricate(:sidebar_url, name: "Sidebar Tags", value: "/tags") diff --git a/spec/system/editing_sidebar_community_section_spec.rb b/spec/system/editing_sidebar_community_section_spec.rb index 16538d4807b..997ce6d12c2 100644 --- a/spec/system/editing_sidebar_community_section_spec.rb +++ b/spec/system/editing_sidebar_community_section_spec.rb @@ -30,6 +30,7 @@ RSpec.describe "Editing Sidebar Community Section", type: :system do modal.fill_link("Topics", "/latest", "paper-plane") modal.topics_link.drag_to(modal.review_link, delay: 0.4) modal.save + modal.confirm_update expect(sidebar.primary_section_links("community")).to eq( ["My Posts", "Topics", "Review", "Admin", "More"], diff --git a/spec/system/page_objects/modals/sidebar_section_form.rb b/spec/system/page_objects/modals/sidebar_section_form.rb index bb3a989e251..08fc10cecd8 100644 --- a/spec/system/page_objects/modals/sidebar_section_form.rb +++ b/spec/system/page_objects/modals/sidebar_section_form.rb @@ -29,6 +29,12 @@ module PageObjects def confirm_delete find(".dialog-container .btn-primary").click + closed? + end + + def confirm_update + find(".dialog-container .btn-primary").click + closed? end def reset @@ -40,7 +46,6 @@ module PageObjects def save find("#save-section").click - closed? self end