web/website/node/public/memorial.js

28 lines
7.7 KiB
JavaScript

const form=document.getElementById('condolence-form');
if(form){
const status=document.getElementById('condolence-status'),path='https://cp.dunerealms.com/api/memorials/'+encodeURIComponent(form.dataset.slug);
async function call(suffix='',body){const r=await fetch(path+suffix,{credentials:'include',cache:'no-store',...(body?{method:'POST',headers:{'Content-Type':'application/json'},body:JSON.stringify(body)}:{})});let d;try{d=await r.json();}catch{throw Error('The support service could not respond. Please try again.');}if(!r.ok)throw Error(d.message||'Please try again.');return d;}
form.onsubmit=async event=>{event.preventDefault();const button=form.querySelector('button');button.disabled=true;status.textContent='Submitting…';try{const d=await call('',Object.fromEntries(new FormData(form)));status.textContent=d.message;form.reset();}catch(e){status.textContent=e.message;}finally{button.disabled=false;}};
async function moderation(){const data=await call(),box=document.getElementById('condolence-moderation'),queue=document.getElementById('condolence-queue');box.hidden=false;queue.replaceChildren();if(!data.pending.length)queue.textContent='No messages await review.';for(const message of data.pending){const card=document.createElement('article'),name=document.createElement('strong'),text=document.createElement('p');card.className='condolence';name.textContent=message.username;text.textContent=message.body;card.append(name,text);for(const[state,label]of[['approved','Publish condolence'],['denied','Decline']]){const b=document.createElement('button');b.type='button';b.textContent=label;b.onclick=async()=>{b.disabled=true;try{await call('/decision',{id:message.id,version:message.version,status:state});await moderation();}catch(e){status.textContent=e.message;b.disabled=false;}};card.append(b);}queue.append(card);}}
async function session(){try{const user=await call('/session'),link=document.getElementById('memorial-signin');if(link)link.hidden=true;status.textContent='Signed in as '+user.username+'. Your message will be reviewed before publication.';if(user.canModerate)await moderation();}catch(e){status.textContent=e.message;}}
session();
}
const memberRoot=document.getElementById('member-profile');
if(memberRoot){
const status=document.getElementById('member-profile-status'),login=document.getElementById('member-profile-signin'),boxes=document.getElementById('member-profile-boxes');
const el=(tag,text)=>{const n=document.createElement(tag);if(text!==undefined)n.textContent=text;return n;};
const box=(title,wide=false)=>{const n=el('section');n.className='memorial-box member-profile-box'+(wide?' member-profile-wide':'');n.append(el('h2',title));boxes.append(n);return n;};
const date=value=>value&&Number.isFinite(Number(value))?new Date(Number(value)).toLocaleDateString(undefined,{year:'numeric',month:'long',day:'numeric'}):'Not shared';
const link=(text,url)=>{const a=el('a',text);a.href=url;a.className='member-profile-link';return a;};
const details=(root,rows)=>{const table=el('table'),caption=el('caption',(root.querySelector('h2')?.textContent||'Profile')+' details'),body=el('tbody');table.className='member-profile-table';caption.className='member-profile-sr';for(const[label,value]of rows){const tr=el('tr'),th=el('th',label);th.scope='row';tr.append(th,el('td',String(value??'Not shared')));body.append(tr);}table.append(caption,body);root.append(table);};
async function loadMember(){try{const r=await fetch('https://cp.dunerealms.com/api/people/'+encodeURIComponent(memberRoot.dataset.username),{credentials:'include',cache:'no-store'}),data=await r.json();if(!r.ok)throw Error(data.message||'Profile unavailable.');const p=data.profile;const hero=document.querySelector('.memorial-hero');if(hero){hero.classList.add('people-hero');if(p.personalSeal){const img=el('img');img.className='people-personal-seal';img.src=p.personalSeal.url;img.alt=p.username+' personal seal';img.width=img.height=180;hero.append(img);}if(p.showcase){const note=el('p','Official account · Example account — community and supporter seals are display examples, not earned awards or purchases.');note.className='people-showcase';hero.append(note);}}boxes.replaceChildren();boxes.hidden=false;login.hidden=true;status.textContent='Public community information · private messages and support cases stay private.';
const about=box('About '+p.displayName,true),portrait=el('span');portrait.className='member-profile-avatar'+(p.avatarMode==='skin'?' member-profile-skin':'');portrait.setAttribute('role','img');portrait.setAttribute('aria-label',p.username+' avatar');for(const layer of(p.avatarMode==='skin'?['face','hat']:['image'])){const image=el('img');image.className='member-profile-avatar-'+layer;image.src=p.avatar;image.alt='';image.width=image.height=96;portrait.append(image);}about.append(portrait,el('p',p.bio||'This member has not added an introduction yet.'));if(p.publicInfo)about.append(el('p',p.publicInfo));details(about,[['Community member since',date(p.joinedAt)]]);const social=el('nav');social.className='member-profile-socials';social.setAttribute('aria-label','Public social links');for(const[name,url]of Object.entries(p.socials||{})){const a=link(name==='dunehub'?'DuneHub':name,url);a.target='_blank';a.rel='noopener noreferrer nofollow ugc';social.append(a);}about.append(social);
const category=m=>m.key.startsWith('team-')||m.url.includes('/personal-department-')?'Awards':m.group==='Community'?'Community':(m.key==='personal-role'&&!m.url.includes('/rank-network-staff-'))?'Personal':'Official';
for(const name of ['Personal','Community','Official','Awards']){const medals=box(name,true);medals.classList.add('member-medals-center');medals.dataset.sealCategory=name.toLowerCase();const items=p.medals.filter(m=>category(m)===name);if(!items.length)medals.append(el('p','No '+name.toLowerCase()+' seals are currently assigned.'));const grid=el('div');grid.className='member-medal-grid';for(const m of items){const figure=el('figure'),img=el('img');img.src=m.url;img.alt=m.label;img.title=m.label;img.width=img.height=120;img.loading='lazy';figure.append(img,el('figcaption',m.label));grid.append(figure);}medals.append(grid);}
const forum=box('Forum profile');if(p.forum.status==='linked'){details(forum,[['Forum name',p.forum.username],['Title',p.forum.title||'Community member'],['Joined',date(p.forum.joinedAt)],['Posts',p.forum.posts],['Threads',p.forum.threads],['Reputation',p.forum.reputation],['Last visit',date(p.forum.lastVisit)]]);forum.append(link('Open forum profile',p.forum.url));}else forum.append(el('p',p.forum.status==='not-linked'?'No verified forum profile is linked yet.':'Forum profile information is temporarily unavailable.'));
const support=box('Support statistics');if(p.support.status==='available'){details(support,[['Verified tickets closed',p.support.closed]]);support.append(el('p','Public staff service statistics. Ticket contents and customer identities are never shown here.'));}else support.append(el('p',p.support.status==='not-staff'?'No public staff ticket statistics for this member.':'Verified ticket statistics are temporarily unavailable.'));
const clan=box('Clan');if(p.clan.status==='member'){details(clan,[['Clan',p.clan.name],['Tag',p.clan.tag],['Role',p.clan.role],['Members',p.clan.members],['Joined',date(p.clan.joinedAt)]]);if(p.clan.description)clan.append(el('p',p.clan.description));clan.append(link('Visit the Clan center','https://clans.dunerealms.com/'));}else clan.append(el('p',p.clan.status==='none'?'This member is not currently in a clan.':'Clan information is temporarily unavailable.'));
const quests=box('Quest statistics');quests.append(el('span','COMING LATER'),el('p','Quest statistics are being prepared. Progress and completion totals will appear here when the feature is connected.'));
}catch(e){status.textContent=e.message;boxes.hidden=true;}}
loadMember();
}