68 lines
6.7 KiB
JavaScript
68 lines
6.7 KiB
JavaScript
import {element as el} from './cp-v2-page-template.mjs';
|
|
|
|
export function mountItemPublication(host,{fetcher=fetch}={}){
|
|
const controller=new AbortController();
|
|
const form=el('form'),revision=el('select'),recipient=el('input'),quantity=el('input');
|
|
const status=el('p','Loading saved item revisions…','cp-v2-news-status');
|
|
status.setAttribute('role','status');
|
|
recipient.required=true;recipient.maxLength=16;recipient.pattern='[A-Za-z0-9_]{1,16}';
|
|
quantity.type='number';quantity.min=1;quantity.max=36;quantity.value=1;quantity.required=true;
|
|
const label=(text,input)=>{const node=el('label');node.append(el('span',text),input);return node;};
|
|
const gift=el('button','Create redeem key'),refresh=el('button','Refresh saved items'),command=el('input');
|
|
const market=el('form'),marketQuantity=el('input'),price=el('input'),confirmation=el('input'),publish=el('button','Post to Exchange');
|
|
marketQuantity.type='number';marketQuantity.min=1;marketQuantity.max=36;marketQuantity.value=1;marketQuantity.required=true;
|
|
price.type='number';price.min='0.01';price.max='1000000';price.step='0.01';price.required=true;
|
|
confirmation.type='checkbox';confirmation.required=true;publish.type='submit';publish.disabled=true;
|
|
gift.type='submit';gift.disabled=true;refresh.type='button';
|
|
command.readOnly=true;command.hidden=true;command.setAttribute('aria-label','Minecraft redemption command');
|
|
const withdrawConfirm=el('input');withdrawConfirm.type='checkbox';const withdraw=el('button','Withdraw official listing');withdraw.type='button';const withdrawBox=el('div');withdrawBox.append(label('Withdraw this listing; existing paid deliveries remain owed.',withdrawConfirm),withdraw);
|
|
const controls=el('div',undefined,'cp-v2-company-actions');
|
|
controls.append(gift,refresh);
|
|
form.append(label('Minecraft player',recipient),label('Quantity',quantity),controls);
|
|
market.append(label('Stock',marketQuantity),label('Unit price in DM',price),label('Confirm that proceeds go to In-game Bank Treasury',confirmation),publish);
|
|
host.append(el('h4','Item publication'),label('Saved item revision',revision),el('h5','Gift in Minecraft'),el('p','Give the redeem command to the named player.'),form,command,
|
|
el('h5','Post to Exchange'),el('p','Origin world · J.A.R.V.I.S. HQ'),market,withdrawBox,status);
|
|
let rows=[],busy=false,publication=null;
|
|
function update(){gift.disabled=busy||!rows.length;const row=rows[Number(revision.value)],sealed=row?.state==='published',existing=row?.publication;withdrawBox.hidden=!sealed;withdraw.disabled=busy||!withdrawConfirm.checked;publish.textContent=existing?.state==='unsealed'?'Connect existing official listing':'Post to Exchange';if(existing?.state==='unsealed'){marketQuantity.value=existing.quantity;price.value=(existing.priceMinor/100).toFixed(2);}publish.disabled=busy||!rows.length||sealed;marketQuantity.disabled=price.disabled=busy||sealed||existing?.state==='unsealed';confirmation.disabled=busy||sealed;refresh.disabled=busy;}
|
|
async function load(){
|
|
busy=true;update();
|
|
try{
|
|
const response=await fetcher('/api/items/designs',{cache:'no-store',credentials:'same-origin',signal:controller.signal});
|
|
const body=await response.json();if(!response.ok)throw Error(body.message||'Item revisions are unavailable.');
|
|
rows=body.definitions;
|
|
revision.replaceChildren(...rows.map((row,index)=>{const option=el('option',row.design.name+' · revision '+row.revision);option.value=String(index);return option;}));
|
|
status.textContent=rows.length?'Select a saved revision.':'Save an item in the editor first.';
|
|
}catch(error){if(!controller.signal.aborted)status.textContent=error.message;}
|
|
finally{busy=false;update();}
|
|
}
|
|
withdrawConfirm.onchange=update;withdraw.onclick=async()=>{const row=rows[Number(revision.value)];if(busy||!withdrawConfirm.checked||!row?.publication)return;busy=true;update();try{const response=await fetcher('/api/items/designs',{method:'POST',credentials:'same-origin',headers:{'content-type':'application/json'},signal:controller.signal,body:JSON.stringify({action:'withdraw-publication',keyId:row.keyId,listingId:row.publication.listingId,confirmed:true})}),data=await response.json();if(!response.ok)throw Error(data.message||'Withdrawal was not confirmed.');withdrawConfirm.checked=false;await load();status.textContent='Official listing withdrawn. Save a new revision before publishing again.';}catch(error){if(!controller.signal.aborted)status.textContent=error.message;}finally{busy=false;update();}};revision.onchange=update;refresh.onclick=()=>void load();
|
|
form.onsubmit=async event=>{
|
|
event.preventDefault();const selected=rows[Number(revision.value)];if(!selected||busy)return;
|
|
busy=true;update();command.hidden=true;command.value='';
|
|
try{
|
|
const response=await fetcher('/api/items/designs',{method:'POST',credentials:'same-origin',headers:{'content-type':'application/json'},signal:controller.signal,
|
|
body:JSON.stringify({action:'gift',id:selected.id,revision:selected.revision,recipient:recipient.value.trim(),quantity:Number(quantity.value)})});
|
|
const body=await response.json();if(!response.ok)throw Error(body.message||'Gift key was not created.');
|
|
command.value=body.command;command.hidden=false;command.select();
|
|
status.textContent='Key ready for '+body.recipient+'. It expires if unused after seven days.';
|
|
}catch(error){if(!controller.signal.aborted)status.textContent=error.message;}
|
|
finally{busy=false;update();}
|
|
};
|
|
market.onsubmit=async event=>{
|
|
event.preventDefault();const selected=rows[Number(revision.value)];if(!selected||busy||selected.state==='published'||!confirmation.checked)return;
|
|
const existing=selected.publication?.state==='unsealed';const input=existing?{action:'seal-publication',keyId:selected.keyId,listingId:selected.publication.listingId,confirmed:true}:{action:'publish',id:selected.id,revision:selected.revision,quantity:Number(marketQuantity.value),priceMinor:Math.round(Number(price.value)*100),confirmed:true};
|
|
const signature=JSON.stringify(input);
|
|
if(publication?.signature!==signature)publication={signature,requestId:crypto.randomUUID()};
|
|
busy=true;update();
|
|
try{
|
|
const response=await fetcher('/api/items/designs',{method:'POST',credentials:'same-origin',headers:{'content-type':'application/json'},signal:controller.signal,
|
|
body:JSON.stringify(existing?input:{...input,requestId:publication.requestId})});
|
|
const body=await response.json();if(!response.ok)throw Error(body.message||'Exchange posting could not be confirmed.');
|
|
status.textContent='Exchange listing '+body.listingId+' is published.';
|
|
confirmation.checked=false;publication=null;await load();
|
|
}catch(error){if(!controller.signal.aborted)status.textContent=error.message;}
|
|
finally{busy=false;update();}
|
|
};
|
|
void load();
|
|
return ()=>controller.abort();
|
|
}
|

Staff