forked from unimatrix/web
16 lines
3 KiB
JavaScript
16 lines
3 KiB
JavaScript
// Settings forms for both organisation adapters; dashboards remain display-only.
|
|
export function mountOrganisationEditor(host,{kind,id,onSaved=()=>{}}){
|
|
const controller=new AbortController(),url='/api/organisations/'+kind+'/'+encodeURIComponent(id)+'/settings';let active=true;
|
|
const card=title=>[...host.querySelectorAll('.organisation-settings-card')].find(n=>n.querySelector('h3')?.textContent===title);
|
|
const request=async input=>{const response=await fetch(url,{method:input?'POST':'GET',credentials:'same-origin',cache:'no-store',headers:input?{'Content-Type':'application/json'}:undefined,body:input?JSON.stringify(input):undefined,signal:controller.signal});const data=await response.json();if(!response.ok)throw Error(data.message||'Settings unavailable.');return data;};
|
|
const field=(label,value,multiline=false)=>{const node=document.createElement('label'),input=document.createElement(multiline?'textarea':'input');node.textContent=label;input.value=value||'';input.maxLength=multiline?1000:120;node.append(input);return{node,input};};
|
|
request().then(data=>{if(!active)return;
|
|
function form(title,action,fields,revision){const box=card(title);if(!box)return;box.querySelector('.organisation-settings-status')?.remove();const form=document.createElement('form');form.className='organisation-settings-form';const status=document.createElement('p');status.setAttribute('role','status');const save=document.createElement('button');save.type='submit';save.textContent=action==='motd'?'Save message':'Save details';form.append(...fields.map(f=>f.node),save,status);let version=revision;
|
|
form.onsubmit=async e=>{e.preventDefault();save.disabled=true;status.textContent='Saving…';try{const input={action,revision:version};for(const f of fields)input[f.key]=f.input.value;const updated=await request(input);if(!active)return;version=action==='motd'?updated.motdRevision:updated.identityRevision;onSaved(updated);status.textContent='Saved.';}catch(error){if(active)status.textContent=error.message;}finally{if(active)save.disabled=false;}};box.append(form);
|
|
}
|
|
if(data.canEditIdentity){const name=field('Company name',data.name),description=field('Company description',data.description,true);name.key='name';description.key='description';form('Identity & homeworld','identity',[name,description],data.identityRevision);}
|
|
if(data.canEditMotd===false){const box=card('Message of the day');if(box){box.querySelector('.organisation-settings-status')?.remove();const note=document.createElement('p');note.textContent=data.motd||'No message has been published.';box.append(note);}return;}
|
|
const message=field('Message for your members · up to 1,000 characters',data.motd,true);message.key='message';form('Message of the day','motd',[message],data.motdRevision);
|
|
}).catch(error=>{if(!active)return;const box=card('Message of the day');if(box){box.querySelector('.organisation-settings-status')?.remove();const note=document.createElement('p');note.textContent=error.message;box.append(note);}});
|
|
return()=>{active=false;controller.abort();};
|
|
}
|

Staff