mirror of
https://github.com/BradNut/boredgame
synced 2025-09-08 17:40:22 +00:00
Fix loading shown on login, add base enhance to logout and show loading UI with toast on success.
This commit is contained in:
parent
fb565c1593
commit
3ead1dc357
4 changed files with 63 additions and 13 deletions
|
|
@ -1,10 +1,13 @@
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { enhance } from '$app/forms';
|
import { applyAction, enhance } from '$app/forms';
|
||||||
import { ListChecks, ListTodo, LogOut, User } from 'lucide-svelte';
|
import { ListChecks, ListTodo, LogOut, User } from 'lucide-svelte';
|
||||||
import { Button } from '$lib/components/ui/button';
|
import { Button } from '$lib/components/ui/button';
|
||||||
import * as DropdownMenu from "$lib/components/ui/dropdown-menu";
|
import * as DropdownMenu from "$lib/components/ui/dropdown-menu";
|
||||||
import * as Avatar from "$lib/components/ui/avatar";
|
import * as Avatar from "$lib/components/ui/avatar";
|
||||||
import Logo from '$components/logo.svelte';
|
import Logo from '$components/logo.svelte';
|
||||||
|
import { boredState } from '$lib/stores/boredState';
|
||||||
|
import { invalidateAll } from '$app/navigation';
|
||||||
|
import toast from 'svelte-french-toast';
|
||||||
|
|
||||||
export let user: any;
|
export let user: any;
|
||||||
|
|
||||||
|
|
@ -53,7 +56,24 @@
|
||||||
</DropdownMenu.Item>
|
</DropdownMenu.Item>
|
||||||
</a>
|
</a>
|
||||||
<form
|
<form
|
||||||
use:enhance
|
use:enhance={() => {
|
||||||
|
boredState.update((n) => ({ ...n, loading: true }));
|
||||||
|
return async ({ result }) => {
|
||||||
|
console.log(result);
|
||||||
|
if (result.type === 'success' || result.type === 'redirect') {
|
||||||
|
toast.success('Logged Out');
|
||||||
|
} else if (result.type === 'error') {
|
||||||
|
console.log(result);
|
||||||
|
toast.error(`Error: ${result.error.message}`);
|
||||||
|
} else {
|
||||||
|
toast.error(`Something went wrong.`);
|
||||||
|
console.log(result);
|
||||||
|
}
|
||||||
|
await invalidateAll();
|
||||||
|
await applyAction(result);
|
||||||
|
boredState.update((n) => ({ ...n, loading: true }));
|
||||||
|
};
|
||||||
|
}}
|
||||||
action="/logout"
|
action="/logout"
|
||||||
method="POST"
|
method="POST"
|
||||||
>
|
>
|
||||||
|
|
|
||||||
36
src/lib/state/loading.ts
Normal file
36
src/lib/state/loading.ts
Normal file
|
|
@ -0,0 +1,36 @@
|
||||||
|
import { writable } from 'svelte/store';
|
||||||
|
|
||||||
|
const newLoading = () => {
|
||||||
|
const { subscribe, update, set } = writable({
|
||||||
|
status: 'IDLE', // IDLE, LOADING, NAVIGATING
|
||||||
|
message: ''
|
||||||
|
});
|
||||||
|
|
||||||
|
function setNavigate(isNavigating: boolean) {
|
||||||
|
update(() => {
|
||||||
|
return {
|
||||||
|
status: isNavigating ? 'NAVIGATING' : 'IDLE',
|
||||||
|
message: ''
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
function setLoading(isLoading: boolean, message = '') {
|
||||||
|
update(() => {
|
||||||
|
return {
|
||||||
|
status: isLoading ? 'LOADING' : 'IDLE',
|
||||||
|
message: isLoading ? message : ''
|
||||||
|
};
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return {
|
||||||
|
subscribe,
|
||||||
|
update,
|
||||||
|
set,
|
||||||
|
setNavigate,
|
||||||
|
setLoading
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
export const loading = newLoading();
|
||||||
|
|
@ -10,11 +10,11 @@
|
||||||
import { Button } from '$components/ui/button';
|
import { Button } from '$components/ui/button';
|
||||||
import * as Alert from "$components/ui/alert";
|
import * as Alert from "$components/ui/alert";
|
||||||
import { boredState } from '$lib/stores/boredState.js';
|
import { boredState } from '$lib/stores/boredState.js';
|
||||||
import Portal from '$lib/Portal.svelte';
|
|
||||||
import Loading from '$components/loading.svelte';
|
|
||||||
|
|
||||||
export let data;
|
export let data;
|
||||||
const { form, errors, enhance, delayed } = superForm(data.form, {
|
const { form, errors, enhance } = superForm(data.form, {
|
||||||
|
onSubmit: () => boredState.update((n) => ({ ...n, loading: true })),
|
||||||
|
onResult: () => boredState.update((n) => ({ ...n, loading: false })),
|
||||||
flashMessage: {
|
flashMessage: {
|
||||||
module: flashModule,
|
module: flashModule,
|
||||||
onError: ({ result, message }) => {
|
onError: ({ result, message }) => {
|
||||||
|
|
@ -29,15 +29,9 @@
|
||||||
taintedMessage: null,
|
taintedMessage: null,
|
||||||
validators: signInSchema,
|
validators: signInSchema,
|
||||||
validationMethod: 'oninput',
|
validationMethod: 'oninput',
|
||||||
delayMs: 250,
|
delayMs: 0,
|
||||||
});
|
});
|
||||||
|
|
||||||
$: {
|
|
||||||
if ($delayed) {
|
|
||||||
boredState.update((n) => ({ ...n, loading: true }));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
const flash = flashModule.getFlash(page);
|
const flash = flashModule.getFlash(page);
|
||||||
|
|
||||||
$: {
|
$: {
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,6 @@ export const actions = {
|
||||||
}
|
}
|
||||||
await auth.invalidateSession(session.sessionId); // invalidate session
|
await auth.invalidateSession(session.sessionId); // invalidate session
|
||||||
locals.auth.setSession(null); // remove cookie
|
locals.auth.setSession(null); // remove cookie
|
||||||
throw redirect(302, '/login');
|
throw redirect(302, '/');
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue