From a31039c4c8cea2d5c4f574a7f1dee49d5d907ca0 Mon Sep 17 00:00:00 2001 From: Matchu Date: Sat, 11 Nov 2023 08:41:31 -0800 Subject: [PATCH] Load item page restricted zones data from Rails app, not impress-2020 Just moving more stuff over! I modernized Item's `as_json` method while I was here. (Note that I removed the NC/own/want fields, because I think the only other place this method is still called from is the quick-add feature on the closet lists page, and I think it doesn't use these fields to do anything: updating the page is basically a full-page reload, done sneakily.) --- .../item_appearances_controller.rb | 4 +- .../wardrobe-2020/ItemPageOutfitPreview.js | 9 ++--- app/javascript/wardrobe-2020/loaders/items.js | 37 ++++++++++--------- app/models/item.rb | 25 ++----------- 4 files changed, 30 insertions(+), 45 deletions(-) diff --git a/app/controllers/item_appearances_controller.rb b/app/controllers/item_appearances_controller.rb index be8edabb..2c72f9fc 100644 --- a/app/controllers/item_appearances_controller.rb +++ b/app/controllers/item_appearances_controller.rb @@ -1,6 +1,8 @@ class ItemAppearancesController < ApplicationController def index @item = Item.find(params[:item_id]) - render json: @item.appearances + render json: @item.as_json( + only: [:id], methods: [:appearances, :restricted_zones] + ) end end diff --git a/app/javascript/wardrobe-2020/ItemPageOutfitPreview.js b/app/javascript/wardrobe-2020/ItemPageOutfitPreview.js index e85b55ab..ffe6ea25 100644 --- a/app/javascript/wardrobe-2020/ItemPageOutfitPreview.js +++ b/app/javascript/wardrobe-2020/ItemPageOutfitPreview.js @@ -105,7 +105,8 @@ function ItemPageOutfitPreview({ itemId }) { loading: loadingAppearances, error: errorAppearances, } = useItemAppearances(itemId); - const itemAppearances = itemAppearancesData || []; + const itemAppearances = itemAppearancesData?.appearances ?? []; + const restrictedZones = itemAppearancesData?.restrictedZones ?? []; // Start by loading the "canonical" pet and item appearance for the outfit // preview. We'll use this to initialize both the preview and the picker. @@ -132,10 +133,6 @@ function ItemPageOutfitPreview({ itemId }) { item(id: $itemId) { id name - restrictedZones { - id - label - } canonicalAppearance( preferredSpeciesId: $preferredSpeciesId preferredColorId: $preferredColorId @@ -387,7 +384,7 @@ function ItemPageOutfitPreview({ itemId }) { {itemAppearances.length > 0 && ( )} diff --git a/app/javascript/wardrobe-2020/loaders/items.js b/app/javascript/wardrobe-2020/loaders/items.js index fb12f70e..1387e39b 100644 --- a/app/javascript/wardrobe-2020/loaders/items.js +++ b/app/javascript/wardrobe-2020/loaders/items.js @@ -4,11 +4,11 @@ export function useItemAppearances(id, options = {}) { return useQuery({ ...options, queryKey: ["items", String(id)], - queryFn: () => loadItemAppearances(id), + queryFn: () => loadItemAppearancesData(id), }); } -async function loadItemAppearances(id) { +async function loadItemAppearancesData(id) { const res = await fetch(`/items/${encodeURIComponent(id)}/appearances.json`); if (!res.ok) { @@ -17,24 +17,27 @@ async function loadItemAppearances(id) { ); } - return res.json().then(normalizeItemAppearances); + return res.json().then(normalizeItemAppearancesData); } -function normalizeItemAppearances(appearances) { - return appearances.map((appearance) => ({ - body: normalizeBody(appearance.body), - swfAssets: appearance.swf_assets.map((asset) => ({ - id: String(asset.id), - knownGlitches: asset.known_glitches, - zone: normalizeZone(asset.zone), - restrictedZones: asset.restricted_zones.map((z) => normalizeZone(z)), - urls: { - swf: asset.urls.swf, - png: asset.urls.png, - manifest: asset.urls.manifest, - }, +function normalizeItemAppearancesData(data) { + return { + appearances: data.appearances.map((appearance) => ({ + body: normalizeBody(appearance.body), + swfAssets: appearance.swf_assets.map((asset) => ({ + id: String(asset.id), + knownGlitches: asset.known_glitches, + zone: normalizeZone(asset.zone), + restrictedZones: asset.restricted_zones.map((z) => normalizeZone(z)), + urls: { + swf: asset.urls.swf, + png: asset.urls.png, + manifest: asset.urls.manifest, + }, + })), })), - })); + restrictedZones: data.restricted_zones.map((z) => normalizeZone(z)), + }; } function normalizeBody(body) { diff --git a/app/models/item.rb b/app/models/item.rb index a6893cd4..8e8aae06 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -414,27 +414,10 @@ class Item < ApplicationRecord end def as_json(options={}) - json = { - :description => description, - :id => id, - :name => name, - :thumbnail_url => thumbnail.secure_url, - :zones_restrict => zones_restrict, - :rarity_index => rarity_index, - :nc => nc? - } - - # Set owned and wanted keys, unless explicitly told not to. (For example, - # item proxies don't want us to bother, since they'll override.) - unless options.has_key?(:include_hanger_status) - options[:include_hanger_status] = true - end - if options[:include_hanger_status] - json[:owned] = owned? - json[:wanted] = wanted? - end - - json + super({ + only: [:id, :name, :description, :thumbnail_url, :rarity_index], + methods: [:zones_restrict], + }.merge(options)) end before_create do