refactor(user): apply change on update entries
This commit is contained in:
@@ -1,7 +1,7 @@
|
|||||||
/** Profile / account endpoints — ported from views/profile/profile.js. */
|
/** Profile / account endpoints — ported from views/profile/profile.js. */
|
||||||
import { apiFetch } from '$lib/api/client';
|
import { apiFetch } from '$lib/api/client';
|
||||||
import { getCsrfHeaders } from '$lib/api/csrf';
|
import { getCsrfHeaders } from '$lib/api/csrf';
|
||||||
import type { PublicUser } from '$lib/api/types';
|
import type { SelfUser } from '$lib/api/types';
|
||||||
import { t } from '$lib/i18n/index.svelte';
|
import { t } from '$lib/i18n/index.svelte';
|
||||||
|
|
||||||
const JSON_HEADERS = { 'Content-Type': 'application/json' };
|
const JSON_HEADERS = { 'Content-Type': 'application/json' };
|
||||||
@@ -24,7 +24,7 @@ export interface ProfilePatch {
|
|||||||
ui_preferences?: Record<string, unknown>;
|
ui_preferences?: Record<string, unknown>;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function updateProfile(patch: ProfilePatch): Promise<PublicUser> {
|
export async function updateProfile(patch: ProfilePatch): Promise<SelfUser> {
|
||||||
const res = await apiFetch('/api/auth/me/profile', {
|
const res = await apiFetch('/api/auth/me/profile', {
|
||||||
method: 'PATCH',
|
method: 'PATCH',
|
||||||
credentials: 'same-origin',
|
credentials: 'same-origin',
|
||||||
@@ -57,7 +57,7 @@ export async function updateProfile(patch: ProfilePatch): Promise<PublicUser> {
|
|||||||
}
|
}
|
||||||
throw new Error(err.message || err.error || `profile update failed: ${res.status}`);
|
throw new Error(err.message || err.error || `profile update failed: ${res.status}`);
|
||||||
}
|
}
|
||||||
return (await res.json()) as PublicUser;
|
return (await res.json()) as SelfUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function changePassword(currentPw: string, newPw: string): Promise<void> {
|
export async function changePassword(currentPw: string, newPw: string): Promise<void> {
|
||||||
|
|||||||
@@ -210,13 +210,13 @@
|
|||||||
|
|
||||||
savingProfile = true;
|
savingProfile = true;
|
||||||
try {
|
try {
|
||||||
await updateProfile(patch);
|
// PATCH /me/profile echoes SelfUser (same shape as GET /me)
|
||||||
// Server returns the truncated `PublicUser` shape; re-fetch
|
// so the SPA absorbs the just-written state in one round
|
||||||
// `/me` so `session.me` picks up self-only edits (locale,
|
// trip — no follow-up refresh needed. `session.user` is a
|
||||||
// notify_on_share, ui_preferences bag) as well as the public
|
// derived accessor over `session.me.full.user`, so it
|
||||||
// identity changes. `session.user` is a derived accessor over
|
// updates in lockstep with the me assignment.
|
||||||
// `session.me.full.user`, so it updates in lockstep.
|
const updated = await updateProfile(patch);
|
||||||
await session.refresh();
|
session.me = updated;
|
||||||
if (patch.preferred_locale) await setLocale(patch.preferred_locale as Locale);
|
if (patch.preferred_locale) await setLocale(patch.preferred_locale as Locale);
|
||||||
ui.notify(t('profile.saved', 'Profile saved'), 'success');
|
ui.notify(t('profile.saved', 'Profile saved'), 'success');
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
Reference in New Issue
Block a user