added loading indicators on forms

This commit is contained in:
James Shiffer 2020-07-06 07:50:00 -07:00
parent b0bb02a9bd
commit a9d2b49456
No known key found for this signature in database
GPG Key ID: C0DB8774A1B3BA45
2 changed files with 46 additions and 24 deletions

View File

@ -18,6 +18,7 @@
const { session } = stores(); const { session } = stores();
let editor, form, uploadForm; let editor, form, uploadForm;
let loading = false;
let title = '', category = ''; let title = '', category = '';
export let categories; export let categories;
@ -64,18 +65,25 @@
let html = editor.getHtml(true); let html = editor.getHtml(true);
let fd = new FormData(form); let fd = new FormData(form);
fd.append('html', html); fd.append('html', html);
const res = await fetch(`/cms/article`, { loading = true;
method: 'POST', try {
headers: { const res = await fetch(`/cms/article`, {
'Accept': 'application/json' method: 'POST',
}, headers: {
body: fd 'Accept': 'application/json'
}); },
const json = await res.json(); body: fd
if (res.status === 200) { });
goto(`/a/${json.slug}`); const json = await res.json();
} else { if (res.status === 200) {
alert(`Error ${res.status}: ${json.message}`); goto(`/a/${json.slug}`);
} else {
alert(`Error ${res.status}: ${json.message}`);
}
} catch (err) {
console.error(err);
} finally {
loading = false;
} }
} }
@ -164,4 +172,7 @@
</p> </p>
</form> </form>
<button on:click={submit}>Submit</button> <button on:click={submit}>Submit</button>
{#if loading}
<progress>Uploading...</progress>
{/if}
</div> </div>

View File

@ -15,23 +15,31 @@
import { goto, stores } from '@sapper/app'; import { goto, stores } from '@sapper/app';
const { session } = stores(); const { session } = stores();
let loading = false;
let uploadForm; let uploadForm;
async function upload() async function upload()
{ {
let fd = new FormData(uploadForm); let fd = new FormData(uploadForm);
const res = await fetch(`/me/avatar`, { loading = true;
method: 'POST', try {
headers: { const res = await fetch(`/me/avatar`, {
'Accept': 'application/json' method: 'POST',
}, headers: {
body: fd 'Accept': 'application/json'
}); },
const json = await res.json(); body: fd
if (res.status === 200) { });
$session.user.avatar = json.filename; const json = await res.json();
} else { if (res.status === 200) {
alert(`Error ${res.status}: ${json.message}`); $session.user.avatar = json.filename;
} else {
alert(`Error ${res.status}: ${json.message}`);
}
} catch (err) {
console.error(err);
} finally {
loading = false;
} }
} }
@ -64,6 +72,9 @@
Change your photo so you at least have a chance: Change your photo so you at least have a chance:
<input type="file" name="upload"> <input type="file" name="upload">
<button type="submit">Upload</button> <button type="submit">Upload</button>
{#if loading}
<progress>Uploading...</progress>
{/if}
</p> </p>
</form> </form>
<p>Or better yet, erase your wretched face from this site entirely: <button on:click={del}>Reset Avatar</button></p> <p>Or better yet, erase your wretched face from this site entirely: <button on:click={del}>Reset Avatar</button></p>