From 6fdf9fa03838d3595460ce906f26561da70d8fd2 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Bellet Date: Thu, 16 Mar 2023 16:05:04 +0100 Subject: [PATCH] Add/Remove white label logo in enterprise preferences panel --- app/reflexes/white_label_reflex.rb | 24 +++++++++++++++ .../enterprises/form/_white_label.html.haml | 17 +++++++++++ config/locales/en.yml | 4 +++ spec/system/admin/enterprises_spec.rb | 29 +++++++++++++++++++ 4 files changed, 74 insertions(+) create mode 100644 app/reflexes/white_label_reflex.rb diff --git a/app/reflexes/white_label_reflex.rb b/app/reflexes/white_label_reflex.rb new file mode 100644 index 0000000000..4412028c92 --- /dev/null +++ b/app/reflexes/white_label_reflex.rb @@ -0,0 +1,24 @@ +# frozen_string_literal: true + +class WhiteLabelReflex < ApplicationReflex + include EnterpriseConcern + delegate :view_context, to: :controller + + def remove_logo + @enterprise.update!(white_label_logo: nil) + + f = ActionView::Helpers::FormBuilder.new(:enterprise, @enterprise, view_context, {}) + + html = with_locale { + render(partial: "admin/enterprises/form/white_label", + locals: { f: f, enterprise: @enterprise }) + } + morph "#white_label_panel", html + + flash[:success] = with_locale { + I18n.t("admin.enterprises.form.white_label.remove_logo_success") + } + cable_ready.dispatch_event(name: "modal:close") + morph "#flashes", render(partial: "shared/flashes", locals: { flashes: flash }) + end +end diff --git a/app/views/admin/enterprises/form/_white_label.html.haml b/app/views/admin/enterprises/form/_white_label.html.haml index b083ec7700..bf1ae27aeb 100644 --- a/app/views/admin/enterprises/form/_white_label.html.haml +++ b/app/views/admin/enterprises/form/_white_label.html.haml @@ -1,6 +1,23 @@ +- @object ||= enterprise + .row .three.columns.alpha = f.label :hide_ofn_navigation, t('.hide_ofn_navigation') .three.columns = f.check_box :hide_ofn_navigation +.row{id: "white_label_logo"} + .three.columns.alpha + = f.label :white_label_logo, t('.upload_logo') + .three.columns + = image_tag @object.white_label_logo_url if @object.white_label_logo.present? + = f.file_field :white_label_logo, accept: "image/*" + - if @object.white_label_logo.variable? + %button.button.small{ type: "button", "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "remove_logo" } + = t('.remove_logo') + + +- if @object.white_label_logo.variable? + = render ConfirmModalComponent.new(id: "remove_logo", confirm_reflexes: "click->WhiteLabel#remove_logo" ) do + .margin-bottom-30 + = t('.remove_logo_confirm') diff --git a/config/locales/en.yml b/config/locales/en.yml index 37e31bca99..e9fba7e4f8 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1161,6 +1161,10 @@ en: white_label: legend: "White Label" hide_ofn_navigation: "Hide OFN navigation" + upload_logo: "Logo used in shopfront" + remove_logo: "Remove logo" + remove_logo_confirm: "Are you sure you want to remove this logo?" + remove_logo_success: "Logo removed" actions: edit_profile: Settings properties: Properties diff --git a/spec/system/admin/enterprises_spec.rb b/spec/system/admin/enterprises_spec.rb index 4062a53617..eaf981c30c 100644 --- a/spec/system/admin/enterprises_spec.rb +++ b/spec/system/admin/enterprises_spec.rb @@ -10,6 +10,7 @@ describe ' include AuthenticationHelper include ShopWorkflow include UIComponentHelper + include FileHelper it "viewing an enterprise" do e = create(:enterprise) @@ -645,6 +646,34 @@ describe ' expect(page).not_to have_link "White Label" end end + + context "when white label is active via `hide_ofn_navigation`" do + before do + distributor1.update_attribute(:preferred_hide_ofn_navigation, true) + end + + it "can updload the white label logo for the current shop" do + attach_file "enterprise_white_label_logo", white_logo_path + click_button 'Update' + expect(flash_message).to eq('Enterprise "First Distributor" has been successfully updated!') + expect(distributor1.reload.white_label_logo_blob.filename).to eq("logo-white.png") + end + + context "when enterprise has a white label logo" do + before do + distributor1.update white_label_logo: white_logo_file + end + + it "can remove the white label logo for the current shop" do + click_button "Remove" + within ".reveal-modal" do + click_button "Confirm" + end + expect(flash_message).to eq("Logo removed") + expect(distributor1.reload.white_label_logo).to be_nil + end + end + end end end end