Oops, load the data for the bulk item quantity form on the trades page!

This commit is contained in:
Emi Matchu 2024-01-21 06:42:24 -08:00
parent 8e3d2b994f
commit 5d577db649
7 changed files with 43 additions and 26 deletions

View file

@ -1,14 +1,18 @@
class ItemTradesController < ApplicationController class ItemTradesController < ApplicationController
def index def index
@item = Item.find params[:item_id] @item = Item.find params[:item_id]
@type = type_from_params @type = type_from_params
@item_trades = @item.closet_hangers.trading.includes(:user, :list). @item_trades = @item.closet_hangers.trading.includes(:user, :list).
user_is_active.order('users.last_trade_activity_at DESC').to_trades user_is_active.order('users.last_trade_activity_at DESC').to_trades
@trades = @item_trades[@type] @trades = @item_trades[@type]
if user_signed_in?
@current_user_lists = current_user.closet_lists.alphabetical.
group_by_owned
@current_user_quantities = current_user.item_quantities_for(@item)
end
render layout: 'items' render layout: 'items'
end end

View file

@ -63,23 +63,10 @@ class ItemsController < ApplicationController
@contributors_with_counts = @item.contributors_with_counts @contributors_with_counts = @item.contributors_with_counts
if user_signed_in? if user_signed_in?
# Empty arrays are important so that we can loop over this and still @current_user_lists = current_user.closet_lists.alphabetical.
# show the generic no-list case group_by_owned
@current_user_lists = {true => [], false => []} @current_user_quantities = current_user.item_quantities_for(@item)
current_user.closet_lists.alphabetical.each do |list|
@current_user_lists[list.hangers_owned] << list
end end
@current_user_quantities = Hash.new(0) # default is zero
hangers = current_user.closet_hangers.where(item_id: @item.id).
select([:owned, :list_id, :quantity])
hangers.each do |hanger|
key = hanger.list_id || hanger.owned
@current_user_quantities[key] = hanger.quantity
end
end
end end
format.gif do format.gif do

View file

@ -87,6 +87,13 @@ class ClosetList < ApplicationRecord
end end
end end
def self.group_by_owned
h = all.group_by(&:hangers_owned?)
h[true] ||= []
h[false] ||= []
h
end
include VisibilityMethods include VisibilityMethods
class Null class Null

View file

@ -175,6 +175,18 @@ class User < ApplicationRecord
contact_neopets_connection.try(:neopets_username) contact_neopets_connection.try(:neopets_username)
end end
def item_quantities_for(item_id)
quantities = Hash.new(0)
hangers = closet_hangers.where(item_id: item_id).
select([:owned, :list_id, :quantity])
hangers.each do |hanger|
quantities[hanger.list_id || hanger.owned?] = hanger.quantity
end
quantities
end
def log_trade_activity def log_trade_activity
touch(:last_trade_activity_at) touch(:last_trade_activity_at)
end end

View file

@ -1,8 +1,11 @@
- title t(".title.#{@type}") - title t(".title.#{@type}")
- hide_title_header - hide_title_header
= render partial: "items/item_header", locals: {item: @item, = render partial: "items/item_header",
trades: @item_trades, current_subpage: "trades_#{@type}"} locals: {item: @item, trades: @item_trades,
current_subpage: "trades_#{@type}",
current_user_lists: @current_user_lists,
current_user_quantities: @current_user_quantities}
%h2.item-subpage-title= t(".title.#{@type}") %h2.item-subpage-title= t(".title.#{@type}")

View file

@ -1,5 +1,7 @@
- raise ArgumentError unless defined? item - raise ArgumentError unless defined? item
- raise ArgumentError unless defined? trades - raise ArgumentError unless defined? trades
- raise ArgumentError unless defined? current_user_lists
- raise ArgumentError unless defined? current_user_quantities
- raise ArgumentError unless defined? current_subpage - raise ArgumentError unless defined? current_subpage
%header.item-header %header.item-header
@ -38,22 +40,22 @@
= t 'items.show.closet_hangers.header_html', = t 'items.show.closet_hangers.header_html',
user_items_link: link_to(t('your_items'), user_items_link: link_to(t('your_items'),
user_closet_hangers_path(current_user)) user_closet_hangers_path(current_user))
= form_tag update_quantities_user_item_closet_hangers_path(:user_id => current_user, :item_id => @item), :method => :put do = form_tag update_quantities_user_item_closet_hangers_path(:user_id => current_user, :item_id => item), :method => :put do
.closet-hangers-ownership-groups .closet-hangers-ownership-groups
- @current_user_lists.each do |owned, lists| - current_user_lists.each do |owned, lists|
%div %div
%h4= closet_lists_group_name(:you, owned) %h4= closet_lists_group_name(:you, owned)
%ul %ul
- lists.each_with_index do |list, index| - lists.each_with_index do |list, index|
%li %li
= number_field_tag "quantity[#{list.id}]", = number_field_tag "quantity[#{list.id}]",
@current_user_quantities[list.id], min: 0, current_user_quantities[list.id], min: 0,
autofocus: owned && index == 0 autofocus: owned && index == 0
= label_tag "quantity[#{list.id}]", list.name = label_tag "quantity[#{list.id}]", list.name
%li %li
= number_field_tag "quantity[#{owned}]", = number_field_tag "quantity[#{owned}]",
@current_user_quantities[owned], min: 0, current_user_quantities[owned], min: 0,
autofocus: owned && lists.empty? autofocus: owned && lists.empty?
- unless lists.empty? - unless lists.empty?
@ -65,7 +67,7 @@
t('items.show.closet_hangers.quantity_label') t('items.show.closet_hangers.quantity_label')
= submit_tag t('items.show.closet_hangers.submit') = submit_tag t('items.show.closet_hangers.submit')
%p.item-description= @item.description %p.item-description= item.description
%nav.item-subpages-nav %nav.item-subpages-nav
= link_to t('items.show.subpages_nav.preview'), item, = link_to t('items.show.subpages_nav.preview'), item,

View file

@ -2,7 +2,9 @@
- canonical_path @item - canonical_path @item
= render partial: "item_header", = render partial: "item_header",
locals: {item: @item, trades: @trades, current_subpage: "preview"} locals: {item: @item, trades: @trades, current_subpage: "preview",
current_user_lists: @current_user_lists,
current_user_quantities: @current_user_quantities}
#outfit-preview-root{'data-item-id': @item.id} #outfit-preview-root{'data-item-id': @item.id}