80 lines
2.0 KiB
Svelte
80 lines
2.0 KiB
Svelte
<script lang="ts">
|
|||
|
|
import type { ProfileUpdateInput } from '../types';
|
||
|
|
import Button from './Button.svelte';
|
||
|
|
import Dialog from './Dialog.svelte';
|
||
|
|
|
||
|
|
let {
|
||
|
|
currentEmail = '',
|
||
|
|
currentUsername = '',
|
||
|
|
onSave,
|
||
|
|
open = $bindable(false)
|
||
|
|
}: {
|
||
|
|
currentEmail?: string;
|
||
|
|
currentUsername?: string;
|
||
|
|
onSave?: (input: ProfileUpdateInput) => void | Promise<void>;
|
||
|
|
open?: boolean;
|
||
|
|
} = $props();
|
||
|
|
|
||
|
|
let username = $state('');
|
||
|
|
let email = $state('');
|
||
|
|
let error = $state('');
|
||
|
|
let saving = $state(false);
|
||
|
|
|
||
|
|
$effect(() => {
|
||
|
|
if (open) {
|
||
|
|
username = currentUsername;
|
||
|
|
email = currentEmail;
|
||
|
|
error = '';
|
||
|
|
}
|
||
|
|
});
|
||
|
|
|
||
|
|
async function save(event: SubmitEvent): Promise<void> {
|
||
|
|
event.preventDefault();
|
||
|
|
error = '';
|
||
|
|
|
||
|
|
if (!onSave) {
|
||
|
|
error = 'The application has not configured its profile update handler.';
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
saving = true;
|
||
|
|
try {
|
||
|
|
await onSave({ username, email });
|
||
|
|
open = false;
|
||
|
|
} catch (caught) {
|
||
|
|
error = caught instanceof Error ? caught.message : 'Profile update failed.';
|
||
|
|
} finally {
|
||
|
|
saving = false;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
</script>
|
||
|
|
|
||
|
|
<Dialog bind:open title="Profile">
|
||
|
|
<div class="sc-profile-summary">
|
||
|
|
<strong>{currentUsername || 'Your profile'}</strong>
|
||
|
|
{#if currentEmail}
|
||
|
|
<span>{currentEmail}</span>
|
||
|
|
{/if}
|
||
|
|
</div>
|
||
|
|
|
||
|
|
<form class="sc-form" onsubmit={save}>
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Username</span>
|
||
|
|
<input autocomplete="username" class="sc-input" required type="text" bind:value={username} />
|
||
|
|
</label>
|
||
|
|
|
||
|
|
<label class="sc-field">
|
||
|
|
<span class="sc-label">Email</span>
|
||
|
|
<input autocomplete="email" class="sc-input" required type="email" bind:value={email} />
|
||
|
|
</label>
|
||
|
|
|
||
|
|
{#if error}
|
||
|
|
<p aria-live="polite" class="sc-error">{error}</p>
|
||
|
|
{/if}
|
||
|
|
|
||
|
|
<div class="sc-form-actions">
|
||
|
|
<Button variant="secondary" type="button" onclick={() => (open = false)}>Cancel</Button>
|
||
|
|
<Button disabled={saving} type="submit">{saving ? 'Saving…' : 'Save changes'}</Button>
|
||
|
|
</div>
|
||
|
|
</form>
|
||
|
|
</Dialog>
|