misc: fix persistence

This commit is contained in:
Xyphyn committed 2023-08-01 11:24:49 -07:00
1 parent 55a047ab73
commit fc1afecfdd
2 files changed
+57 -14

No files matched your search

+33 -10
View File
@@ -34,16 +34,16 @@ interface PersonData extends MyUserInfo {
reports: number reports: number
} }
export const profileData = writable<ProfileData>({ profiles: [], profile: -1 }) export const profileData = writable<ProfileData>(
export const profile = writable<Profile | undefined>({ getFromStorage<ProfileData>('profileData') ?? { profiles: [], profile: -1 }
id: -1, )
instance: DEFAULT_INSTANCE_URL,
})
profileData.subscribe((pd) => { profileData.subscribe((pd) => {
setFromStorage('profileData', pd) setFromStorage('profileData', pd)
}) })
export const profile = writable<Profile | undefined>(getProfile())
profile.subscribe(async (p) => { profile.subscribe(async (p) => {
if (!p || !p.jwt) { if (!p || !p.jwt) {
profileData.update((pd) => ({ ...pd, profile: -1 })) profileData.update((pd) => ({ ...pd, profile: -1 }))
@@ -117,12 +117,35 @@ const userToPersonData = (user: MyUserInfo): PersonData => ({
reports: 0, reports: 0,
}) })
function getProfileData() { function getProfile() {
const pd = getFromStorage<ProfileData>('profileData')! 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
} }
getProfileData() console.log('fetching user of id', id)
const pd = get(profileData)
console.log('pd', pd)
return pd.profiles.find((p) => p.id == id)
}
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
}
+24 -4
View File
@@ -1,4 +1,5 @@
<script lang="ts"> <script lang="ts">
import { profile, profileData, setUserID } from '$lib/auth.js'
import Button from '$lib/components/input/Button.svelte' import Button from '$lib/components/input/Button.svelte'
import Link from '$lib/components/input/Link.svelte' import Link from '$lib/components/input/Link.svelte'
import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte' import ShieldIcon from '$lib/components/lemmy/moderation/ShieldIcon.svelte'
@@ -18,6 +19,7 @@
import { import {
ArrowLeftOnRectangle, ArrowLeftOnRectangle,
ArrowRightOnRectangle, ArrowRightOnRectangle,
Beaker,
Bookmark, Bookmark,
Cog6Tooth, Cog6Tooth,
GlobeAlt, GlobeAlt,
@@ -174,8 +176,8 @@
<li class="text-xs opacity-80 text-left mx-4 my-1 py-1"> <li class="text-xs opacity-80 text-left mx-4 my-1 py-1">
{$user ? $user.local_user_view.person.name : 'Profile'} {$user ? $user.local_user_view.person.name : 'Profile'}
</li> </li>
{#if $user && $authData} {#if $profile}
<MenuButton <!-- <MenuButton
link link
href="/u/{$user.local_user_view.person.name}@{$authData.instance}" href="/u/{$user.local_user_view.person.name}@{$authData.instance}"
> >
@@ -203,13 +205,31 @@
color="dangerSecondary" color="dangerSecondary"
> >
<Icon src={ArrowRightOnRectangle} mini width={16} />Log out <Icon src={ArrowRightOnRectangle} mini width={16} />Log out
</MenuButton> </MenuButton> -->
{/if} {/if}
{#if !$user || !$authData} {#if !$profile?.user}
{#if $profileData.profiles.length == 0}
<MenuButton link href="/login"> <MenuButton link href="/login">
<Icon src={ArrowLeftOnRectangle} mini width={16} /> <Icon src={ArrowLeftOnRectangle} mini width={16} />
Log in Log in
</MenuButton> </MenuButton>
{:else}
<MenuButton
on:click={() => {
const acc = Number(prompt('user id'))
setUserID(acc)
}}
>
<Icon src={ArrowLeftOnRectangle} mini width={16} />
change account
</MenuButton>
{/if}
{:else}
<MenuButton on:click={() => console.log($profile)}>
<Icon src={Beaker} mini width={16} />
Log user
</MenuButton>
{/if} {/if}
<hr class="dark:opacity-10 w-[90%] my-2 mx-auto" /> <hr class="dark:opacity-10 w-[90%] my-2 mx-auto" />
<li class="text-xs px-4 py-1 my-1 opacity-80">App</li> <li class="text-xs px-4 py-1 my-1 opacity-80">App</li>