mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-27 01:43:22 +00:00
Fix cart disappearing bug after adding a voucher
The split checkout page uses `mrujs` and `CableCar` to set the form as a remote one and perform `CableCar` operation if any : https://mrujs.com/how-tos/integrate-cablecar The previous solution broke the cart handled by angularJS, it looks like `morpdom` (https://mrujs.com/references/remote-forms-and-links Navigation Adapter section ) was doing something that angularJS didn't like when redirecting to the current page. With the power of `CableCar`, we now replace the #voucher-section on the split checkout page instead of doing a redirect.
This commit is contained in:
committed by
Maikel Linke
parent
94294fa161
commit
fe9b94a80e
@@ -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)
|
||||
|
||||
@@ -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")
|
||||
|
||||
22
app/views/split_checkout/_voucher_section.html.haml
Normal file
22
app/views/split_checkout/_voucher_section.html.haml
Normal file
@@ -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
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user