mirror of
https://github.com/bartvdbraak/omnidash.git
synced 2025-04-29 16:31:21 +00:00
refactor: user profile form component
This commit is contained in:
parent
69b67301d6
commit
1d61eec4ea
2 changed files with 19 additions and 5 deletions
|
@ -2,8 +2,8 @@
|
||||||
import type { PageData } from "./$types";
|
import type { PageData } from "./$types";
|
||||||
import ProfileForm from "./profile-form.svelte";
|
import ProfileForm from "./profile-form.svelte";
|
||||||
import { Separator } from "$lib/components/ui/separator";
|
import { Separator } from "$lib/components/ui/separator";
|
||||||
|
|
||||||
export let data: PageData;
|
export let data: PageData;
|
||||||
let { user, form } = data;
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="space-y-6">
|
<div class="space-y-6">
|
||||||
|
@ -12,5 +12,5 @@
|
||||||
<p class="text-sm text-muted-foreground">This is how others will see you on the site.</p>
|
<p class="text-sm text-muted-foreground">This is how others will see you on the site.</p>
|
||||||
</div>
|
</div>
|
||||||
<Separator />
|
<Separator />
|
||||||
<ProfileForm {user} data={form} />
|
<ProfileForm user={data.user} data={data.form} />
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
<script lang="ts" context="module">
|
<script lang="ts" context="module">
|
||||||
import { z } from 'zod';
|
import { z } from 'zod';
|
||||||
export const profileFormSchema = z.object({
|
export const profileFormSchema = z.object({
|
||||||
name: z.string()
|
name: z.string().min(3).max(50)
|
||||||
});
|
});
|
||||||
export type ProfileFormSchema = typeof profileFormSchema;
|
export type ProfileFormSchema = typeof profileFormSchema;
|
||||||
</script>
|
</script>
|
||||||
|
@ -16,12 +16,26 @@
|
||||||
import { browser, dev } from '$app/environment';
|
import { browser, dev } from '$app/environment';
|
||||||
import { PUBLIC_DEBUG_FORMS } from '$env/static/public';
|
import { PUBLIC_DEBUG_FORMS } from '$env/static/public';
|
||||||
import type { LayoutData } from '../$types';
|
import type { LayoutData } from '../$types';
|
||||||
|
import { toast } from 'svelte-sonner';
|
||||||
|
|
||||||
export let user: LayoutData['user'];
|
export let user: LayoutData['user'];
|
||||||
export let data: SuperValidated<Infer<ProfileFormSchema>>;
|
export let data: SuperValidated<Infer<ProfileFormSchema>>;
|
||||||
|
let isLoading = false;
|
||||||
|
|
||||||
const form = superForm(data, {
|
const form = superForm(data, {
|
||||||
validators: zodClient(profileFormSchema)
|
validators: zodClient(profileFormSchema),
|
||||||
|
onSubmit: () => {
|
||||||
|
isLoading = true;
|
||||||
|
toast.success('Updating your name...');
|
||||||
|
},
|
||||||
|
onUpdated: ({ form: f }) => {
|
||||||
|
isLoading = false;
|
||||||
|
if (f.valid) {
|
||||||
|
toast.success('Your name has been updated.');
|
||||||
|
} else {
|
||||||
|
toast.error('Please fix the errors in the form.');
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const { form: formData, enhance } = form;
|
const { form: formData, enhance } = form;
|
||||||
|
@ -45,7 +59,7 @@
|
||||||
<Form.FieldErrors />
|
<Form.FieldErrors />
|
||||||
</Form.Field>
|
</Form.Field>
|
||||||
|
|
||||||
<Form.Button>Update name</Form.Button>
|
<Form.Button disabled={isLoading}>Update name</Form.Button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser}
|
{#if dev && PUBLIC_DEBUG_FORMS == 'true' && browser}
|
||||||
|
|
Loading…
Reference in a new issue