diff --git a/app/components/tag_rule_group_form_component.rb b/app/components/tag_rule_group_form_component.rb index 0eb6ea45a8..13f8e725dd 100644 --- a/app/components/tag_rule_group_form_component.rb +++ b/app/components/tag_rule_group_form_component.rb @@ -1,13 +1,14 @@ # frozen_string_literal: true class TagRuleGroupFormComponent < ViewComponent::Base - def initialize(group:, index:, customer_rule_index:) + def initialize(group:, index:, customer_rule_index:, tag_rule_types:) @group = group @index = index @customer_rule_index = customer_rule_index + @tag_rule_types = tag_rule_types end - attr_reader :group, :index, :customer_rule_index + attr_reader :group, :index, :customer_rule_index, :tag_rule_types private @@ -15,6 +16,10 @@ class TagRuleGroupFormComponent < ViewComponent::Base "tg_#{index}" end + def customer_tag_rule_div_id + "new-customer-tag-rule-#{index}" + end + def tag_list_input_name "group[#{index}][preferred_customer_tags]" end diff --git a/app/components/tag_rule_group_form_component/tag_rule_group_form_component.html.haml b/app/components/tag_rule_group_form_component/tag_rule_group_form_component.html.haml index 22849701e2..cf6746ec0a 100644 --- a/app/components/tag_rule_group_form_component/tag_rule_group_form_component.html.haml +++ b/app/components/tag_rule_group_form_component/tag_rule_group_form_component.html.haml @@ -1,4 +1,5 @@ %div{ id: form_id, "data-controller": "tag-rule-group-form-component--tag-rule-group-form"} + - rule_index = customer_rule_index .customer_tag .header %table @@ -8,26 +9,43 @@ %tr %td %h5 + = #TODO translation = t('.for_customers_tagged') %td -# TODO multiple tags ? = render(TagListInputComponent.new(name: tag_list_input_name, tags: group[:tags], hidden_field_data_options: { "data-action": "input->tag-rule-group-form-component--tag-rule-group-form#updatePreferredCustomerTag", "data-tag-rule-group-form-component--tag-rule-group-form-target": "customerTag" })) + %div{ id: customer_tag_rule_div_id } - if group[:rules].empty? .no_rules = t('components.tag_rule_group_form.no_rules_yet') - else - #new-customer-tag-rule - - rule_index = customer_rule_index - - group[:rules].each_with_index do |rule, rule_index| - -rule_index += 1 - = render(TagRuleFormComponent.new(rule: rule, - rule_data: helpers.rule_data(rule), - index: rule_index, - customer_tags: group[:tags], - hidden_field_customer_tag_options: { "data-tag-rule-group-form-component--tag-rule-group-form-target": "ruleCustomerTag" })) + - group[:rules].each do |rule| + -rule_index += 1 + = render(TagRuleFormComponent.new(rule: rule, + rule_data: helpers.rule_data(rule), + index: rule_index, + customer_tags: group[:tags], + hidden_field_customer_tag_options: { "data-tag-rule-group-form-component--tag-rule-group-form-target": "ruleCustomerTag" })) %hr .add_rule.text-center - %input.button{ type: 'button', value: t('.add_new_rule') } + %input.button{ type: 'button', value: t('components.tag_rule_group_form.add_new_rule'), "data-controller": "modal-link", "data-action": "click->modal-link#open", "data-modal-link-target-value": "tag_rule_add_new_rule_#{index}" } + + -# TODO sort this out, should probably create a component, need to make sure to use the correct index, and insert in the correct place, + -# need to dinamically have the correct index and div id, or create 2 diferent modal. Probaly component that create 2 different modal, need to sort out the opening of modal + = render ModalComponent.new(id: "tag_rule_add_new_rule_#{index}", close_button: false, modal_class: "tiny" ) do + #new-tag-rule-dialog{ "data-controller": "default-tag-rule" } + = hidden_field_tag "current_index", (rule_index + 1), { "data-default-tag-rule-target": "index" } + = hidden_field_tag "customer_tag", group[:tags], { "data-default-tag-rule-target": "customerTag", "data-tag-rule-group-form-component--tag-rule-group-form-target": "ruleCustomerTag" } + =# TODO pass all of this as value + = hidden_field_tag "div_id", customer_tag_rule_div_id, { "data-default-tag-rule-target": "divId" } + = hidden_field_tag "is_default", false, { "data-default-tag-rule-target": "isDefault" } + .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{ type: 'button', value: "#{t('.new_tag_rule_dialog.add_rule')}", "data-action": "click->default-tag-rule#add click->modal#close", } + diff --git a/app/controllers/admin/enterprises_controller.rb b/app/controllers/admin/enterprises_controller.rb index 90f583c210..39353354e9 100644 --- a/app/controllers/admin/enterprises_controller.rb +++ b/app/controllers/admin/enterprises_controller.rb @@ -50,6 +50,10 @@ module Admin @object.build_custom_tab if @object.custom_tab.nil? load_tag_rule_types + # TODO translation + @visibility_options = [ + [t('js.tag_rules.visible'), "visible"], [t('js.tag_rules.not_visible'), "hidden"] + ] return unless params[:stimulus] @@ -147,6 +151,17 @@ module Admin end end + def new_tag_rule_group + load_tag_rule_types + + @index = params[:index] + @group = { tags: [], rules: [] } + + respond_to do |format| + format.turbo_stream + end + end + protected def delete_custom_tab diff --git a/app/controllers/admin/tag_rules_controller.rb b/app/controllers/admin/tag_rules_controller.rb index 7919a7f725..6764dd6c16 100644 --- a/app/controllers/admin/tag_rules_controller.rb +++ b/app/controllers/admin/tag_rules_controller.rb @@ -6,9 +6,15 @@ module Admin def new @index = params[:index] + @div_id = params[:div_id] + is_default = params[:is_default] + @customer_tags = params[:customer_tags] + # TODO translation + @visibility_options = [[t('js.tag_rules.visible'), "visible"], + [t('js.tag_rules.not_visible'), "hidden"]] status = :ok if permitted_tag_rule_type.include?(params[:rule_type]) - @default_rule = "TagRule::#{params[:rule_type]}".constantize.new(is_default: true) + @default_rule = "TagRule::#{params[:rule_type]}".constantize.new(is_default:) else flash.now[:error] = t(".not_supported_type") status = :internal_server_error diff --git a/app/models/spree/ability.rb b/app/models/spree/ability.rb index a040649084..f5c32f1694 100644 --- a/app/models/spree/ability.rb +++ b/app/models/spree/ability.rb @@ -150,7 +150,7 @@ module Spree can [:admin, :index, :create], Enterprise can [:read, :edit, :update, :remove_logo, :remove_promo_image, :remove_terms_and_conditions, - :bulk_update, :resend_confirmation], Enterprise do |enterprise| + :bulk_update, :resend_confirmation, :new_tag_rule_group], Enterprise do |enterprise| OpenFoodNetwork::Permissions.new(user).editable_enterprises.include? enterprise end can [:welcome, :register], Enterprise do |enterprise| diff --git a/app/views/admin/enterprises/form/_tag_rules.html.haml b/app/views/admin/enterprises/form/_tag_rules.html.haml index bfe5347aaa..c8adb69ed2 100644 --- a/app/views/admin/enterprises/form/_tag_rules.html.haml +++ b/app/views/admin/enterprises/form/_tag_rules.html.haml @@ -1,6 +1,7 @@ .row .eleven.columns.alpha.omega %div{ "data-turbo": true } + - current_group_index = 0 - rules = @enterprise.tag_rules.prioritised.reject(&:is_default) - if rules.empty? .no_tags @@ -10,9 +11,13 @@ #customer-tag-rule -# TODO both index can't overlap - - customer_rule_index = 10 + - customer_rule_index = 0 - tag_groups(rules).each_with_index do |group, group_index| - = render TagRuleGroupFormComponent.new(group:, index: group_index, customer_rule_index: ) + - current_group_index = group_index + 1 + - customer_rule_index +=10 + = render TagRuleGroupFormComponent.new(group:, index: group_index, customer_rule_index:, tag_rule_types: @tag_rule_types) + + .add_tag{ "data-controller": "tag-rule" } + = hidden_field_tag "customer_rule_index", current_group_index, { "data-tag-rule-target": "index" } + %input.button{ type: 'button', value: t('.add_new_tag'), "data-action": "click->tag-rule#add" } - .add_tag - %input.button{ type: 'button', value: t('.add_new_tag') } 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 63133c68b2..36c01ba666 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 @@ -1,4 +1,4 @@ -.default_rules{ "data-turbo": true } +.default_rules .header %table %colgroup @@ -14,9 +14,6 @@ - if default_rules.empty? .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| - current_index = index + 1 @@ -28,7 +25,9 @@ = 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" } + = hidden_field_tag "current_index", current_index, { "data-default-tag-rule-target": "index" } + = hidden_field_tag "div_id", "default-tag-rule", { "data-default-tag-rule-target": "divId" } + = hidden_field_tag "is_default", true, { "data-default-tag-rule-target": "isDefault" } .text-normal.margin-bottom-30.text-center = t('.new_tag_rule_dialog.select_rule_type') .text-center.margin-bottom-30 diff --git a/app/views/admin/tag_rules/new.turbo_stream.haml b/app/views/admin/tag_rules/new.turbo_stream.haml index 42e33609f5..c77eff1c3e 100644 --- a/app/views/admin/tag_rules/new.turbo_stream.haml +++ b/app/views/admin/tag_rules/new.turbo_stream.haml @@ -2,5 +2,9 @@ = 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) + = turbo_stream.append @div_id do + = render(TagRuleFormComponent.new(rule: @default_rule, + rule_data: rule_data(@default_rule), + index: @index, customer_tags: @customer_tags, + hidden_field_customer_tag_options: { "data-tag-rule-group-form-component--tag-rule-group-form-target": "ruleCustomerTag" })) + = turbo_stream.remove_all ".no_rules" diff --git a/app/webpacker/controllers/default_tag_rule_controller.js b/app/webpacker/controllers/default_tag_rule_controller.js index 502c4cd1a7..1e301a5cac 100644 --- a/app/webpacker/controllers/default_tag_rule_controller.js +++ b/app/webpacker/controllers/default_tag_rule_controller.js @@ -1,19 +1,26 @@ import { Controller } from "stimulus"; export default class extends Controller { - static targets = ["rule", "index"]; + static targets = ["rule", "index", "divId", "isDefault", "customerTag"]; add() { + console.log(this.divIdTarget.value); const rule_type = this.ruleTarget.value; const index = this.indexTarget.value; + const divId = this.divIdTarget.value; + const isDefault = this.isDefaultTarget.value; + const customerTags = this.customerTagTarget.value; // fetch from backend - fetch(`tag_rules/new?rule_type=${rule_type}&index=${index}`, { - method: "GET", - headers: { - Accept: "text/vnd.turbo-stream.html", + fetch( + `tag_rules/new?rule_type=${rule_type}&index=${index}&div_id=${divId}&is_default=${isDefault}&customer_tags=${customerTags}`, + { + method: "GET", + headers: { + Accept: "text/vnd.turbo-stream.html", + }, }, - }) + ) .then((r) => r.text()) .then((html) => { Turbo.renderStreamMessage(html); diff --git a/config/locales/en.yml b/config/locales/en.yml index 078e7cd514..d65796d58f 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -1419,10 +1419,11 @@ en: new_tag_rule_dialog: select_rule_type: "Select a rule type:" add_rule: "Add 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' @@ -5078,7 +5079,9 @@ See the %{link} to find out more about %{sitename}'s features and to start using previous: Previous tag_list_input: default_placeholder: Add a tag - + tag_rule_group_form: + add_new_rule: '+ Add A New Rule' + no_rules_yet: No rules apply to this tag yet # Gem to prevent bot form submissions invisible_captcha: diff --git a/config/routes/admin.rb b/config/routes/admin.rb index 9737ccf0dd..5a0875ee64 100644 --- a/config/routes/admin.rb +++ b/config/routes/admin.rb @@ -33,6 +33,7 @@ Openfoodnetwork::Application.routes.draw do member do get :welcome patch :register + get :new_tag_rule_group end resources :connected_apps, only: [:create, :destroy]