diff --git a/app/controllers/split_checkout_controller.rb b/app/controllers/split_checkout_controller.rb index a125ca6e20..9282fa9fba 100644 --- a/app/controllers/split_checkout_controller.rb +++ b/app/controllers/split_checkout_controller.rb @@ -31,7 +31,7 @@ class SplitCheckoutController < ::BaseController def update if add_voucher - return redirect_to checkout_step_path(:payment) + return render_voucher_section elsif @order.errors.present? return render_error end @@ -56,7 +56,7 @@ class SplitCheckoutController < ::BaseController adjustment = Spree::Adjustment.find_by(id: params[:adjustment_id]) adjustment.destroy - redirect_to checkout_step_path(:payment) + render_voucher_section end private @@ -72,6 +72,20 @@ class SplitCheckoutController < ::BaseController replace("#flashes", partial("shared/flashes", locals: { flashes: flash })) end + # Using the power of cable_car we replace only the #voucher_section instead of reloading the page + def render_voucher_section + render( + status: :ok, + operations: cable_car.replace( + "#voucher-section", + partial( + "split_checkout/voucher_section", + locals: { order: @order, voucher_adjustment: @order.vouchers.first } + ) + ) + ) + end + def order_error_messages # Remove ship_address.* errors if no shipping method is not selected remove_ship_address_errors if no_ship_address_needed? @@ -193,7 +207,7 @@ class SplitCheckoutController < ::BaseController end def add_voucher - return unless payment_step? && params[:order] && params[:order][:voucher_code] + return unless payment_step? && params[:order] && params[:order][:voucher_code].present? # Fetch Voucher voucher = Voucher.find_by(code: params[:order][:voucher_code], enterprise: @order.distributor) diff --git a/app/views/split_checkout/_payment.html.haml b/app/views/split_checkout/_payment.html.haml index d211686e8f..08e4a846b3 100644 --- a/app/views/split_checkout/_payment.html.haml +++ b/app/views/split_checkout/_payment.html.haml @@ -1,24 +1,6 @@ .medium-6 %div.checkout-substep{"data-controller": "paymentmethod"} - - if @order.distributor.vouchers.present? - .checkout-title - = t("split_checkout.step2.voucher.apply_voucher") - - .checkout-input - .two-columns-inputs.voucher - - if @voucher_adjustment.present? - %span.button.voucher-added - %i.ofn-i_051-check-big - = "#{@voucher_adjustment.originator.display_value} #{t("split_checkout.step2.voucher.voucher")}" - = link_to t("split_checkout.step2.voucher.remove_code"), checkout_destroy_path(adjustment_id: @voucher_adjustment.id), method: "delete", data: { confirm: t("split_checkout.step2.voucher.confirm_delete") } - - if @voucher_adjustment.originator.value > @order.total - .checkout-input - %span.formError.standalone - = t("split_checkout.step2.voucher.warning_forfeit_remaining_amount") - - else - = f.text_field :voucher_code, { placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher" } - - # TODO: enable button when code entered - = f.submit t("split_checkout.step2.voucher.apply"), class: "button cancel voucher", disabled: false + = render "split_checkout/voucher_section", { order: @order, voucher_adjustment: @voucher_adjustment } %div.checkout-title = t("split_checkout.step2.payment_method.title") diff --git a/app/views/split_checkout/_voucher_section.html.haml b/app/views/split_checkout/_voucher_section.html.haml new file mode 100644 index 0000000000..10271cdedd --- /dev/null +++ b/app/views/split_checkout/_voucher_section.html.haml @@ -0,0 +1,22 @@ +%div#voucher-section + = form_with url: checkout_update_path(:payment), model: order, method: :put, data: { remote: "true" } do |f| + - if order.distributor.vouchers.present? + .checkout-title + = t("split_checkout.step2.voucher.apply_voucher") + + .checkout-input + .two-columns-inputs.voucher + - if voucher_adjustment.present? + %span.button.voucher-added + %i.ofn-i_051-check-big + = "#{voucher_adjustment.originator.display_value} #{t("split_checkout.step2.voucher.voucher")}" + = link_to t("split_checkout.step2.voucher.remove_code"), checkout_destroy_path(adjustment_id: voucher_adjustment.id), method: "delete", data: { confirm: t("split_checkout.step2.voucher.confirm_delete") } + - # TODO: this might not be true, ie payment method include fee which wouldn't be covered by voucher, tax implication raise total to be bigger than voucher other ? + - if voucher_adjustment.originator.value > order.total + .checkout-input + %span.formError.standalone + = t("split_checkout.step2.voucher.warning_forfeit_remaining_amount") + - else + = f.text_field :voucher_code, { placeholder: t("split_checkout.step2.voucher.placeholder"), class: "voucher" } + - # TODO: enable button when code entered + = f.submit t("split_checkout.step2.voucher.apply"), class: "button cancel voucher", disabled: false diff --git a/spec/controllers/split_checkout_controller_spec.rb b/spec/controllers/split_checkout_controller_spec.rb index 9c834e7123..0739a53537 100644 --- a/spec/controllers/split_checkout_controller_spec.rb +++ b/spec/controllers/split_checkout_controller_spec.rb @@ -251,7 +251,7 @@ describe SplitCheckoutController, type: :controller do it "adds a voucher to the order" do put :update, params: params - expect(response).to redirect_to checkout_step_path(:payment) + expect(response.status).to eq(200) expect(order.reload.vouchers.length).to eq(1) end @@ -292,7 +292,7 @@ describe SplitCheckoutController, type: :controller do delete :destroy, params: { adjustment_id: adjustment.id } - expect(response).to redirect_to checkout_step_path(:payment) + expect(response.status).to eq(200) expect(order.reload.vouchers.length).to eq(0) end end