import { html, render, } from 'https://unpkg.com/htm@latest/preact/index.mjs?module'; import { useState, useEffect, } from 'https://unpkg.com/preact@latest/hooks/dist/hooks.module.js?module'; function SiteForm({ create = false }) { const [newSite, setNewSite] = useState({}); const [error, setError] = useState(null); const [siteKey, setSiteKey] = useState(null); const [siteUpdated, setSiteUpdated] = useState(false); const onChange = (att) => (e) => { setNewSite((prev) => ({ ...prev, [att]: e.target.value })); }; const onClick = async (e) => { e.preventDefault(); setSiteUpdated(false); setError(null); const result = await fetch( create ? '/_register/' : `/_register/${newSite.siteId}`, { method: create ? 'POST' : 'PATCH', headers: { Accept: 'application/json', 'Content-Type': 'application/json', }, body: JSON.stringify(newSite), } ); if (result.status === 400) { const { message } = await result.json(); setError(message); return; } if (result.status === 403) { const { message } = await result.json(); setError(message); return; } if (result.status === 404) { const { message } = await result.json(); setError(message); return; } if (result.status >= 300) { setError('Unknown error, try again later...'); return; } setNewSite({}); if (create) { const { key } = await result.json(); setSiteKey(key); } else { setSiteUpdated(true); } }; return html`

${create ? 'Create new site' : 'Update site'}

Only letters and '_' are accepted.

This name will appears in sent email.

All sent email for this site will have this origin.

${create && html`

This is the site owner email. Confirmation links are sent to this address.

`}
${error && html`

${error}

`} ${siteKey && html`

Success! Please, save the site encryption key: e.preventDefault()} /> this is the last opportunity to read it.

Now you must confirm the site creation before using it, you will receive an email at the 'owner' address with a confirmation link.

`} ${siteUpdated && html`

Success! Now you must confirm the site update by visiting the confirmation link we have just sent to the owner email.

`}
`; } function App() { const [settings, setSettings] = useState({}); useEffect(() => { let mounted = true; const updateRegistration = async () => { const result = await (await fetch('/site/settings')).json(); console.log(result); if (mounted) setSettings(result); }; updateRegistration(); return () => (mounted = false); }, []); return html`

Ricochet.js admin

${settings.registrationEnabled && html`
<${SiteForm} create />
<${SiteForm} />
`} ${settings.registrationEnabled === false && html`

Site registration is disabled

`}
`; } render(html`<${App} />`, document.body);