2026-04-27 21:53:36 +12:00
|
|
|
<script lang="ts">
|
2026-05-08 09:06:14 +12:00
|
|
|
import { api } from '$lib/api';
|
2026-05-08 23:07:01 +12:00
|
|
|
import AppSecondaryRail from '$lib/components/navigation/AppSecondaryRail.svelte';
|
|
|
|
|
import AppSecondaryRailLayout from '$lib/components/navigation/AppSecondaryRailLayout.svelte';
|
2026-04-27 21:53:36 +12:00
|
|
|
import { clientSession } from '$lib/session';
|
2026-05-08 09:06:14 +12:00
|
|
|
import { toast } from '$lib/toast';
|
|
|
|
|
import { CircleUserRound, LockKeyhole } from 'lucide-svelte';
|
2026-04-27 21:53:36 +12:00
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
type Section = 'profile' | 'security';
|
|
|
|
|
let activeSection = $state<Section>('profile');
|
2026-04-27 21:53:36 +12:00
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
let name = $state($clientSession?.name ?? '');
|
|
|
|
|
let email = $state($clientSession?.email ?? '');
|
2026-04-27 21:53:36 +12:00
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
let currentPassword = $state('');
|
|
|
|
|
let newPassword = $state('');
|
|
|
|
|
let confirmPassword = $state('');
|
2026-04-27 21:53:36 +12:00
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
let profileSaving = $state(false);
|
|
|
|
|
let passwordSaving = $state(false);
|
|
|
|
|
let passwordError = $state('');
|
|
|
|
|
|
|
|
|
|
async function saveProfile() {
|
|
|
|
|
profileSaving = true;
|
|
|
|
|
const tid = toast.loading('Saving profile…');
|
|
|
|
|
try {
|
|
|
|
|
const updated = await api.updateMe({ name: name.trim(), email: email.trim().toLowerCase() });
|
|
|
|
|
clientSession.set({
|
|
|
|
|
...$clientSession!,
|
|
|
|
|
name: updated.name,
|
|
|
|
|
email: updated.email,
|
|
|
|
|
});
|
|
|
|
|
toast.dismiss(tid);
|
|
|
|
|
toast.success('Profile updated');
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
toast.dismiss(tid);
|
|
|
|
|
toast.error(err instanceof Error ? err.message : 'An error occurred');
|
|
|
|
|
} finally {
|
|
|
|
|
profileSaving = false;
|
|
|
|
|
}
|
2026-04-27 21:53:36 +12:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
async function savePassword() {
|
|
|
|
|
passwordError = '';
|
|
|
|
|
if (newPassword !== confirmPassword) {
|
|
|
|
|
passwordError = 'New passwords do not match';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (newPassword.length < 8) {
|
|
|
|
|
passwordError = 'Password must be at least 8 characters';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
passwordSaving = true;
|
|
|
|
|
const tid = toast.loading('Updating password…');
|
|
|
|
|
try {
|
|
|
|
|
await api.updateMe({ current_password: currentPassword, new_password: newPassword });
|
|
|
|
|
currentPassword = '';
|
|
|
|
|
newPassword = '';
|
|
|
|
|
confirmPassword = '';
|
|
|
|
|
toast.dismiss(tid);
|
|
|
|
|
toast.success('Password updated');
|
|
|
|
|
} catch (err: unknown) {
|
|
|
|
|
toast.dismiss(tid);
|
|
|
|
|
const msg = err instanceof Error ? err.message : 'An error occurred';
|
|
|
|
|
passwordError = msg;
|
|
|
|
|
toast.error(msg);
|
|
|
|
|
} finally {
|
|
|
|
|
passwordSaving = false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const initials = $derived(
|
|
|
|
|
($clientSession?.name ?? '')
|
|
|
|
|
.split(' ')
|
|
|
|
|
.slice(0, 2)
|
|
|
|
|
.map((w) => w[0])
|
|
|
|
|
.join('')
|
|
|
|
|
.toUpperCase() || '?'
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const navItems: { id: Section; label: string; icon: typeof CircleUserRound }[] = [
|
|
|
|
|
{ id: 'profile', label: 'Profile', icon: CircleUserRound },
|
|
|
|
|
{ id: 'security', label: 'Security', icon: LockKeyhole },
|
|
|
|
|
];
|
2026-05-08 23:07:01 +12:00
|
|
|
|
|
|
|
|
const railGroups = [{ items: navItems }];
|
2026-05-08 09:06:14 +12:00
|
|
|
</script>
|
|
|
|
|
|
2026-05-08 23:07:01 +12:00
|
|
|
<AppSecondaryRailLayout>
|
|
|
|
|
{#snippet rail()}
|
|
|
|
|
<AppSecondaryRail
|
|
|
|
|
sectionLabel="Settings"
|
|
|
|
|
identityAvatarText={initials}
|
|
|
|
|
identityTitle={$clientSession?.name ?? 'Unknown'}
|
|
|
|
|
identitySubtitle={$clientSession?.role_name ?? $clientSession?.role ?? 'User'}
|
|
|
|
|
groups={railGroups}
|
|
|
|
|
activeId={activeSection}
|
|
|
|
|
onSelect={(id) => (activeSection = id as Section)}
|
|
|
|
|
/>
|
|
|
|
|
{/snippet}
|
2026-05-08 09:06:14 +12:00
|
|
|
|
|
|
|
|
<div class="settings-panel">
|
|
|
|
|
{#if activeSection === 'profile'}
|
|
|
|
|
<div class="panel-section">
|
|
|
|
|
<header class="panel-header">
|
|
|
|
|
<h2>Profile</h2>
|
|
|
|
|
<p>Update your display name and email address.</p>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<form class="panel-form" onsubmit={(e) => { e.preventDefault(); saveProfile(); }}>
|
|
|
|
|
<div class="field-row">
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="name">Full name</label>
|
|
|
|
|
<input id="name" type="text" bind:value={name} autocomplete="name" required />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="email">Email address</label>
|
|
|
|
|
<input id="email" type="email" bind:value={email} autocomplete="email" required />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="form-footer">
|
|
|
|
|
<button class="btn-primary" type="submit" disabled={profileSaving}>
|
|
|
|
|
{profileSaving ? 'Saving…' : 'Save changes'}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{:else if activeSection === 'security'}
|
|
|
|
|
<div class="panel-section">
|
|
|
|
|
<header class="panel-header">
|
|
|
|
|
<h2>Security</h2>
|
|
|
|
|
<p>Choose a strong password with at least 8 characters.</p>
|
|
|
|
|
</header>
|
|
|
|
|
|
|
|
|
|
<form class="panel-form" onsubmit={(e) => { e.preventDefault(); savePassword(); }}>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="current-password">Current password</label>
|
|
|
|
|
<input id="current-password" type="password" bind:value={currentPassword} autocomplete="current-password" required />
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div class="divider" aria-hidden="true"></div>
|
|
|
|
|
|
|
|
|
|
<div class="field-row">
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="new-password">New password</label>
|
|
|
|
|
<input id="new-password" type="password" bind:value={newPassword} autocomplete="new-password" required />
|
|
|
|
|
</div>
|
|
|
|
|
<div class="field">
|
|
|
|
|
<label for="confirm-password">Confirm new password</label>
|
|
|
|
|
<input id="confirm-password" type="password" bind:value={confirmPassword} autocomplete="new-password" required />
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
{#if passwordError}
|
|
|
|
|
<p class="form-error">{passwordError}</p>
|
|
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
<div class="form-footer">
|
|
|
|
|
<button class="btn-primary" type="submit" disabled={passwordSaving}>
|
|
|
|
|
{passwordSaving ? 'Updating…' : 'Update password'}
|
|
|
|
|
</button>
|
|
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</div>
|
2026-05-08 23:07:01 +12:00
|
|
|
</AppSecondaryRailLayout>
|
2026-05-08 09:06:14 +12:00
|
|
|
|
|
|
|
|
<style>
|
|
|
|
|
.settings-panel {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
min-width: 0;
|
|
|
|
|
background: var(--panel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.panel-section {
|
|
|
|
|
display: flex;
|
|
|
|
|
flex-direction: column;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.panel-header {
|
|
|
|
|
padding: 1.5rem 1.75rem 1.25rem;
|
|
|
|
|
border-bottom: 1px solid var(--line);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.panel-header h2 {
|
|
|
|
|
margin: 0 0 0.3rem;
|
|
|
|
|
font-size: 1.1rem;
|
2026-04-27 21:53:36 +12:00
|
|
|
font-weight: 700;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
.panel-header p {
|
|
|
|
|
margin: 0;
|
|
|
|
|
font-size: 0.85rem;
|
2026-04-27 21:53:36 +12:00
|
|
|
color: var(--muted);
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
/* ── Form ───────────────────────────────────────────────────── */
|
|
|
|
|
|
|
|
|
|
.panel-form {
|
|
|
|
|
display: grid;
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
width: 100%;
|
|
|
|
|
padding: 1.5rem 1.75rem;
|
|
|
|
|
max-width: 42rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.field-row {
|
2026-04-27 21:53:36 +12:00
|
|
|
display: grid;
|
|
|
|
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
|
|
|
gap: 1rem;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
.field {
|
2026-04-27 21:53:36 +12:00
|
|
|
display: grid;
|
2026-05-08 09:06:14 +12:00
|
|
|
gap: 0.42rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.field label {
|
|
|
|
|
font-size: 0.82rem;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
color: var(--text);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.field input {
|
|
|
|
|
width: 100%;
|
|
|
|
|
padding: 0.62rem 0.85rem;
|
2026-04-27 21:53:36 +12:00
|
|
|
border: 1px solid var(--line);
|
2026-05-08 09:06:14 +12:00
|
|
|
border-radius: 0.6rem;
|
2026-04-27 21:53:36 +12:00
|
|
|
background: var(--panel-soft);
|
2026-05-08 09:06:14 +12:00
|
|
|
color: var(--text);
|
|
|
|
|
font-size: 0.9rem;
|
|
|
|
|
transition: border-color 140ms ease, box-shadow 140ms ease;
|
|
|
|
|
box-sizing: border-box;
|
2026-04-27 21:53:36 +12:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
.field input:focus {
|
|
|
|
|
outline: none;
|
|
|
|
|
border-color: var(--green-deep);
|
|
|
|
|
box-shadow: 0 0 0 3px color-mix(in srgb, var(--color-brand) 18%, transparent);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.divider {
|
|
|
|
|
height: 1px;
|
|
|
|
|
background: var(--line);
|
|
|
|
|
margin: 0.25rem 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.form-error {
|
|
|
|
|
margin: 0;
|
|
|
|
|
padding: 0.65rem 0.85rem;
|
|
|
|
|
background: color-mix(in srgb, #e53e3e 8%, transparent);
|
|
|
|
|
border: 1px solid color-mix(in srgb, #e53e3e 25%, transparent);
|
|
|
|
|
border-radius: 0.6rem;
|
|
|
|
|
color: #c53030;
|
2026-04-27 21:53:36 +12:00
|
|
|
font-size: 0.84rem;
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
.form-footer {
|
|
|
|
|
display: flex;
|
|
|
|
|
justify-content: flex-end;
|
|
|
|
|
padding-top: 0.25rem;
|
|
|
|
|
border-top: 1px solid var(--line);
|
|
|
|
|
margin-top: 0.5rem;
|
2026-04-27 21:53:36 +12:00
|
|
|
}
|
|
|
|
|
|
2026-05-08 09:06:14 +12:00
|
|
|
.btn-primary {
|
|
|
|
|
padding: 0.58rem 1.4rem;
|
|
|
|
|
background: var(--color-brand);
|
|
|
|
|
color: #fff;
|
|
|
|
|
border: none;
|
|
|
|
|
border-radius: 0.6rem;
|
|
|
|
|
font-size: 0.88rem;
|
|
|
|
|
font-weight: 600;
|
|
|
|
|
cursor: pointer;
|
|
|
|
|
transition: opacity 140ms ease;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-primary:hover:not(:disabled) {
|
|
|
|
|
opacity: 0.88;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.btn-primary:disabled {
|
|
|
|
|
opacity: 0.6;
|
|
|
|
|
cursor: not-allowed;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* ── Responsive ─────────────────────────────────────────────── */
|
|
|
|
|
|
|
|
|
|
@media (max-width: 720px) {
|
|
|
|
|
.field-row {
|
2026-04-27 21:53:36 +12:00
|
|
|
grid-template-columns: 1fr;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
</style>
|