2026-08-04 15:17:15 +02:00
|
|
|
<script lang="ts">
|
2026-08-20 14:02:16 +02:00
|
|
|
import type { ProfileUpdateInput, TotpSetupRequest, TotpStatusRequest, TotpTokenAction } from '../types';
|
2026-08-04 15:17:15 +02:00
|
|
|
import Button from './Button.svelte';
|
|
|
|
|
import Dialog from './Dialog.svelte';
|
2026-08-05 14:57:01 +02:00
|
|
|
import TotpSettings from './TotpSettings.svelte';
|
2026-08-04 15:17:15 +02:00
|
|
|
|
|
|
|
|
let {
|
|
|
|
|
currentEmail = '',
|
|
|
|
|
currentUsername = '',
|
|
|
|
|
onSave,
|
2026-08-05 14:57:01 +02:00
|
|
|
onTotpDisable,
|
|
|
|
|
onTotpEnable,
|
|
|
|
|
onTotpSetup,
|
|
|
|
|
onTotpStatus,
|
|
|
|
|
totpEnabled = false,
|
2026-08-04 15:17:15 +02:00
|
|
|
open = $bindable(false)
|
|
|
|
|
}: {
|
|
|
|
|
currentEmail?: string;
|
|
|
|
|
currentUsername?: string;
|
|
|
|
|
onSave?: (input: ProfileUpdateInput) => void | Promise<void>;
|
2026-08-05 14:57:01 +02:00
|
|
|
onTotpDisable?: TotpTokenAction;
|
|
|
|
|
onTotpEnable?: TotpTokenAction;
|
|
|
|
|
onTotpSetup?: TotpSetupRequest;
|
|
|
|
|
onTotpStatus?: TotpStatusRequest;
|
|
|
|
|
totpEnabled?: boolean;
|
2026-08-04 15:17:15 +02:00
|
|
|
open?: boolean;
|
|
|
|
|
} = $props();
|
|
|
|
|
|
|
|
|
|
let username = $state('');
|
|
|
|
|
let email = $state('');
|
2026-08-04 16:29:20 +02:00
|
|
|
let password = $state('');
|
|
|
|
|
let confirmPassword = $state('');
|
2026-08-04 15:17:15 +02:00
|
|
|
let error = $state('');
|
|
|
|
|
let saving = $state(false);
|
|
|
|
|
|
|
|
|
|
$effect(() => {
|
|
|
|
|
if (open) {
|
|
|
|
|
username = currentUsername;
|
|
|
|
|
email = currentEmail;
|
2026-08-04 16:29:20 +02:00
|
|
|
password = '';
|
|
|
|
|
confirmPassword = '';
|
2026-08-04 15:17:15 +02:00
|
|
|
error = '';
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
async function save(event: SubmitEvent): Promise<void> {
|
|
|
|
|
event.preventDefault();
|
|
|
|
|
error = '';
|
|
|
|
|
|
2026-08-04 16:29:20 +02:00
|
|
|
if (password && password !== confirmPassword) {
|
|
|
|
|
error = 'Passwords do not match.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-08-04 15:17:15 +02:00
|
|
|
if (!onSave) {
|
|
|
|
|
error = 'The application has not configured its profile update handler.';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
saving = true;
|
|
|
|
|
try {
|
2026-08-04 16:29:20 +02:00
|
|
|
await onSave({ username, email, password: password || undefined });
|
2026-08-04 15:17:15 +02:00
|
|
|
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>
|
|
|
|
|
|
2026-08-04 16:29:20 +02:00
|
|
|
<label class="sc-field">
|
|
|
|
|
<span class="sc-label">New password</span>
|
|
|
|
|
<input
|
|
|
|
|
autocomplete="new-password"
|
|
|
|
|
class="sc-input"
|
|
|
|
|
minlength="8"
|
|
|
|
|
placeholder="Leave blank to keep the current password"
|
|
|
|
|
type="password"
|
|
|
|
|
bind:value={password}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
|
|
|
|
|
{#if password}
|
|
|
|
|
<label class="sc-field">
|
|
|
|
|
<span class="sc-label">Confirm new password</span>
|
|
|
|
|
<input
|
|
|
|
|
autocomplete="new-password"
|
|
|
|
|
class="sc-input"
|
|
|
|
|
minlength="8"
|
|
|
|
|
required
|
|
|
|
|
type="password"
|
|
|
|
|
bind:value={confirmPassword}
|
|
|
|
|
/>
|
|
|
|
|
</label>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2026-08-04 15:17:15 +02:00
|
|
|
{#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>
|
2026-08-05 14:57:01 +02:00
|
|
|
|
|
|
|
|
{#if onTotpSetup && onTotpEnable && onTotpDisable}
|
|
|
|
|
<TotpSettings
|
|
|
|
|
{open}
|
|
|
|
|
onDisable={onTotpDisable}
|
|
|
|
|
onEnable={onTotpEnable}
|
|
|
|
|
onSetup={onTotpSetup}
|
|
|
|
|
onStatus={onTotpStatus}
|
|
|
|
|
enabled={totpEnabled}
|
|
|
|
|
/>
|
|
|
|
|
{/if}
|
2026-08-04 15:17:15 +02:00
|
|
|
</Dialog>
|