Skip to content

Commit a577091

Browse files
committed
🥟✨ Show username in settings
1 parent 4d7674a commit a577091

File tree

4 files changed

+50
-3
lines changed

4 files changed

+50
-3
lines changed

gyoza/src/lib/MainHeader.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import {logOutMutation, verifyQuery} from "$lib/backend";
33
import {useQueryClient} from "@tanstack/svelte-query";
44
5-
const verify = verifyQuery();
5+
const query = verifyQuery();
66
const logOut = logOutMutation(useQueryClient());
77
88
function handleLogOut(e: MouseEvent) {
@@ -19,7 +19,7 @@
1919
<nav>
2020
<a href="/">🏠️</a>
2121
<a class="plain" href="/about">About</a>
22-
{#if $verify?.data?.is_authenticated}
22+
{#if $query?.data?.is_authenticated}
2323
<a href="/settings">Settings</a>
2424
<a href="/log-out" on:click={handleLogOut}>Log Out</a>
2525
{:else}

gyoza/src/lib/backend.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,16 @@ export function logOutMutation(client: QueryClient) {
4848
});
4949
}
5050

51+
export function currentUserQuery() {
52+
return createQuery({
53+
queryFn: api().getCurrentUser,
54+
queryKey: ["current-user"],
55+
});
56+
}
57+
5158
export type Status = {status: string};
5259
export type Verify = {is_authenticated: boolean};
60+
export type User = {user_id: string; username: string;}
5361

5462
type SignUpArgs = {username: string; email: string; password: string;};
5563
type LogInArgs = {username_or_email: string; password: string;};
@@ -97,5 +105,12 @@ export function api(_fetch = fetch) {
97105
});
98106
return data as Status;
99107
},
108+
getCurrentUser: async (): Promise<User> => {
109+
const data = await handleFetch({
110+
url: `${backend}/api/users/me`,
111+
_fetch,
112+
});
113+
return data as User;
114+
},
100115
};
101116
}

gyoza/src/lib/user/Profile.svelte

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<script lang="ts">
2+
import {currentUserQuery} from "$lib/backend";
3+
4+
const query = currentUserQuery();
5+
$: user = $query?.data ?? undefined;
6+
</script>
7+
8+
<span class="text-neutral-100">Username:</span>
9+
{#if user }
10+
{user.username}
11+
{/if}
Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,27 @@
1+
<script>
2+
import Profile from "$lib/user/Profile.svelte";
3+
</script>
4+
15
<svelte:head>
26
<title>Settings</title>
37
<meta name="description" content="User settings page">
48
</svelte:head>
59

6-
<h1>Settings</h1>
10+
<main class="max-w-7xl mx-auto p-4">
11+
<h1 class="text-5xl">Settings</h1>
12+
<div class="flex flex-col gap-4 mt-4">
13+
<section>
14+
<h2>Profile</h2>
15+
<Profile />
16+
</section>
17+
</div>
18+
</main>
19+
20+
<style lang="postcss">
21+
section {
22+
@apply mt-2;
23+
}
24+
section h2 {
25+
@apply text-2xl;
26+
}
27+
</style>

0 commit comments

Comments
 (0)