Skip to content

Commit

Permalink
wip: store credit
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed Jan 15, 2025
1 parent 3e0a177 commit fba7722
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 79 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag :edit_amount_modal do %>
<%= turbo_frame_tag :resource_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @store_credit, url: solidus_admin.update_amount_user_store_credit_path(@user, @store_credit), method: :put, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag :edit_memo_modal do %>
<%= turbo_frame_tag :resource_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @store_credit, url: solidus_admin.update_memo_user_store_credit_path(@user, @store_credit), method: :put, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<%= turbo_frame_tag :edit_validity_modal do %>
<%= turbo_frame_tag :resource_modal do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @store_credit, url: solidus_admin.invalidate_user_store_credit_path(@user, @store_credit), method: :put, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
tag: :a,
href: solidus_admin.new_user_store_credit_path(user_id: @user.id),
data: {
turbo_frame: :new_store_credit_modal,
turbo_frame: :resource_modal,
turbo_prefetch: false
},
text: t(".add_store_credit"),
Expand Down Expand Up @@ -48,7 +48,7 @@
<%= t(".no_credits_found") %>
<%= render component("ui/button").new(
"data-action": "click->#{stimulus_id}#actionButtonClicked",
"data-#{stimulus_id}-url-param": solidus_admin.new_user_store_credit_path(user_id: @user.id, _turbo_frame: :new_store_credit_modal),
"data-#{stimulus_id}-url-param": solidus_admin.new_user_store_credit_path(user_id: @user.id, _turbo_frame: :resource_modal),
text: t(".create_one"),
)%>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def tabs

def turbo_frames
%w[
new_store_credit_modal
resource_modal
]
end

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%= turbo_frame_tag :new_store_credit_modal do %>
<%= turbo_frame_tag :resource_modal, target: "_top" do %>
<%= render component("ui/modal").new(title: t(".title")) do |modal| %>
<%= form_for @store_credit, url: solidus_admin.user_store_credits_path(@user), method: :post, html: { id: form_id } do |f| %>
<%= form_for @store_credit, url: form_url, method: :post, html: { id: form_id } do |f| %>
<div class="flex flex-col gap-6 pb-4">
<%= render component("ui/forms/field").text_field(f, :amount, class: "required") %>
<%= render component("ui/forms/field").select(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# frozen_string_literal: true

class SolidusAdmin::Users::StoreCredits::New::Component < SolidusAdmin::BaseComponent
class SolidusAdmin::Users::StoreCredits::New::Component < SolidusAdmin::Resource::New::Component
def initialize(user:, store_credit:, categories:)
@user = user
@store_credit = store_credit
@store_credit_categories = categories
end

def form_id
dom_id(@store_credit, "#{stimulus_id}_new_form")
def form_url
solidus_admin.user_store_credits_path(@user, **search_filter_params)
end

def currency_select_options
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,21 +30,21 @@
<%= render component("ui/button").new(
scheme: :danger,
"data-action": "click->#{stimulus_id}#actionButtonClicked",
"data-#{stimulus_id}-url-param": solidus_admin.edit_validity_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_validity_modal),
"data-#{stimulus_id}-url-param": solidus_admin.edit_validity_user_store_credit_path(@user, @store_credit, _turbo_frame: :resource_modal),
text: t(".invalidate"),
)%>
<% end %>

<%= render component("ui/button").new(
"data-action": "click->#{stimulus_id}#actionButtonClicked",
"data-#{stimulus_id}-url-param": solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_memo_modal),
"data-#{stimulus_id}-url-param": solidus_admin.edit_memo_user_store_credit_path(@user, @store_credit, _turbo_frame: :resource_modal),
text: t(".edit_memo"),
)%>

<% if @store_credit.editable? %>
<%= render component("ui/button").new(
"data-action": "click->#{stimulus_id}#actionButtonClicked",
"data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :edit_amount_modal),
"data-#{stimulus_id}-url-param": solidus_admin.edit_amount_user_store_credit_path(@user, @store_credit, _turbo_frame: :resource_modal),
text: t(".edit_amount"),
)%>
<% end %>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,7 @@ def tabs

def turbo_frames
%w[
edit_amount_modal
edit_memo_modal
edit_validity_modal
resource_modal
]
end

Expand Down
110 changes: 48 additions & 62 deletions admin/app/controllers/solidus_admin/store_credits_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

module SolidusAdmin
class StoreCreditsController < SolidusAdmin::BaseController
class StoreCreditsController < SolidusAdmin::ResourcesController
before_action :set_user
before_action :set_store_credit, only: [:show, :edit_amount, :update_amount, :edit_memo, :update_memo, :edit_validity, :invalidate]
before_action :set_store_credit_reasons, only: [:edit_amount, :update_amount, :edit_validity, :invalidate]
Expand Down Expand Up @@ -38,7 +38,7 @@ def new

def create
@store_credit = @user.store_credits.build(
permitted_store_credit_params.merge({
permitted_resource_params.merge({
created_by: spree_current_user,
action_originator: spree_current_user
})
Expand All @@ -48,17 +48,8 @@ def create
return unless ensure_store_credit_category { render_new_with_errors }

if @store_credit.save
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.user_store_credits_path(@user), status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
flash[:notice] = t('.success')
redirect_to after_create_path, status: :see_other
else
render_new_with_errors
end
Expand All @@ -72,7 +63,7 @@ def edit_amount
store_credit: @store_credit,
events: @store_credit_events,
reasons: @store_credit_reasons
)
)
}
end
end
Expand All @@ -81,18 +72,9 @@ def update_amount
return unless ensure_amount { render_edit_with_errors }
return unless ensure_store_credit_reason { render_edit_with_errors }

if @store_credit.update_amount(permitted_store_credit_params[:amount], @store_credit_reason, spree_current_user)
respond_to do |format|
flash[:notice] = t('.success')

format.html do
redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
if @store_credit.update_amount(permitted_resource_params[:amount], @store_credit_reason, spree_current_user)
flash[:notice] = t('.success')
redirect_to after_update_path, status: :see_other
else
render_edit_with_errors
end
Expand All @@ -111,22 +93,14 @@ def edit_memo
end

def update_memo
if @store_credit.update(memo: permitted_store_credit_params[:memo])
if @store_credit.update(memo: permitted_resource_params[:memo])
flash[:notice] = t('.success')
else
# Memo update failures are nearly impossible to trigger due to lack of validation.
flash[:error] = t('.failure')
end

respond_to do |format|
format.html do
redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
redirect_to after_update_path, status: :see_other
end

def edit_validity
Expand All @@ -153,21 +127,15 @@ def invalidate
flash[:error] = t('.failure')
end

respond_to do |format|
format.html do
redirect_to solidus_admin.user_store_credit_path(@user, @store_credit), status: :see_other
end

format.turbo_stream do
render turbo_stream: '<turbo-stream action="refresh" />'
end
end
redirect_to after_update_path, status: :see_other
end

private

def resource_class = Spree::StoreCredit

def set_store_credit
@store_credit = Spree::StoreCredit.find(params[:id])
@store_credit ||= Spree::StoreCredit.find(params[:id])
end

def set_user
Expand All @@ -186,7 +154,7 @@ def set_store_credit_events
@store_credit_events = @store_credit.store_credit_events.chronological
end

def permitted_store_credit_params
def permitted_resource_params
permitted_params = [:amount, :currency, :category_id, :memo]
permitted_params << :category_id if action_name.to_sym == :create
permitted_params << :store_credit_reason_id if [:update_amount, :invalidate].include?(action_name.to_sym)
Expand All @@ -197,13 +165,18 @@ def permitted_store_credit_params
def render_new_with_errors
set_store_credit_categories

page_component = component("users/store_credits/new").new(
user: @user,
store_credit: @store_credit,
categories: @store_credit_categories
)

respond_to do |format|
format.html do
render component("users/store_credits/new").new(
user: @user,
store_credit: @store_credit,
categories: @store_credit_categories
),
render page_component, status: :unprocessable_entity
end
format.turbo_stream do
render turbo_stream: turbo_stream.replace(:resource_modal, page_component),
status: :unprocessable_entity
end
end
Expand All @@ -218,30 +191,35 @@ def render_edit_with_errors
"edit_amount"
end

page_component = component("users/store_credits/#{template}").new(
user: @user,
store_credit: @store_credit,
events: @store_credit_events,
reasons: @store_credit_reasons
)

respond_to do |format|
format.html do
render component("users/store_credits/#{template}").new(
user: @user,
store_credit: @store_credit,
events: @store_credit_events,
reasons: @store_credit_reasons
),
render page_component, status: :unprocessable_entity
end
format.turbo_stream do
render turbo_stream: turbo_stream.replace(:resource_modal, page_component),
status: :unprocessable_entity
end
end
end

def ensure_amount
if permitted_store_credit_params[:amount].blank?
@store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_store_credit_params[:amount])
if permitted_resource_params[:amount].blank?
@store_credit.errors.add(:amount, :greater_than, count: 0, value: permitted_resource_params[:amount])
yield if block_given? # Block is for error template rendering on a per-action basis so this can be re-used.
return false
end
true
end

def ensure_store_credit_reason
@store_credit_reason = Spree::StoreCreditReason.find_by(id: permitted_store_credit_params[:store_credit_reason_id])
@store_credit_reason = Spree::StoreCreditReason.find_by(id: permitted_resource_params[:store_credit_reason_id])

if @store_credit_reason.blank?
@store_credit.errors.add(:store_credit_reason_id, "Store Credit reason must be provided")
Expand All @@ -252,7 +230,7 @@ def ensure_store_credit_reason
end

def ensure_store_credit_category
@store_credit_category = Spree::StoreCreditCategory.find_by(id: permitted_store_credit_params[:category_id])
@store_credit_category = Spree::StoreCreditCategory.find_by(id: permitted_resource_params[:category_id])

if @store_credit_category.blank?
@store_credit.errors.add(:category_id, "Store Credit category must be provided")
Expand All @@ -261,5 +239,13 @@ def ensure_store_credit_category
end
true
end

def after_create_path
solidus_admin.user_store_credits_path(@user, **search_filter_params)
end

def after_update_path
solidus_admin.user_store_credit_path(@user, @store_credit, **search_filter_params)
end
end
end

0 comments on commit fba7722

Please sign in to comment.