♻️ 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">
|
||||
import { onMount } from 'svelte';
|
||||
import { settings, setToken, hasToken, type Settings, ApiError } from '$lib/api/client';
|
||||
import { todayKey } from '$lib/utils';
|
||||
import { goto } from '$app/navigation';
|
||||
import {
|
||||
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 saved = $state('');
|
||||
|
||||
// Token setup
|
||||
let tokenInput = $state('');
|
||||
let tokenSaved = $state(hasToken());
|
||||
// Account
|
||||
const PASSWORD_LABELS: Record<string, string> = {
|
||||
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
|
||||
const initial = currentSettings();
|
||||
let formEffectiveFrom = $state(todayKey());
|
||||
let formHoursPerWeek = $state(40);
|
||||
let formWorkdaysMask = $state(31);
|
||||
let formTimezone = $state('UTC');
|
||||
let formHoursPerWeek = $state(initial.hours_per_week);
|
||||
let formWorkdaysMask = $state(initial.workdays_mask);
|
||||
let formTimezone = $state(Intl.DateTimeFormat().resolvedOptions().timeZone || 'UTC');
|
||||
|
||||
// Inline edit state for history rows
|
||||
let editingId = $state<string | null>(null);
|
||||
@@ -49,31 +70,53 @@
|
||||
return DAYS.filter((d) => mask & d.bit).map((d) => d.label).join(', ');
|
||||
}
|
||||
|
||||
async function load() {
|
||||
if (!hasToken()) return;
|
||||
error = '';
|
||||
function passwordError(e: unknown): string {
|
||||
const data = (e as { response?: { data?: Record<string, unknown> } }).response?.data ?? {};
|
||||
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 {
|
||||
const h = await settings.history();
|
||||
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;
|
||||
}
|
||||
await pb.collection('users').update(record.id, { oldPassword, password, passwordConfirm: password });
|
||||
} 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() {
|
||||
const t = tokenInput.trim();
|
||||
if (!t) return;
|
||||
setToken(t);
|
||||
tokenSaved = true;
|
||||
tokenInput = '';
|
||||
load();
|
||||
async function handleSignOut() {
|
||||
try {
|
||||
await logout();
|
||||
await goto('/login', { replaceState: true });
|
||||
} catch (e) {
|
||||
accountError = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleSaveSettings(ev: Event) {
|
||||
@@ -81,22 +124,21 @@
|
||||
error = '';
|
||||
saved = '';
|
||||
try {
|
||||
const s = await settings.upsert({
|
||||
await addSettings({
|
||||
effective_from: formEffectiveFrom,
|
||||
hours_per_week: formHoursPerWeek,
|
||||
workdays_mask: formWorkdaysMask,
|
||||
timezone: formTimezone
|
||||
});
|
||||
saved = 'Settings saved!';
|
||||
await load();
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
// ── Inline edit ──────────────────────────────────────────────────────────────
|
||||
|
||||
function startEdit(s: Settings) {
|
||||
function startEdit(s: Doc<Settings>) {
|
||||
editingId = s.id;
|
||||
editEffectiveFrom = s.effective_from;
|
||||
editHoursPerWeek = s.hours_per_week;
|
||||
@@ -113,52 +155,58 @@
|
||||
async function saveEdit(id: string) {
|
||||
editError = '';
|
||||
try {
|
||||
await settings.update(id, {
|
||||
await updateSettings(id, {
|
||||
effective_from: editEffectiveFrom,
|
||||
hours_per_week: editHoursPerWeek,
|
||||
workdays_mask: editWorkdaysMask,
|
||||
timezone: editTimezone
|
||||
});
|
||||
editingId = null;
|
||||
await load();
|
||||
} catch (e) {
|
||||
editError = e instanceof ApiError ? e.message : String(e);
|
||||
editError = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function handleDelete(id: string) {
|
||||
error = '';
|
||||
try {
|
||||
await settings.delete(id);
|
||||
await load();
|
||||
await deleteSettings(id);
|
||||
} catch (e) {
|
||||
error = e instanceof ApiError ? e.message : String(e);
|
||||
error = errorMessage(e);
|
||||
}
|
||||
}
|
||||
|
||||
onMount(load);
|
||||
</script>
|
||||
|
||||
<div class="settings">
|
||||
<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 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 -->
|
||||
<section>
|
||||
<h2>Add / change working hours</h2>
|
||||
@@ -191,7 +239,6 @@
|
||||
</section>
|
||||
|
||||
<!-- Current effective settings -->
|
||||
{#if current}
|
||||
<section>
|
||||
<h2>Current effective settings</h2>
|
||||
<dl>
|
||||
@@ -201,7 +248,6 @@
|
||||
<dt>Effective from</dt><dd>{current.effective_from}</dd>
|
||||
</dl>
|
||||
</section>
|
||||
{/if}
|
||||
|
||||
<!-- History -->
|
||||
{#if history.length > 0}
|
||||
@@ -274,7 +320,6 @@
|
||||
{/if}
|
||||
</section>
|
||||
{/if}
|
||||
{/if}
|
||||
</div>
|
||||
|
||||
<style>
|
||||
@@ -283,12 +328,11 @@
|
||||
section { margin-bottom: 2rem; }
|
||||
.error { color: #c0392b; background: #fde8e4; padding: 0.5rem; border-radius: 6px; }
|
||||
.ok { color: #155724; background: #d4edda; padding: 0.5rem; border-radius: 6px; }
|
||||
.token-row { display: flex; gap: 0.5rem; }
|
||||
.token-row input { flex: 1; padding: 0.5rem; border: 1px solid #ced4da; border-radius: 6px; font-size: 0.95rem; }
|
||||
.token-row button, .ok button { padding: 0.5rem 1rem; background: #2c7be5; color: #fff; border: none; border-radius: 6px; }
|
||||
.account-email { margin: 0 0 1rem; font-size: 0.9rem; color: #495057; }
|
||||
.signout { margin-top: 1rem; padding: 0.55rem 1.4rem; background: #e9ecef; color: #343a40; border: none; border-radius: 6px; font-weight: 600; }
|
||||
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 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;
|
||||
}
|
||||
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; }
|
||||
.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"]:disabled { opacity: 0.6; cursor: not-allowed; }
|
||||
dl { display: grid; grid-template-columns: auto 1fr; gap: 0.25rem 1rem; font-size: 0.9rem; }
|
||||
dt { font-weight: 500; color: #6c757d; }
|
||||
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
|
||||
|
||||
Reference in New Issue
Block a user