MC/website/web/item-dashboard.mjs
2026-11-27 23:43:05 +00:00

18 lines
4 KiB
JavaScript

import {itemIdentity,itemLoreModel,itemWords} from './item-lore-model.mjs';
import {itemLoreCard,itemLoreStyles} from './item-lore-view.mjs';
const el=(tag,text,className)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;if(className)n.className=className;return n;};
export async function mountItemDashboard(host,{search=location.search,fetcher=fetch}={}){
itemLoreStyles();const query=new URLSearchParams(search),kind=query.get('kind'),id=query.get('id'),identity=itemIdentity(kind==='kit'?{type:'kit',listingId:id}:{type:'item',id});
host.replaceChildren(el('p','Opening the Imperial item register…'));if(!identity||!['item','kit'].includes(kind)){host.replaceChildren(el('h1','Item not found'),el('p','Choose an item or kit from Exchange.'));return;}
try{
const response=await fetcher(kind==='kit'?'/api/kits/catalogue':'/api/exchange/listings',{cache:'no-store'});if(!response.ok)throw Error('The item register is temporarily unavailable.');const data=await response.json();if(!Array.isArray(data.listings))throw Error('The item register returned an unreadable response.');
const row=data.listings.find(r=>kind==='kit'?String(r.listingId).toLowerCase()===id.toLowerCase():String(r.id)===id);if(!row){host.replaceChildren(el('h1','Listing no longer available'),el('p','This offer may have sold or been withdrawn. Your paid purchases remain in your account receipts.'));return;}
const record={...row,type:kind},item=itemLoreModel(record),layout=el('div',undefined,'item-dashboard-layout'),details=el('section',undefined,'item-dashboard-details');host.replaceChildren();
const back=el('a','← Back to Exchange');back.href='/exchange.html?type='+(kind==='kit'?'kits':'items');host.append(back,el('p','THE IMPERIAL ITEM REGISTER','item-dashboard-eyebrow'),el('h1',item.title));
layout.append(itemLoreCard(record),details);details.append(el('h2','Current offer'));const facts=el('dl');for(const[label,value]of [['Seller',item.owner||'Not published'],['Material',item.material?itemWords(item.material):'Kit contents'],['Available',item.quantity===null?'Published kit design':String(item.quantity)],['Unit price',item.price===null?'Not published':item.price.toFixed(2)+' DM']])facts.append(el('dt',label),el('dd',value));details.append(facts);
if(kind==='kit'&&item.purchaseAvailable){const checkout=el('a','Review kit purchase →','item-dashboard-action');checkout.href='https://cp.dunerealms.com/?kit-listing='+encodeURIComponent(id)+'#realm/kits-exchange';details.append(checkout);}else if(kind==='item')details.append(el('p','Purchase in Minecraft through the existing Exchange.'),el('code','/exchange buy '+id));else details.append(el('p','Purchasing is currently unavailable for this offer.'));
const edition=el('section');edition.append(el('h2','Edition & provenance'));const metadata=el('dl');for(const[label,value]of [['Quality',item.qualityLabel],['Mark',item.markLabel],['Season',item.season||'Not registered'],['Listing reference',id],...(kind==='kit'?[['Design revision',String(row.blueprintVersion||'Not published')],['Offer revision',String(row.listingVersion||'Not published')]]:[])])metadata.append(el('dt',label),el('dd',value));edition.append(metadata);details.append(edition);
const offers=el('section');offers.append(el('h2','Available offers'));const same=kind==='item'?data.listings.filter(r=>r.material===row.material):[row];const table=el('table'),thead=el('thead'),head=el('tr');for(const title of ['Seller','Available','Unit price'])head.append(el('th',title));thead.append(head);const body=el('tbody');for(const offer of same){const m=itemLoreModel({...offer,type:kind}),tr=el('tr');tr.append(el('td',m.owner||'Not published'),el('td',m.quantity===null?'Kit':String(m.quantity)),el('td',m.price===null?'—':m.price.toFixed(2)+' DM'));body.append(tr);}table.append(thead,body);offers.append(table);details.append(offers);host.append(layout);
}catch(error){host.replaceChildren(el('h1','Item register unavailable'),el('p',error.message));}
}
if(typeof document!=='undefined'){const host=document.getElementById('item-dashboard');if(host)void mountItemDashboard(host);}