diff --git a/app/controllers/user_passwords_controller.rb b/app/controllers/user_passwords_controller.rb index b0ad3970d1..26e4bee4ff 100644 --- a/app/controllers/user_passwords_controller.rb +++ b/app/controllers/user_passwords_controller.rb @@ -1,8 +1,6 @@ # frozen_string_literal: true class UserPasswordsController < Spree::UserPasswordsController - include CablecarResponses - layout 'darkswarm' def create @@ -11,31 +9,37 @@ class UserPasswordsController < Spree::UserPasswordsController self.resource = resource_class.send_reset_password_instructions(raw_params[resource_name]) if resource.errors.empty? - render cable_ready: cable_car.inner_html( - "#forgot-feedback", - partial("layouts/alert", locals: { type: "success", message: t(:password_reset_sent) }) - ) + @message = t(:password_reset_sent) + @type = :success + respond_to do |format| + format.html { head :ok } + format.turbo_stream { render :create } + end else - render status: :not_found, cable_ready: cable_car.inner_html( - "#forgot-feedback", - partial("layouts/alert", locals: { type: "alert", message: t(:email_not_found) }) - ) + @type = :alert + @message = t(:email_not_found) + respond_to do |format| + format.html { head :not_found } + format.turbo_stream { render :create, status: :not_found } + end end end private def render_unconfirmed_response - render status: :unprocessable_entity, cable_ready: cable_car.inner_html( - "#forgot-feedback", - partial("layouts/alert", - locals: { type: "alert", message: t(:email_unconfirmed), - unconfirmed: true, tab: "forgot" }) - ) + @type = :alert + @message = t(:email_unconfirmed) + @unconfirmed = true + @tab = 'forgot' + respond_to do |format| + format.html { head :unprocessable_entity } + format.turbo_stream { render :create, status: :unprocessable_entity } + end end def user_unconfirmed? - user = Spree::User.find_by(email: params.dig(:spree_user, :email)) - user && !user.confirmed? + @user = Spree::User.find_by(email: params.dig(:spree_user, :email)) + @user && !@user.confirmed? end end diff --git a/app/views/layouts/_forgot_tab.html.haml b/app/views/layouts/_forgot_tab.html.haml index 47ad3c03d2..80a9727b60 100644 --- a/app/views/layouts/_forgot_tab.html.haml +++ b/app/views/layouts/_forgot_tab.html.haml @@ -1,5 +1,5 @@ #forgot-tab - = form_with url: spree_user_password_path, scope: :spree_user, data: { remote: "true" } do |form| + = form_with url: spree_user_password_path, scope: :spree_user, data: { turbo: true } do |form| .row .large-12.columns#forgot-feedback diff --git a/app/views/user_passwords/create.turbo_stream.haml b/app/views/user_passwords/create.turbo_stream.haml new file mode 100644 index 0000000000..9783307018 --- /dev/null +++ b/app/views/user_passwords/create.turbo_stream.haml @@ -0,0 +1,2 @@ += turbo_stream.update 'forgot-feedback' do + = render partial: 'layouts/alert', locals: { type: @type, message: @message, tab: @tab, unconfirmed: @unconfirmed, email: params.dig(:spree_user, :email) } \ No newline at end of file diff --git a/spec/controllers/user_passwords_controller_spec.rb b/spec/controllers/user_passwords_controller_spec.rb index 594f5dbd79..a7796e37f4 100644 --- a/spec/controllers/user_passwords_controller_spec.rb +++ b/spec/controllers/user_passwords_controller_spec.rb @@ -14,20 +14,20 @@ RSpec.describe UserPasswordsController, type: :controller do describe "create" do it "returns 404 if user is not found" do - spree_post :create, spree_user: { email: "xxxxxxxxxx@example.com" } + spree_post :create, spree_user: { email: "xxxxxxxxxx@example.com" }, format: :turbo_stream expect(response.status).to eq 404 expect(response.body).to match 'Email address not found' end it "returns 422 if user is registered but not confirmed" do - spree_post :create, spree_user: { email: unconfirmed_user.email } + spree_post :create, spree_user: { email: unconfirmed_user.email }, format: :turbo_stream expect(response.status).to eq 422 expect(response.body).to match "You must confirm your email \ address before you can reset your password." end it "returns 200 when password reset was successful" do - spree_post :create, spree_user: { email: user.email } + spree_post :create, spree_user: { email: user.email }, format: :turbo_stream expect(response.status).to eq 200 expect(response.body).to match "An email with instructions on resetting \ your password has been sent!"