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.)
This commit is contained in:
Emi Matchu 2023-11-11 08:41:31 -08:00
parent c6cb61ef38
commit a31039c4c8
4 changed files with 30 additions and 45 deletions

View file

@ -1,6 +1,8 @@
class ItemAppearancesController < ApplicationController class ItemAppearancesController < ApplicationController
def index def index
@item = Item.find(params[:item_id]) @item = Item.find(params[:item_id])
render json: @item.appearances render json: @item.as_json(
only: [:id], methods: [:appearances, :restricted_zones]
)
end end
end end

View file

@ -105,7 +105,8 @@ function ItemPageOutfitPreview({ itemId }) {
loading: loadingAppearances, loading: loadingAppearances,
error: errorAppearances, error: errorAppearances,
} = useItemAppearances(itemId); } = 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 // 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. // preview. We'll use this to initialize both the preview and the picker.
@ -132,10 +133,6 @@ function ItemPageOutfitPreview({ itemId }) {
item(id: $itemId) { item(id: $itemId) {
id id
name name
restrictedZones {
id
label
}
canonicalAppearance( canonicalAppearance(
preferredSpeciesId: $preferredSpeciesId preferredSpeciesId: $preferredSpeciesId
preferredColorId: $preferredColorId preferredColorId: $preferredColorId
@ -387,7 +384,7 @@ function ItemPageOutfitPreview({ itemId }) {
{itemAppearances.length > 0 && ( {itemAppearances.length > 0 && (
<ItemZonesInfo <ItemZonesInfo
itemAppearances={itemAppearances} itemAppearances={itemAppearances}
restrictedZones={data?.item?.restrictedZones || []} restrictedZones={restrictedZones}
/> />
)} )}
<Box width="6" /> <Box width="6" />

View file

@ -4,11 +4,11 @@ export function useItemAppearances(id, options = {}) {
return useQuery({ return useQuery({
...options, ...options,
queryKey: ["items", String(id)], 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`); const res = await fetch(`/items/${encodeURIComponent(id)}/appearances.json`);
if (!res.ok) { if (!res.ok) {
@ -17,11 +17,12 @@ async function loadItemAppearances(id) {
); );
} }
return res.json().then(normalizeItemAppearances); return res.json().then(normalizeItemAppearancesData);
} }
function normalizeItemAppearances(appearances) { function normalizeItemAppearancesData(data) {
return appearances.map((appearance) => ({ return {
appearances: data.appearances.map((appearance) => ({
body: normalizeBody(appearance.body), body: normalizeBody(appearance.body),
swfAssets: appearance.swf_assets.map((asset) => ({ swfAssets: appearance.swf_assets.map((asset) => ({
id: String(asset.id), id: String(asset.id),
@ -34,7 +35,9 @@ function normalizeItemAppearances(appearances) {
manifest: asset.urls.manifest, manifest: asset.urls.manifest,
}, },
})), })),
})); })),
restrictedZones: data.restricted_zones.map((z) => normalizeZone(z)),
};
} }
function normalizeBody(body) { function normalizeBody(body) {

View file

@ -414,27 +414,10 @@ class Item < ApplicationRecord
end end
def as_json(options={}) def as_json(options={})
json = { super({
:description => description, only: [:id, :name, :description, :thumbnail_url, :rarity_index],
:id => id, methods: [:zones_restrict],
:name => name, }.merge(options))
: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
end end
before_create do before_create do