feat: add errorcontainer to move error messages to a better place

This commit is contained in:
Xylight committed 2024-09-01 14:55:10 -07:00
1 parent 60004f03a7
commit e097df959b
3 files changed
+108 -18

No files matched your search

@@ -0,0 +1,77 @@
<script lang="ts" context="module">
import { writable } from 'svelte/store'
interface PageScopedError {
scope: string
message: string
}
export let errors = writable<PageScopedError[]>([
{
scope: '/login',
message: 'AAAAAAAAAAAAAAAAA',
},
])
export function pushError(args: { scope: string; message: string }) {
errors.update((e) => [...e, args])
}
export function clearErrorScope(scope: string | null | undefined) {
errors.update((errors) => errors.filter((error) => error.scope != scope))
}
</script>
<script lang="ts">
import { Button } from 'mono-svelte'
import { ExclamationCircle, Icon, XCircle, XMark } from 'svelte-hero-icons'
import { fly, slide } from 'svelte/transition'
import { expoOut } from 'svelte/easing'
import { onDestroy } from 'svelte'
export let scope: string | undefined | null = undefined
$: scopedErrors = $errors.filter(
(e) => e.scope == scope || e.scope == 'global'
)
onDestroy(() => {
clearErrorScope(scope)
})
</script>
{#if scopedErrors.length > 0}
<div
class={$$props.class}
in:slide={{ duration: 400, easing: expoOut }}
out:slide={{ duration: 400, delay: 400, easing: expoOut }}
>
<div
class="flex flex-row p-1.5 bg-red-500/20 rounded-3xl border border-red-500/50 gap-2 items-center"
in:fly={{ y: -8, duration: 400, delay: 200, opacity: 0, easing: expoOut }}
out:fly={{ y: -8, duration: 400, opacity: 0, easing: expoOut }}
>
<div class="rounded-full p-0.5 bg-red-400">
<Icon
src={XMark}
class="text-red-500 dark:text-red-400 p-px rounded-full bg-white dark:bg-zinc-950"
size="20"
micro
/>
</div>
<div class="flex flex-col text-sm">
{#each scopedErrors as error}
{error.message}
{/each}
</div>
<Button
on:click={() => clearErrorScope(scope)}
color="tertiary"
size="square-sm"
class="ml-auto"
>
<Icon src={XMark} size="16" micro />
</Button>
</div>
</div>
{/if}
@@ -26,6 +26,10 @@
import { slide } from 'svelte/transition' import { slide } from 'svelte/transition'
import { feature } from '$lib/version' import { feature } from '$lib/version'
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
import ErrorContainer, {
pushError,
} from '$lib/components/error/ErrorContainer.svelte'
import { errorMessage } from '$lib/lemmy/error'
export let edit = false export let edit = false
@@ -99,9 +103,9 @@
async function submit() { async function submit() {
if (!data.community && !edit) { if (!data.community && !edit) {
toast({ pushError({
type: 'warning', message: $t('toast.needCommunity'),
content: $t('toast.needCommunity'), scope: 'post-form',
}) })
return return
} }
@@ -110,9 +114,9 @@
try { try {
new URL(data.url) new URL(data.url)
} catch (err) { } catch (err) {
toast({ pushError({
content: $t('toast.invalidURL'), message: $t('toast.invalidURL'),
type: 'warning', scope: 'post-form',
}) })
return return
} }
@@ -162,7 +166,7 @@
dispatcher('submit', post.post_view) dispatcher('submit', post.post_view)
} }
} catch (err) { } catch (err) {
toast({ content: err as any, type: 'error' }) pushError({ message: errorMessage(err as any), scope: 'post-form' })
data.loading = false data.loading = false
} }
} }
@@ -194,8 +198,9 @@
duration: 15 * 1000, duration: 15 * 1000,
}) })
} catch (e) { } catch (e) {
toast({ pushError({
content: $t('toast.failGenerateTitle'), message: $t('toast.failGenerateTitle'),
scope: 'post-form',
}) })
} }
generation.loading = false generation.loading = false
@@ -241,6 +246,7 @@
{edit ? $t('form.post.edit') : $t('form.post.create')} {edit ? $t('form.post.edit') : $t('form.post.create')}
</Header> </Header>
</slot> </slot>
<ErrorContainer scope="post-form" />
{#if !edit && data} {#if !edit && data}
{#if !data.community} {#if !data.community}
<ObjectAutocomplete <ObjectAutocomplete
+16 -9
View File
@@ -21,6 +21,11 @@
import { t } from '$lib/translations' import { t } from '$lib/translations'
import Header from '$lib/components/ui/layout/pages/Header.svelte' import Header from '$lib/components/ui/layout/pages/Header.svelte'
import { errorMessage } from '$lib/lemmy/error' import { errorMessage } from '$lib/lemmy/error'
import ErrorContainer, {
clearErrorScope,
pushError,
} from '$lib/components/error/ErrorContainer.svelte'
import { page } from '$app/stores'
let data = { let data = {
instance: DEFAULT_INSTANCE_URL, instance: DEFAULT_INSTANCE_URL,
@@ -33,6 +38,7 @@
async function logIn() { async function logIn() {
data.loading = true data.loading = true
clearErrorScope($page.route.id)
try { try {
data.instance = data.instance.trim() data.instance = data.instance.trim()
@@ -57,8 +63,8 @@
throw new Error('Invalid credentials') throw new Error('Invalid credentials')
} }
} catch (error) { } catch (error) {
toast({ pushError({
content: message:
JSON.parse((error as any)?.body?.message ?? '{}')?.error == JSON.parse((error as any)?.body?.message ?? '{}')?.error ==
'incorrect_login' 'incorrect_login'
? errorMessage( ? errorMessage(
@@ -68,7 +74,7 @@
: `_${data.attempts + 1}`) : `_${data.attempts + 1}`)
) )
: errorMessage(error), : errorMessage(error),
type: 'error', scope: $page.route.id!,
}) })
data.attempts++ data.attempts++
} }
@@ -82,7 +88,7 @@
<div class="max-w-xl w-full mx-auto h-max my-auto"> <div class="max-w-xl w-full mx-auto h-max my-auto">
<form on:submit|preventDefault={logIn} class="flex flex-col gap-5"> <form on:submit|preventDefault={logIn} class="flex flex-col gap-5">
<div class="flex flex-col gap-2"> <div class="flex flex-col">
<Header>{$t('account.login')}</Header> <Header>{$t('account.login')}</Header>
{#if $site && mayBeIncompatible(MINIMUM_VERSION, $site.version.replace('v', ''))} {#if $site && mayBeIncompatible(MINIMUM_VERSION, $site.version.replace('v', ''))}
<Note> <Note>
@@ -92,6 +98,7 @@
})} })}
</Note> </Note>
{/if} {/if}
<ErrorContainer class="pt-2" scope={$page.route.id} />
</div> </div>
<div class="flex flex-row w-full items-center gap-2"> <div class="flex flex-row w-full items-center gap-2">
<TextInput <TextInput
@@ -147,18 +154,18 @@
> >
{$t('account.login')} {$t('account.login')}
</Button> </Button>
<hr class="dark:border-zinc-700" /> <hr class="border-slate-200 dark:border-zinc-800" />
<div class="flex flex-row items-center gap-2"> <div class="flex flex-row items-center gap-2 overflow-auto *:flex-shrink-0">
<Button color="tertiary" href="/signup"> <Button rounding="pill" color="ghost" href="/signup">
<Icon src={Identification} mini size="16" /> <Icon src={Identification} mini size="16" />
{$t('account.signup')} {$t('account.signup')}
</Button> </Button>
<Button color="tertiary" href="/login_reset"> <Button rounding="pill" color="ghost" href="/login_reset">
<Icon src={QuestionMarkCircle} mini size="16" /> <Icon src={QuestionMarkCircle} mini size="16" />
{$t('form.forgotpassword')} {$t('form.forgotpassword')}
</Button> </Button>
{#if !LINKED_INSTANCE_URL} {#if !LINKED_INSTANCE_URL}
<Button color="tertiary" href="/login/guest"> <Button rounding="pill" color="ghost" href="/login/guest">
<Icon src={UserCircle} mini size="16" /> <Icon src={UserCircle} mini size="16" />
{$t('account.guest')} {$t('account.guest')}
</Button> </Button>