From fc1afecfdd7050a8d33a0c2141af8da7a99c8602 Mon Sep 17 00:00:00 2001 From: Xyphyn Date: Tue, 1 Aug 2023 10:55:14 -0700 Subject: [PATCH] misc: fix persistence --- src/lib/auth.ts | 43 ++++++++++++++++++++++------- src/lib/components/ui/Navbar.svelte | 34 ++++++++++++++++++----- 2 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/lib/auth.ts b/src/lib/auth.ts index ae3dad47..fdd5b143 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -34,16 +34,16 @@ interface PersonData extends MyUserInfo { reports: number } -export const profileData = writable({ profiles: [], profile: -1 }) -export const profile = writable({ - id: -1, - instance: DEFAULT_INSTANCE_URL, -}) +export const profileData = writable( + getFromStorage('profileData') ?? { profiles: [], profile: -1 } +) profileData.subscribe((pd) => { setFromStorage('profileData', pd) }) +export const profile = writable(getProfile()) + profile.subscribe(async (p) => { if (!p || !p.jwt) { profileData.update((pd) => ({ ...pd, profile: -1 })) @@ -117,12 +117,35 @@ const userToPersonData = (user: MyUserInfo): PersonData => ({ reports: 0, }) -function getProfileData() { - const pd = getFromStorage('profileData')! +function getProfile() { + const id = get(profileData).profile - profileData.set(pd) + console.log('id is', id) + // returns something that isn't -1 - console.log(get(profileData)) + if (id == -1) { + console.log('returning, since ID is', id) + // still logs :/ + return + } + + console.log('fetching user of id', id) + + const pd = get(profileData) + + console.log('pd', pd) + + return pd.profiles.find((p) => p.id == id) } -getProfileData() +export function setUserID(id: number) { + const pd = get(profileData) + console.log('pd', pd) + const prof = pd.profiles.find((p) => p.id == id) + + profile.set(prof) + profileData.update((p) => ({ ...p, profile: id })) + console.log('new', pd) + + return prof +} diff --git a/src/lib/components/ui/Navbar.svelte b/src/lib/components/ui/Navbar.svelte index 6cb1b295..a58da9d6 100644 --- a/src/lib/components/ui/Navbar.svelte +++ b/src/lib/components/ui/Navbar.svelte @@ -1,4 +1,5 @@