Move guest checkout handling to the frontend

This commit is contained in:
Matt-Yorkley
2022-01-13 21:44:25 +00:00
parent 27c7b16b03
commit 8ce27bed86
7 changed files with 61 additions and 42 deletions

View File

@@ -16,16 +16,10 @@ class SplitCheckoutController < ::BaseController
helper OrderHelper
def edit
return redirect_to_step unless params[:step]
return redirect_to_guest if !spree_current_user &&
!@order.distributor.allow_guest_orders? &&
params[:step] != "guest"
redirect_to_step unless params[:step]
end
def update
return redirect_to_guest if !spree_current_user && !@order.distributor.allow_guest_orders?
if confirm_order || update_order
clear_invalid_payments
advance_order_state
@@ -33,10 +27,9 @@ class SplitCheckoutController < ::BaseController
else
flash.now[:error] = I18n.t('split_checkout.errors.global')
render operations: cable_car.
render status: :unprocessable_entity, operations: cable_car.
replace("#checkout", partial("split_checkout/checkout")).
replace("#flashes", partial("shared/flashes", locals: { flashes: flash })),
status: :unprocessable_entity
replace("#flashes", partial("shared/flashes", locals: { flashes: flash }))
end
end
@@ -91,13 +84,7 @@ class SplitCheckoutController < ::BaseController
@order_params ||= Checkout::Params.new(@order, params).call
end
def redirect_to_guest
redirect_to checkout_step_path(:guest)
end
def redirect_to_step
return redirect_to_guest if !spree_current_user && !params[:step]
case @order.state
when "cart", "address", "delivery"
redirect_to checkout_step_path(:details)

View File

@@ -1,4 +1,4 @@
%checkout.row#checkout
.small-12.medium-12.columns
= render partial: "split_checkout/tabs" unless checkout_step?(:guest)
= render partial: "split_checkout/tabs"
= render partial: "split_checkout/form"

View File

@@ -1,12 +1,13 @@
.medium-10
%div.checkout-guest-title
= t :checkout_headline
.checkout-step
.medium-10
%div.checkout-guest-title
= t :checkout_headline
%div.checkout-submit{ class: "#{@order.distributor.allow_guest_orders? ? 'checkout-submit-inline' : 'medium-6' }" }
%a.primary.button{href: main_app.login_path}
= t :label_login
-if @order.distributor.allow_guest_orders?
%span.checkout-submit-or
or
%a.button.cancel{href: main_app.checkout_step_path(:details)}
= t :checkout_as_guest
%div.checkout-submit{ class: "#{@order.distributor.allow_guest_orders? ? 'checkout-submit-inline' : 'medium-6' }" }
%a.primary.button{ href: main_app.login_path }
= t :label_login
-if @order.distributor.allow_guest_orders?
%span.checkout-submit-or
or
%button.button.cancel{ "data-action": "click->guest-checkout#guestSelected" }
= t :checkout_as_guest

View File

@@ -19,6 +19,12 @@
.sub-header.show-for-medium-down
= render partial: "shopping_shared/order_cycles"
= render partial: "checkout"
.row{ "data-controller": "guest-checkout", "data-guest-checkout-distributor-value": @order.distributor.id }
%div{ style: "display: #{spree_current_user ? 'block' : 'none'}", "data-guest-checkout-target": "checkout" }
= render partial: "checkout"
- unless spree_current_user
%div{ style: "display: block", "data-guest-checkout-target": "guest" }
= render partial: "guest"
= render partial: "shared/footer"

View File

@@ -0,0 +1,31 @@
import { Controller } from "stimulus"
export default class extends Controller {
static targets = ["checkout", "guest"];
static values = {
distributor: String,
session: { type: String, default: "guest-checkout" }
};
connect() {
if(!this.hasGuestTarget) { return }
if(this.usingGuestCheckout()) {
this.showCheckout();
}
}
showCheckout() {
this.checkoutTarget.style.display = "block";
this.guestTarget.style.display = "none";
}
guestSelected() {
this.showCheckout();
sessionStorage.setItem(this.sessionValue, this.distributorValue);
}
usingGuestCheckout() {
return sessionStorage.getItem(this.sessionValue) === this.distributorValue
}
}

View File

@@ -87,7 +87,7 @@ Openfoodnetwork::Application.routes.draw do
constraints SplitCheckoutConstraint.new do
get '/checkout', to: 'split_checkout#edit'
constraints step: /(guest|details|payment|summary)/ do
constraints step: /(details|payment|summary)/ do
get '/checkout/:step', to: 'split_checkout#edit', as: :checkout_step
put '/checkout/:step', to: 'split_checkout#update', as: :checkout_update
end

View File

@@ -53,26 +53,18 @@ describe "As a consumer, I want to checkout my order", js: true do
context "guest checkout when distributor doesn't allow guest orders" do
before do
visit checkout_path
visit checkout_step_path(:details)
end
it "should display the split checkout login page" do
expect(page).to have_content distributor.name
expect(page).to have_content("Ok, ready to checkout?")
expect(page).to have_content("Login")
expect(page).to have_no_content("Checkout as guest")
end
it "should be redirected if user enter the url" do
order.update(state: "payment")
get checkout_step_path(:details)
expect(response).to have_http_status(:redirect)
expect(page).to have_content("Ok, ready to checkout?")
end
it "should redirect to the login page when clicking the login button" do
click_on "Login"
expect(page).to have_content("Login")
expect(page).to have_current_path "/"
end
end
@@ -83,7 +75,7 @@ describe "As a consumer, I want to checkout my order", js: true do
visit checkout_path
end
it "should display the split checkout login/guest page" do
it "should display the split checkout login/guest form" do
expect(page).to have_content distributor.name
expect(page).to have_content("Ok, ready to checkout?")
expect(page).to have_content("Login")
@@ -126,7 +118,7 @@ describe "As a consumer, I want to checkout my order", js: true do
it "should allow visit '/checkout/details'" do
order.update(state: "payment")
visit checkout_step_path(:details)
expect(page).to have_button("Next - Payment method")
expect(page).to have_current_path("/checkout/details")
end
end
end
@@ -236,8 +228,10 @@ describe "As a consumer, I want to checkout my order", js: true do
before do
variant.update!(on_demand: false, on_hand: 0)
end
it "returns me to the cart with an error message" do
visit checkout_path
expect(page).not_to have_selector 'closing', text: "Checkout now"
expect(page).to have_selector 'closing', text: "Your shopping cart"
expect(page).to have_content "An item in your cart has become unavailable"