12 lines
3.5 KiB
JavaScript
12 lines
3.5 KiB
JavaScript
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
|
|
export function proclamationBox(kind,{editor=false}={}){
|
|
const names={house:'Faction',imperial:'Imperial',guild:'Spacing Guild'},seals={house:'office-house_leader.png',imperial:'emperor-v1.png',guild:'guild-envoy-v1.png'};
|
|
const root=el('article');root.className='politics-card';const art=el('img');art.src='https://www.dunerealms.com/assets/seals/'+seals[kind];art.alt='';art.width=art.height=72;root.append(art,el('h3',names[kind]+(editor?' Message settings':' Proclamations')));const status=el('p');status.role='status';const content=el('div');root.append(status,content);let page=1,requestId=null,version=0;
|
|
async function load(){const serial=++version;try{const response=await fetch('/api/politics/proclamations?kind='+kind+'&page='+page,{cache:'no-store'}),data=await response.json();if(!root.isConnected||serial!==version)return;if(!response.ok)throw Error(data.message||'Messages unavailable.');content.replaceChildren();status.textContent='';
|
|
if(!editor){if(data.post){content.append(el('h4',data.post.title));const body=el('p',data.post.body);body.className='honour-citation';content.append(body,el('small','Issued by '+data.post.author+' · '+new Date(Number(data.post.created_at)).toLocaleString('en-GB',{timeZone:'UTC'})+' GMT'));}else content.append(el('p','No proclamations have been published yet.'));const nav=el('nav');nav.className='politics-pagination';for(const [label,step]of [['Newer',-1],['Older',1]]){const b=el('button',label);b.type='button';b.disabled=step<0?page===1:page>=data.pages;b.onclick=()=>{page+=step;load();};nav.append(b);}nav.append(el('span','Page '+page+' of '+data.pages));content.append(nav);}
|
|
if(editor||kind==='guild'&&data.canPublish){if(!data.canPublish){content.append(el('p','Publishing is reserved for the current '+(kind==='house'?'faction leader':kind==='imperial'?'Emperor':'Guild Envoy')+'.'));return;}
|
|
const form=el('form');form.className='politics-charter';const field=(name,tag,max)=>{const label=el('label',name),input=el(tag);input.required=true;input.maxLength=max;label.append(input);form.append(label);return input;};const title=field('Title','input',120),body=field('Message','textarea',6000);title.minLength=3;body.minLength=10;body.rows=5;const consent=el('label');consent.className='politics-accept';const check=el('input');check.type='checkbox';check.required=true;consent.append(check,el('span','I confirm publication to this proclamation archive.'));const submit=el('button','Publish proclamation');submit.type='submit';form.append(consent,submit);content.append(form);form.onsubmit=async event=>{event.preventDefault();if(!form.reportValidity())return;submit.disabled=true;requestId||=crypto.randomUUID();try{const r=await fetch('/api/politics/proclamations?kind='+kind,{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify({requestId,title:title.value,body:body.value,confirmed:check.checked})}),v=await r.json();if(!r.ok)throw Error(v.message||'Publication failed.');requestId=null;form.reset();status.textContent='Proclamation published and archived.';}catch(error){status.textContent=error.message;}finally{submit.disabled=false;}};}
|
|
}catch(error){if(serial===version)status.textContent=error.message;}}
|
|
queueMicrotask(load);return root;
|
|
}
|
|
export function renderProclamations(){const root=el('section');root.className='politics-grid council-motds';for(const kind of ['house','imperial','guild'])root.append(proclamationBox(kind));return root;}
|

Staff