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

View file

@ -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 && (
<ItemZonesInfo
itemAppearances={itemAppearances}
restrictedZones={data?.item?.restrictedZones || []}
restrictedZones={restrictedZones}
/>
)}
<Box width="6" />

View file

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

View file

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