|
|
|
|
@ -250,13 +250,60 @@ const $switch = (template_id, data) => { |
|
|
|
|
return null; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $no_error = (data, err) => { |
|
|
|
|
if(err === null) { |
|
|
|
|
return true; |
|
|
|
|
} else { |
|
|
|
|
const target = $id('error'); |
|
|
|
|
const templ = $id('error-template'); |
|
|
|
|
$replace_with(target, $render(templ, data)); |
|
|
|
|
const $render_error = (data) => { |
|
|
|
|
const target = $id('error'); |
|
|
|
|
|
|
|
|
|
if(!target) { |
|
|
|
|
console.error("No id=error to display the error."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const templ = $id('error-template'); |
|
|
|
|
|
|
|
|
|
if(!templ) { |
|
|
|
|
console.error("template with id=error-template to display error."); |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$replace_with(target, $render(templ, data)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $has_error = (data, err) => { |
|
|
|
|
if(err === 200) { |
|
|
|
|
return false; |
|
|
|
|
} else { |
|
|
|
|
$render_error(data); |
|
|
|
|
return true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $no_error = (data, err) => { |
|
|
|
|
return !$has_error(data, err); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $redirect = (url, replace=false) => { |
|
|
|
|
window.location.replace(url); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $form_submit = async (form_id, event) => { |
|
|
|
|
event.preventDefault(); |
|
|
|
|
|
|
|
|
|
const form = event.target; |
|
|
|
|
const fd = new FormData(form); |
|
|
|
|
|
|
|
|
|
const resp = await fetch(form.action, |
|
|
|
|
{method: form.method, body: fd}); |
|
|
|
|
const data = await resp.json(); |
|
|
|
|
|
|
|
|
|
return [data, resp.status]; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const $handle_form = (form_id, cb) => { |
|
|
|
|
const submitter = async (event) => { |
|
|
|
|
const [data, status] = await $form_submit(form_id, event); |
|
|
|
|
cb(data, status); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$id('form').addEventListener('submit', submitter); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|