Handle user password responses with turbo_stream

This commit is contained in:
wandji20
2024-10-13 00:59:16 +01:00
parent 742d0d8fae
commit 1af298555b
4 changed files with 28 additions and 22 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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) }

View File

@@ -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!"