-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use tanstack/query for data fetching (#80)
Closes #48
- Loading branch information
1 parent
d320f23
commit a2d12de
Showing
46 changed files
with
736 additions
and
454 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script lang="ts"> | ||
import { initAuthContext } from './auth-context'; | ||
initAuthContext(); | ||
</script> | ||
|
||
<slot /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
<script lang="ts"> | ||
import { initNavigatingContext } from './navigating-context'; | ||
initNavigatingContext(); | ||
</script> | ||
|
||
<slot /> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
export type APICall = { | ||
fetch: typeof fetch; | ||
url: URL; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,12 @@ | ||
import type { APICall } from '../fetch'; | ||
import { handleAPIError } from '../handle-error'; | ||
|
||
export const getSelf = async ({ fetch: _fetch, url }: APICall): Promise<void> => { | ||
export const getSelf = async ({ fetch: _fetch }: APICall): Promise<string> => { | ||
const res = await _fetch('/api/v1/auth/me'); | ||
|
||
if (!res.ok) { | ||
await handleAPIError(res, url); | ||
await handleAPIError(res); | ||
} | ||
|
||
return 'self'; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
export const queryKeys = { | ||
recipe: { | ||
list: 'list-recipes', | ||
get: (id: string) => ['get-recipe', id], | ||
}, | ||
|
||
tag: { | ||
list: 'list-tags', | ||
}, | ||
|
||
user: { | ||
self: 'get-self', | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { getContext, setContext } from 'svelte'; | ||
import { writable, type Writable } from 'svelte/store'; | ||
|
||
const contextKey = 'mise_auth'; | ||
|
||
export type AuthContext = Writable<{ | ||
unauthenticated: boolean; | ||
}>; | ||
|
||
export function useAuth(): AuthContext { | ||
return getContext<AuthContext>(contextKey); | ||
} | ||
|
||
export function initAuthContext() { | ||
setContext<AuthContext>(contextKey, writable({ unauthenticated: false })); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<script lang="ts"> | ||
import { useAuth } from '$lib/auth-context'; | ||
import PageReauthenticateState from './page-states/PageReauthenticateState.svelte'; | ||
const auth = useAuth(); | ||
</script> | ||
|
||
{#if $auth.unauthenticated} | ||
<PageReauthenticateState /> | ||
{:else} | ||
<slot /> | ||
{/if} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
<script lang="ts"> | ||
import { goto } from '$app/navigation'; | ||
import { useNavigating } from '$lib/navigating-context'; | ||
export let href: string; | ||
export let prefetch: () => Promise<void>; | ||
const navigating = useNavigating(); | ||
let isLoading = false; | ||
async function onClick(e: MouseEvent) { | ||
e.preventDefault(); | ||
isLoading = true; | ||
$navigating.to = href; | ||
try { | ||
await prefetch(); | ||
} catch (error: unknown) { | ||
// ignore any prefetch errors and trigger primary page loading state | ||
console.error('prefetch error: ', error); | ||
} | ||
// only navigate if another navigation hasn't triggered | ||
if ($navigating.to === href) { | ||
await goto(href); | ||
$navigating.to = undefined; | ||
} | ||
isLoading = false; | ||
} | ||
// the navigation is not going to proceed if another navigation has started | ||
$: if ($navigating.to !== undefined && $navigating.to !== href) { | ||
isLoading = false; | ||
} | ||
</script> | ||
|
||
<a {href} on:click={onClick} data-loading={isLoading ? true : undefined} {...$$restProps} | ||
><slot {isLoading} /></a | ||
> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.