♻️ Move the settings page to PocketBase and SignalDB
The shared token form is gone. Working hours, workdays and timezone are edited through the synced settings collection, and a new account card shows the signed-in user, a password change and sign out.
This commit is contained in:
@@ -1,22 +1,43 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import { onMount } from 'svelte';
|
import { goto } from '$app/navigation';
|
||||||
import { settings, setToken, hasToken, type Settings, ApiError } from '$lib/api/client';
|
import {
|
||||||
import { todayKey } from '$lib/utils';
|
addSettings,
|
||||||
|
currentSettings,
|
||||||
|
deleteSettings,
|
||||||
|
settingsHistory,
|
||||||
|
todayKey,
|
||||||
|
updateSettings
|
||||||
|
} from '$lib/app.svelte';
|
||||||
|
import type { Doc } from '$lib/db.svelte';
|
||||||
|
import { errorMessage, pb } from '$lib/pb';
|
||||||
|
import { logout, session } from '$lib/session.svelte';
|
||||||
|
import type { Settings } from '$lib/types';
|
||||||
|
|
||||||
|
const current = $derived(currentSettings());
|
||||||
|
const history = $derived(settingsHistory());
|
||||||
|
|
||||||
let current: Settings | null = $state(null);
|
|
||||||
let history: Settings[] = $state([]);
|
|
||||||
let error = $state('');
|
let error = $state('');
|
||||||
let saved = $state('');
|
let saved = $state('');
|
||||||
|
|
||||||
// Token setup
|
// Account
|
||||||
let tokenInput = $state('');
|
const PASSWORD_LABELS: Record<string, string> = {
|
||||||
let tokenSaved = $state(hasToken());
|
oldPassword: 'Current password',
|
||||||
|
password: 'New password',
|
||||||
|
passwordConfirm: 'Confirm new password'
|
||||||
|
};
|
||||||
|
let currentPassword = $state('');
|
||||||
|
let newPassword = $state('');
|
||||||
|
let confirmPassword = $state('');
|
||||||
|
let accountError = $state('');
|
||||||
|
let accountSaved = $state('');
|
||||||
|
let passwordSaving = $state(false);
|
||||||
|
|
||||||
// New settings form
|
// New settings form
|
||||||
|
const initial = currentSettings();
|
||||||
let formEffectiveFrom = $state(todayKey());
|
let formEffectiveFrom = $state(todayKey());
|
||||||
let formHoursPerWeek = $state(40);
|
let formHoursPerWeek = $state(initial.hours_per_week);
|
||||||
let formWorkdaysMask = $state(31);
|
let formWorkdaysMask = $state(initial.workdays_mask);
|
||||||
let formTimezone = $state('UTC');
|
let formTimezone = $state(Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC');
|
||||||
|
|
||||||
// Inline edit state for history rows
|
// Inline edit state for history rows
|
||||||
let editingId = $state<string | null>(null);
|
let editingId = $state<string | null>(null);
|
||||||
@@ -49,31 +70,53 @@
|
|||||||
return DAYS.filter((d) => mask & d.bit).map((d) => d.label).join(', ');
|
return DAYS.filter((d) => mask & d.bit).map((d) => d.label).join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
async function load() {
|
function passwordError(e: unknown): string {
|
||||||
if (!hasToken()) return;
|
const data = (e as { response?: { data?: Record<string, unknown> } }).response?.data ?? {};
|
||||||
error = '';
|
for (const [field, value] of Object.entries(data)) {
|
||||||
|
const message = (value as { message?: unknown } | null)?.message;
|
||||||
|
if (typeof message === 'string' && message) return `${PASSWORD_LABELS[field] ?? field}: ${message}`;
|
||||||
|
}
|
||||||
|
return errorMessage(e);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function changePassword(oldPassword: string, password: string): Promise<void> {
|
||||||
|
const record = pb.authStore.record;
|
||||||
|
if (!record) throw new Error('Not signed in');
|
||||||
try {
|
try {
|
||||||
const h = await settings.history();
|
await pb.collection('users').update(record.id, { oldPassword, password, passwordConfirm: password });
|
||||||
history = h ?? [];
|
|
||||||
const today = new Date().toISOString().slice(0, 10);
|
|
||||||
current = history.find((s) => s.effective_from <= today) ?? history[0] ?? null;
|
|
||||||
if (current) {
|
|
||||||
formHoursPerWeek = current.hours_per_week;
|
|
||||||
formWorkdaysMask = current.workdays_mask;
|
|
||||||
formTimezone = current.timezone;
|
|
||||||
}
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e instanceof ApiError ? e.message : String(e);
|
throw new Error(passwordError(e));
|
||||||
|
}
|
||||||
|
await pb.collection('users').authWithPassword(String(record.email), password);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function handleChangePassword(ev: Event) {
|
||||||
|
ev.preventDefault();
|
||||||
|
accountError = '';
|
||||||
|
accountSaved = '';
|
||||||
|
if (newPassword.length < 8) { accountError = 'New password must be at least 8 characters.'; return; }
|
||||||
|
if (newPassword !== confirmPassword) { accountError = 'New passwords do not match.'; return; }
|
||||||
|
passwordSaving = true;
|
||||||
|
try {
|
||||||
|
await changePassword(currentPassword, newPassword);
|
||||||
|
currentPassword = '';
|
||||||
|
newPassword = '';
|
||||||
|
confirmPassword = '';
|
||||||
|
accountSaved = 'Password changed.';
|
||||||
|
} catch (e) {
|
||||||
|
accountError = errorMessage(e);
|
||||||
|
} finally {
|
||||||
|
passwordSaving = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleSaveToken() {
|
async function handleSignOut() {
|
||||||
const t = tokenInput.trim();
|
try {
|
||||||
if (!t) return;
|
await logout();
|
||||||
setToken(t);
|
await goto('/login', { replaceState: true });
|
||||||
tokenSaved = true;
|
} catch (e) {
|
||||||
tokenInput = '';
|
accountError = errorMessage(e);
|
||||||
load();
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleSaveSettings(ev: Event) {
|
async function handleSaveSettings(ev: Event) {
|
||||||
@@ -81,22 +124,21 @@
|
|||||||
error = '';
|
error = '';
|
||||||
saved = '';
|
saved = '';
|
||||||
try {
|
try {
|
||||||
const s = await settings.upsert({
|
await addSettings({
|
||||||
effective_from: formEffectiveFrom,
|
effective_from: formEffectiveFrom,
|
||||||
hours_per_week: formHoursPerWeek,
|
hours_per_week: formHoursPerWeek,
|
||||||
workdays_mask: formWorkdaysMask,
|
workdays_mask: formWorkdaysMask,
|
||||||
timezone: formTimezone
|
timezone: formTimezone
|
||||||
});
|
});
|
||||||
saved = 'Settings saved!';
|
saved = 'Settings saved!';
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e instanceof ApiError ? e.message : String(e);
|
error = errorMessage(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── Inline edit ──────────────────────────────────────────────────────────────
|
// ── Inline edit ──────────────────────────────────────────────────────────────
|
||||||
|
|
||||||
function startEdit(s: Settings) {
|
function startEdit(s: Doc<Settings>) {
|
||||||
editingId = s.id;
|
editingId = s.id;
|
||||||
editEffectiveFrom = s.effective_from;
|
editEffectiveFrom = s.effective_from;
|
||||||
editHoursPerWeek = s.hours_per_week;
|
editHoursPerWeek = s.hours_per_week;
|
||||||
@@ -113,52 +155,58 @@
|
|||||||
async function saveEdit(id: string) {
|
async function saveEdit(id: string) {
|
||||||
editError = '';
|
editError = '';
|
||||||
try {
|
try {
|
||||||
await settings.update(id, {
|
await updateSettings(id, {
|
||||||
effective_from: editEffectiveFrom,
|
effective_from: editEffectiveFrom,
|
||||||
hours_per_week: editHoursPerWeek,
|
hours_per_week: editHoursPerWeek,
|
||||||
workdays_mask: editWorkdaysMask,
|
workdays_mask: editWorkdaysMask,
|
||||||
timezone: editTimezone
|
timezone: editTimezone
|
||||||
});
|
});
|
||||||
editingId = null;
|
editingId = null;
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
editError = e instanceof ApiError ? e.message : String(e);
|
editError = errorMessage(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function handleDelete(id: string) {
|
async function handleDelete(id: string) {
|
||||||
error = '';
|
error = '';
|
||||||
try {
|
try {
|
||||||
await settings.delete(id);
|
await deleteSettings(id);
|
||||||
await load();
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
error = e instanceof ApiError ? e.message : String(e);
|
error = errorMessage(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMount(load);
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="settings">
|
<div class="settings">
|
||||||
<h1>Settings</h1>
|
<h1>Settings</h1>
|
||||||
|
|
||||||
<!-- Token setup -->
|
|
||||||
<section>
|
|
||||||
<h2>API Token</h2>
|
|
||||||
{#if tokenSaved}
|
|
||||||
<p class="ok">Token configured. <button onclick={() => { tokenSaved = false; }}>Change</button></p>
|
|
||||||
{:else}
|
|
||||||
<div class="token-row">
|
|
||||||
<input type="password" bind:value={tokenInput} placeholder="Paste your AUTH_TOKEN" />
|
|
||||||
<button onclick={handleSaveToken}>Save token</button>
|
|
||||||
</div>
|
|
||||||
{/if}
|
|
||||||
</section>
|
|
||||||
|
|
||||||
{#if tokenSaved}
|
|
||||||
{#if error}<p class="error">{error}</p>{/if}
|
{#if error}<p class="error">{error}</p>{/if}
|
||||||
{#if saved}<p class="ok">{saved}</p>{/if}
|
{#if saved}<p class="ok">{saved}</p>{/if}
|
||||||
|
|
||||||
|
<!-- Account -->
|
||||||
|
<section>
|
||||||
|
<h2>Account</h2>
|
||||||
|
<p class="account-email">Signed in as <strong>{session.email}</strong></p>
|
||||||
|
{#if accountError}<p class="error">{accountError}</p>{/if}
|
||||||
|
{#if accountSaved}<p class="ok">{accountSaved}</p>{/if}
|
||||||
|
<form onsubmit={handleChangePassword}>
|
||||||
|
<label>
|
||||||
|
Current password
|
||||||
|
<input type="password" bind:value={currentPassword} required autocomplete="current-password" />
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
New password
|
||||||
|
<input type="password" bind:value={newPassword} required autocomplete="new-password" />
|
||||||
|
</label>
|
||||||
|
<label>
|
||||||
|
Confirm new password
|
||||||
|
<input type="password" bind:value={confirmPassword} required autocomplete="new-password" />
|
||||||
|
</label>
|
||||||
|
<button type="submit" disabled={passwordSaving}>Change password</button>
|
||||||
|
</form>
|
||||||
|
<button class="signout" onclick={handleSignOut}>Sign out</button>
|
||||||
|
</section>
|
||||||
|
|
||||||
<!-- Settings form -->
|
<!-- Settings form -->
|
||||||
<section>
|
<section>
|
||||||
<h2>Add / change working hours</h2>
|
<h2>Add / change working hours</h2>
|
||||||
@@ -191,7 +239,6 @@
|
|||||||
</section>
|
</section>
|
||||||
|
|
||||||
<!-- Current effective settings -->
|
<!-- Current effective settings -->
|
||||||
{#if current}
|
|
||||||
<section>
|
<section>
|
||||||
<h2>Current effective settings</h2>
|
<h2>Current effective settings</h2>
|
||||||
<dl>
|
<dl>
|
||||||
@@ -201,7 +248,6 @@
|
|||||||
<dt>Effective from</dt><dd>{current.effective_from}</dd>
|
<dt>Effective from</dt><dd>{current.effective_from}</dd>
|
||||||
</dl>
|
</dl>
|
||||||
</section>
|
</section>
|
||||||
{/if}
|
|
||||||
|
|
||||||
<!-- History -->
|
<!-- History -->
|
||||||
{#if history.length > 0}
|
{#if history.length > 0}
|
||||||
@@ -274,7 +320,6 @@
|
|||||||
{/if}
|
{/if}
|
||||||
</section>
|
</section>
|
||||||
{/if}
|
{/if}
|
||||||
{/if}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
@@ -283,12 +328,11 @@
|
|||||||
section { margin-bottom: 2rem; }
|
section { margin-bottom: 2rem; }
|
||||||
.error { color: #c0392b; background: #fde8e4; padding: 0.5rem; border-radius: 6px; }
|
.error { color: #c0392b; background: #fde8e4; padding: 0.5rem; border-radius: 6px; }
|
||||||
.ok { color: #155724; background: #d4edda; padding: 0.5rem; border-radius: 6px; }
|
.ok { color: #155724; background: #d4edda; padding: 0.5rem; border-radius: 6px; }
|
||||||
.token-row { display: flex; gap: 0.5rem; }
|
.account-email { margin: 0 0 1rem; font-size: 0.9rem; color: #495057; }
|
||||||
.token-row input { flex: 1; padding: 0.5rem; border: 1px solid #ced4da; border-radius: 6px; font-size: 0.95rem; }
|
.signout { margin-top: 1rem; padding: 0.55rem 1.4rem; background: #e9ecef; color: #343a40; border: none; border-radius: 6px; font-weight: 600; }
|
||||||
.token-row button, .ok button { padding: 0.5rem 1rem; background: #2c7be5; color: #fff; border: none; border-radius: 6px; }
|
|
||||||
form { display: flex; flex-direction: column; gap: 1rem; max-width: 400px; }
|
form { display: flex; flex-direction: column; gap: 1rem; max-width: 400px; }
|
||||||
form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; font-weight: 500; }
|
form label { display: flex; flex-direction: column; gap: 0.25rem; font-size: 0.9rem; font-weight: 500; }
|
||||||
form input[type="text"], form input[type="number"], form input[type="date"] {
|
form input[type="text"], form input[type="number"], form input[type="date"], form input[type="password"] {
|
||||||
padding: 0.45rem 0.75rem; border: 1px solid #ced4da; border-radius: 6px; font-size: 0.95rem;
|
padding: 0.45rem 0.75rem; border: 1px solid #ced4da; border-radius: 6px; font-size: 0.95rem;
|
||||||
}
|
}
|
||||||
fieldset { border: 1px solid #dee2e6; border-radius: 6px; padding: 0.5rem 1rem; }
|
fieldset { border: 1px solid #dee2e6; border-radius: 6px; padding: 0.5rem 1rem; }
|
||||||
@@ -296,6 +340,7 @@
|
|||||||
.days-row { display: flex; gap: 0.75rem; flex-wrap: wrap; margin-top: 0.25rem; }
|
.days-row { display: flex; gap: 0.75rem; flex-wrap: wrap; margin-top: 0.25rem; }
|
||||||
.day-check { display: flex; align-items: center; gap: 0.3rem; font-weight: 400; cursor: pointer; }
|
.day-check { display: flex; align-items: center; gap: 0.3rem; font-weight: 400; cursor: pointer; }
|
||||||
form button[type="submit"] { align-self: flex-start; padding: 0.55rem 1.4rem; background: #2c7be5; color: #fff; border: none; border-radius: 6px; font-weight: 600; }
|
form button[type="submit"] { align-self: flex-start; padding: 0.55rem 1.4rem; background: #2c7be5; color: #fff; border: none; border-radius: 6px; font-weight: 600; }
|
||||||
|
form button[type="submit"]:disabled { opacity: 0.6; cursor: not-allowed; }
|
||||||
dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 1rem; font-size: 0.9rem; }
|
dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 1rem; font-size: 0.9rem; }
|
||||||
dt { font-weight: 500; color: #6c757d; }
|
dt { font-weight: 500; color: #6c757d; }
|
||||||
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
|
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
|
||||||
|
|||||||
Reference in New Issue
Block a user