diff --git a/app/components/tag_rule_form_component/tag_rule_form_component.html.haml b/app/components/tag_rule_form_component/tag_rule_form_component.html.haml index 54a7cdd260..164d0f07f4 100644 --- a/app/components/tag_rule_form_component/tag_rule_form_component.html.haml +++ b/app/components/tag_rule_form_component/tag_rule_form_component.html.haml @@ -6,7 +6,7 @@ %col.actions{ width: "10%" } %tr %td - = hidden_field_tag element_name("id") + = hidden_field_tag element_name("id"), rule.id = hidden_field_tag element_name("type"), rule.type = hidden_field_tag element_name("priority"), index = hidden_field_tag element_name("is_default"), rule.is_default diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index e10e2c06b8..90f583c210 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -379,16 +379,15 @@ module Admin end def load_tag_rule_types - # Load rule types @tag_rule_types = [ - { id: "FilterShippingMethods", name: t('js.tag_rules.show_hide_shipping') }, - { id: "FilterPaymentMethods", name: t('js.tag_rules.show_hide_payment') }, - { id: "FilterOrderCycles", name: t('js.tag_rules.show_hide_order_cycles') } + [t(".form.tag_rules.show_hide_shipping"), "FilterShippingMethods"], + [t(".form.tag_rules.show_hide_payment"), "FilterPaymentMethods"], + [t(".form.tag_rules.show_hide_order_cycles"), "FilterOrderCycles"] ] return unless helpers.feature?(:inventory, @object) - @tag_rule_types.prepend({ id: "FilterProducts", name: t('js.tag_rules.show_hide_variants') }) + @tag_rule_types.prepend([t(".form.tag_rules.show_hide_variants"), "FilterProducts"]) end def setup_property diff --git a/app/controllers/admin/tag_rules_controller.rb b/app/controllers/admin/tag_rules_controller.rb index d7a749a3a7..d88fe45529 100644 --- a/app/controllers/admin/tag_rules_controller.rb +++ b/app/controllers/admin/tag_rules_controller.rb @@ -8,6 +8,21 @@ module Admin success: lambda { head :no_content } } } + def new + @index = params[:index] + status = :ok + if permitted_tag_rule_type.include?(params[:rule_type]) + @default_rule = "TagRule::#{params[:rule_type]}".constantize.new(is_default: true) + else + flash.now[:error] = t(".not_supported_type") + status = :internal_server_error + end + + respond_with do |format| + return format.turbo_stream { render :new, status: } + end + end + def map_by_tag respond_to do |format| format.json do @@ -39,5 +54,9 @@ module Admin Enterprise.managed_by(spree_current_user) end end + + def permitted_tag_rule_type + %w{FilterOrderCycles FilterPaymentMethods FilterProducts FilterShippingMethods} + end end end diff --git a/app/models/spree/ability.rb b/app/models/spree/ability.rb index ea1c67f827..a040649084 100644 --- a/app/models/spree/ability.rb +++ b/app/models/spree/ability.rb @@ -142,6 +142,7 @@ module Spree can [:admin, :index, :read, :create, :edit, :update_positions, :destroy], ProducerProperty + can :new, TagRule can [:admin, :map_by_tag, :destroy], TagRule do |tag_rule| user.enterprises.include? tag_rule.enterprise end diff --git a/app/views/admin/enterprises/form/tag_rules/_default_rules.html.haml b/app/views/admin/enterprises/form/tag_rules/_default_rules.html.haml index 66b7aeeebb..63133c68b2 100644 --- a/app/views/admin/enterprises/form/tag_rules/_default_rules.html.haml +++ b/app/views/admin/enterprises/form/tag_rules/_default_rules.html.haml @@ -15,6 +15,7 @@ .no_rules = t('.no_rules_yet') .add_rule.text-center + -# TODO add pop up %input.button.icon-plus{ type: 'button', value: t('.add_new_button') } - else - default_rules.each_with_index do |default_rule, index| @@ -23,8 +24,18 @@ %hr .add_rule.text-center - %input.button.icon-plus{ type: 'button', value: t('.add_new_button') } + %input.button.icon-plus{ type: 'button', value: t('.add_new_button'), "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "tag_rule_add_new_default_rule" } += render ModalComponent.new(id: "tag_rule_add_new_default_rule", close_button: false, modal_class: "tiny" ) do + #new-tag-rule-dialog{ "data-controller": "default-tag-rule" } + =hidden_field_tag "current_index", current_index, { "data-default-tag-rule-target": "index" } + .text-normal.margin-bottom-30.text-center + = t('.new_tag_rule_dialog.select_rule_type') + .text-center.margin-bottom-30 + = select_tag :rule_type_selector, options_for_select(@tag_rule_types), { "data-controller": "tom-select", "data-default-tag-rule-target": "rule", class: "primary no-search" } + .text-center + %input.button.red.icon-plus{ type: 'button', value: "#{t('.new_tag_rule_dialog.add_rule')}", "data-action": "click->default-tag-rule#add click->modal#close", } + = render HelpModalComponent.new(id: "tag_rule_help_modal") do #tag-rule-help .margin-bottom-30.text-center diff --git a/app/views/admin/tag_rules/new.turbo_stream.haml b/app/views/admin/tag_rules/new.turbo_stream.haml new file mode 100644 index 0000000000..42e33609f5 --- /dev/null +++ b/app/views/admin/tag_rules/new.turbo_stream.haml @@ -0,0 +1,6 @@ +- if flash[:error] + = turbo_stream.append "flashes" do + = render(partial: 'admin/shared/flashes', locals: { flashes: flash }) +- else + = turbo_stream.append "default-tag-rule" do + = render TagRuleFormComponent.new(rule: @default_rule, rule_data: rule_data(@default_rule), index: @index) diff --git a/app/webpacker/controllers/default_tag_rule_controller.js b/app/webpacker/controllers/default_tag_rule_controller.js new file mode 100644 index 0000000000..502c4cd1a7 --- /dev/null +++ b/app/webpacker/controllers/default_tag_rule_controller.js @@ -0,0 +1,24 @@ +import { Controller } from "stimulus"; + +export default class extends Controller { + static targets = ["rule", "index"]; + + add() { + const rule_type = this.ruleTarget.value; + const index = this.indexTarget.value; + + // fetch from backend + fetch(`tag_rules/new?rule_type=${rule_type}&index=${index}`, { + method: "GET", + headers: { + Accept: "text/vnd.turbo-stream.html", + }, + }) + .then((r) => r.text()) + .then((html) => { + Turbo.renderStreamMessage(html); + this.indexTarget.value = parseInt(index) + 1; + }) + .catch((error) => console.warn(error)); + } +} diff --git a/config/locales/en.yml b/config/locales/en.yml index 1dcd04cb82..99cb93f167 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1416,11 +1416,18 @@ en: by_default: By Default no_rules_yet: No default rules apply yet add_new_button: '+ Add A New Default Rule' + new_tag_rule_dialog: + select_rule_type: "Select a rule type:" + add_rule: "Add Rule" no_tags_yet: No tags apply to this enterprise yet no_rules_yet: No rules apply to this tag yet for_customers_tagged: 'For customers tagged:' add_new_rule: '+ Add A New Rule' add_new_tag: '+ Add A New Tag' + show_hide_variants: 'Show or Hide variants in my shopfront' + show_hide_shipping: 'Show or Hide shipping methods at checkout' + show_hide_payment: 'Show or Hide payment methods at checkout' + show_hide_order_cycles: 'Show or Hide order cycles in my shopfront' users: legend: "Users" email_confirmation_notice_html: "Email confirmation is pending. We've sent a confirmation email to %{email}." @@ -1573,6 +1580,8 @@ en: invite_manager: user_already_exists: "User already exists" error: "Something went wrong" + tag_rules: + not_supported_type: Tag rule type not supported order_cycles: loading_flash: loading_order_cycles: LOADING ORDER CYCLES @@ -3672,9 +3681,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using order_cycle_tagged_bottom: "are:" inventory_tagged_top: "Inventory variants tagged" inventory_tagged_bottom: "are:" - new_tag_rule_dialog: - select_rule_type: "Select a rule type:" - add_rule: "Add Rule" enterprise_fees: inherit_from_product: "Inherit From Product" orders: @@ -3804,10 +3810,6 @@ See the %{link} to find out more about %{sitename}'s features and to start using changing_on_hand_stock: Changing on hand stock levels... stock_reset: Stocks reset to defaults. tag_rules: - show_hide_variants: 'Show or Hide variants in my shopfront' - show_hide_shipping: 'Show or Hide shipping methods at checkout' - show_hide_payment: 'Show or Hide payment methods at checkout' - show_hide_order_cycles: 'Show or Hide order cycles in my shopfront' visible: VISIBLE not_visible: NOT VISIBLE services: diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 9b844fcfd0..9737ccf0dd 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -41,7 +41,7 @@ Openfoodnetwork::Application.routes.draw do post :update_positions, on: :collection end - resources :tag_rules, only: [:destroy] + resources :tag_rules, only: [:destroy, :new] resources :vouchers, only: [:new, :create] end diff --git a/spec/controllers/admin/tag_rules_controller_spec.rb b/spec/controllers/admin/tag_rules_controller_spec.rb index 5e9cb14ab2..d2024420ee 100644 --- a/spec/controllers/admin/tag_rules_controller_spec.rb +++ b/spec/controllers/admin/tag_rules_controller_spec.rb @@ -34,4 +34,31 @@ RSpec.describe Admin::TagRulesController do end end end + + describe "#edit" do + let(:enterprise) { create(:distributor_enterprise) } + let(:params) { { rule_type:, index: 1 } } + let(:rule_type) { "FilterProducts" } + + before do + controller_login_as_enterprise_user [enterprise] + end + + it "returns new tag rule form" do + spree_get(:new, format: :turbo_stream, id: enterprise, params:) + + expect(response).to render_template :new + end + + context "wiht a wrong tag rule type" do + let(:rule_type) { "OtherType" } + + it "returns an error" do + spree_get(:new, format: :turbo_stream, id: enterprise, params:) + + expect(response).to render_template :new + expect(flash[:error]).to eq "Tag rule type not supported" + end + end + end end