diff --git a/src/routes/cms/create.svelte b/src/routes/cms/create.svelte index ec162b0..2f53197 100644 --- a/src/routes/cms/create.svelte +++ b/src/routes/cms/create.svelte @@ -18,7 +18,7 @@ const { session } = stores(); let editor, form, uploadForm; - let loading = false; + let loading = false, loadingAttach = false; let title = '', category = ''; export let categories; @@ -112,28 +112,35 @@ async function upload() { let fd = new FormData(uploadForm); - const res = await fetch(`/cms/upload`, { - method: 'POST', - headers: { - 'Accept': 'application/json' - }, - body: fd - }); - const json = await res.json(); - if (res.status === 200) { - const ans = prompt('(Optional) Enter the dimensions to resize this image to (e.g. "350x150")'); - if (ans) { - const dim = ans.split('x'); - if (Number.isInteger(+dim[0]) && Number.isInteger(+dim[1])) { - editor.setHtml(editor.getHtml(true) + ``, false); + loadingAttach = true; + try { + const res = await fetch(`/cms/upload`, { + method: 'POST', + headers: { + 'Accept': 'application/json' + }, + body: fd + }); + const json = await res.json(); + if (res.status === 200) { + const ans = prompt('(Optional) Enter the dimensions to resize this image to (e.g. "350x150")'); + if (ans) { + const dim = ans.split('x'); + if (Number.isInteger(+dim[0]) && Number.isInteger(+dim[1])) { + editor.setHtml(editor.getHtml(true) + ``, false); + } else { + editor.setHtml(editor.getHtml(true) + ``, false); + } } else { editor.setHtml(editor.getHtml(true) + ``, false); } } else { - editor.setHtml(editor.getHtml(true) + ``, false); + alert(`Error ${res.status}: ${json.message}`); } - } else { - alert(`Error ${res.status}: ${json.message}`); + } catch (err) { + console.error(err); + } finally { + loadingAttach = false; } } @@ -169,6 +176,9 @@

Add Image to Article: + {#if loadingAttach} + Uploading... + {/if}