Skip to content

Commit

Permalink
Switch to TanStack Query
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyfallWasTaken committed Aug 30, 2024
1 parent d6435d9 commit d37b656
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 11 deletions.
Binary file modified frontend/bun.lockb
Binary file not shown.
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
},
"dependencies": {
"@faker-js/faker": "^8.4.1",
"@tanstack/svelte-query": "^5.52.3",
"lucide-svelte": "^0.394.0",
"ofetch": "^1.3.4",
"postal-mime": "^2.2.7"
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/lib/components/mailbox/Mailbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
{/each}
</Accordion>
{:else}
<p class="text-lg font-semibold">No emails... yet</p>
<h2 class="text-lg font-semibold">No emails... yet</h2>
<p>You're all caught up! Go out and do what you do best.</p>
{/if}
</div>
Expand Down
20 changes: 16 additions & 4 deletions frontend/src/routes/+layout.svelte
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
<script>
import { browser } from '$app/environment';
import Header from '$lib/components/Header.svelte';
import { QueryClient, QueryClientProvider } from '@tanstack/svelte-query';
import '../app.css';
const queryClient = new QueryClient({
defaultOptions: {
queries: {
enabled: browser
}
}
});
</script>

<Header />
<main class="mt-20 px-5 md:px-3 lg:px-0">
<slot />
</main>
<QueryClientProvider client={queryClient}>
<Header />
<main class="mt-20 px-5 md:px-3 lg:px-0">
<slot />
</main>
</QueryClientProvider>
30 changes: 24 additions & 6 deletions frontend/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@
import { CopyIcon, CopyCheckIcon } from 'lucide-svelte';
import { username } from '$lib/stores/mailbox';
import { ofetch } from 'ofetch';
import { goto } from '$app/navigation';
import { createQuery } from '@tanstack/svelte-query';
import { debounce } from '$lib/util';
import type { Email } from '$lib/email';
const emailDomain = import.meta.env.VITE_EMAIL_DOMAIN as string;
function refreshPage() {
goto(window.location.href);
window.location.reload();
}
function setUsername(event: KeyboardEvent) {
Expand All @@ -25,6 +26,16 @@
copyButtonState = 'idle';
}, 1000);
}
const query = createQuery({
queryKey: ['emails', username, emailDomain],
queryFn: async () => {
const response = await ofetch<Email[]>(
`${import.meta.env.VITE_API_ENDPOINT}/emails/${$username}@${emailDomain}`
);
return response;
}
});
</script>

<svelte:head>
Expand Down Expand Up @@ -59,15 +70,22 @@
<hr />

<div>
{#await ofetch(`${import.meta.env.VITE_API_ENDPOINT}/emails/${$username}@${emailDomain}`)}
{#if $query.isLoading}
<div
class="light-bg dark:bg-surface-500 flex items-center justify-center rounded-md p-6 shadow-sm"
>
<p class="text-lg font-semibold">One sec...</p>
</div>
{:then emails}
<Mailbox {emails} />
{/await}
{:else if $query.isError}
<div
class="light-bg dark:bg-surface-500 flex items-center justify-center rounded-md p-6 shadow-sm"
>
<h2 class="text-lg font-semibold">Uh oh, something went wrong</h2>
<p>Sorry about that! Please refresh the page and try again.</p>
</div>
{:else if $query.isSuccess}
<Mailbox emails={$query.data} />
{/if}
<p class="my-4 text-gray-400">
Hint: <a href="/" class="text-sky-400 hover:underline" on:click={refreshPage}
>refresh the page</a
Expand Down

0 comments on commit d37b656

Please sign in to comment.