Skip to content

Commit 35d0c3b

Browse files
committedFeb 12, 2023
SveltKit-ify routing
1 parent ad1d6f0 commit 35d0c3b

14 files changed

+71
-45
lines changed
 
File renamed without changes.

‎src/HealthLink.svelte ‎src/lib/HealthLink.svelte

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
66
import type { Writable } from 'svelte/store';
77
import type { SHLAdminParams, SHLClient } from './managementClient';
8+
import { assets } from '$internal/paths';
89
910
export let shl: SHLAdminParams;
1011
let shlStore: Writable<SHLAdminParams[]> = getContext('shlStore');
@@ -83,7 +84,7 @@
8384
<li class="logo">
8485
<span class="show">Present SMART Health Link as QR</span>
8586
<img class="qr" alt="QR Code for SHL" src={dataUrl} />
86-
<img class="logo" alt="SMART Logo" src={'./smart-logo.svg'} />
87+
<img class="logo" alt="SMART Logo" src={assets + '/smart-logo.svg'} />
8788
</li>
8889
{/await}
8990
<li>

‎src/app.d.ts ‎src/lib/app.d.ts

File renamed without changes.

‎src/config.ts ‎src/lib/config.ts

File renamed without changes.
File renamed without changes.
File renamed without changes.

‎src/types.ts ‎src/lib/types.ts

File renamed without changes.

‎src/util.ts ‎src/lib/util.ts

File renamed without changes.

‎src/routes/+layout.svelte

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,32 @@
11
<script lang="ts">
2-
import { Col, Container, Icon, Row, Styles } from 'sveltestrap';
3-
import { browser } from '$app/environment';
2+
import { goto } from '$app/navigation';
43
import { setContext } from 'svelte';
54
import { writable } from 'svelte/store';
65
import {
76
ButtonDropdown,
7+
Col,
8+
Container,
89
DropdownItem,
910
DropdownMenu,
1011
DropdownToggle,
12+
Icon,
1113
Navbar,
12-
NavbarBrand
14+
NavbarBrand,
15+
Row,
16+
Styles
1317
} from 'sveltestrap';
14-
import { SHLClient, type SHLAdminParams } from '../managementClient';
18+
import { SHLClient, type SHLAdminParams } from '$lib/managementClient';
1519
1620
const LOCAL_STORAGE_KEY = 'shlips_store_shls';
1721
let shlStore = writable<SHLAdminParams[]>(
18-
browser && window.localStorage[LOCAL_STORAGE_KEY]
19-
? JSON.parse(window.localStorage[LOCAL_STORAGE_KEY])
20-
: []
22+
window.localStorage[LOCAL_STORAGE_KEY] ? JSON.parse(window.localStorage[LOCAL_STORAGE_KEY]) : []
2123
);
2224
2325
let selectedShl = writable<number | undefined>($shlStore.length > 0 ? 0 : undefined);
2426
setContext('selectedShl', selectedShl);
2527
2628
$: {
27-
if (browser && $shlStore) window.localStorage[LOCAL_STORAGE_KEY] = JSON.stringify($shlStore);
29+
if ($shlStore) window.localStorage[LOCAL_STORAGE_KEY] = JSON.stringify($shlStore);
2830
}
2931
3032
setContext('shlStore', shlStore);
@@ -45,16 +47,16 @@
4547
<ButtonDropdown size="sm">
4648
<DropdownToggle color="primary" caret>Actions...</DropdownToggle>
4749
<DropdownMenu>
48-
<DropdownItem on:click={() => ($selectedShl = undefined)}>Add New SHLink</DropdownItem>
49-
<DropdownItem on:click={() => $reset++}>Reset Demo</DropdownItem>
50+
<DropdownItem href="/create">Add New SHLink</DropdownItem>
51+
<DropdownItem
52+
on:click={() => delete window.localStorage[LOCAL_STORAGE_KEY] && goto('/')}
53+
>Reset Demo</DropdownItem
54+
>
5055
{#if $shlStore.length > 0}
5156
<DropdownItem divider />
5257
<DropdownItem header>Stored SHLinks</DropdownItem>
5358
{#each $shlStore as shl, i}
54-
<DropdownItem
55-
on:click={() => {
56-
$selectedShl = i;
57-
}}
59+
<DropdownItem href={'/view/' + shl.id}
5860
><Icon name="eye" />
5961
{shl.label || `SHLink ${i + 1}`}</DropdownItem
6062
>

‎src/routes/+layout.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
export const prerender = true;
1+
export const prerender = false;
2+
export const ssr = false;

‎src/routes/+page.svelte

+6-28
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,14 @@
11
<script lang="ts">
2-
import { browser } from '$app/environment';
2+
import { goto } from '$app/navigation';
3+
import type { SHLAdminParams, SHLClient } from '$lib/managementClient';
34
import { getContext } from 'svelte';
45
import type { Writable } from 'svelte/store';
5-
import AddFile from '../AddFile.svelte';
6-
import HealthLink from '../HealthLink.svelte';
7-
import type { SHLAdminParams, SHLClient } from '../managementClient';
8-
import type { SHCRetrieveEvent } from '../types';
96
107
let shlClient: SHLClient = getContext('shlClient');
118
let shlStore: Writable<SHLAdminParams[]> = getContext('shlStore');
12-
let selectedShl: Writable<number | undefined> = getContext('selectedShl');
13-
let reset: Writable<number> = getContext('reset');
14-
15-
$: if ($reset) $shlStore = [];
16-
17-
async function newShlFromShc(details: SHCRetrieveEvent) {
18-
const shlCreated = await shlClient.createShl();
19-
shlClient.addFile(shlCreated, details.shc, 'application/smart-health-card');
20-
shlCreated.label = details.label;
21-
return shlCreated;
9+
if ($shlStore.length) {
10+
goto("/view/"+$shlStore[0].id)
11+
} else {
12+
goto("/create")
2213
}
2314
</script>
24-
25-
{#if $selectedShl !== undefined}
26-
<HealthLink shl={$shlStore[$selectedShl]} />
27-
{:else if browser}
28-
<AddFile
29-
on:shc-retrieved={async ({ detail }) => {
30-
$shlStore = [...$shlStore, await newShlFromShc(detail)];
31-
$selectedShl = $shlStore.length - 1;
32-
}}
33-
/>
34-
{:else}
35-
Loading
36-
{/if}

‎src/routes/create/+page.svelte

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<script lang="ts">
2+
import { goto } from '$app/navigation';
3+
import { getContext } from 'svelte';
4+
import type { Writable } from 'svelte/store';
5+
import type { SHLAdminParams, SHLClient } from '$lib/managementClient';
6+
import type { SHCRetrieveEvent } from '$lib/types';
7+
import AddFile from '$lib/AddFile.svelte';
8+
9+
let shlClient: SHLClient = getContext('shlClient');
10+
let shlStore: Writable<SHLAdminParams[]> = getContext('shlStore');
11+
12+
async function newShlFromShc(details: SHCRetrieveEvent): Promise<SHLAdminParams> {
13+
const shlCreated = await shlClient.createShl();
14+
shlClient.addFile(shlCreated, details.shc, 'application/smart-health-card');
15+
shlCreated.label = details.label;
16+
return shlCreated;
17+
}
18+
</script>
19+
20+
<AddFile
21+
on:shc-retrieved={async ({ detail }) => {
22+
const newShl = await newShlFromShc(detail);
23+
$shlStore = [...$shlStore, newShl];
24+
goto(`/view/${newShl.id}`);
25+
}}
26+
/>

‎src/routes/view/[id]/+page.svelte

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<script lang="ts">
2+
import { page } from '$app/stores';
3+
import { getContext } from 'svelte';
4+
import type { Writable } from 'svelte/store';
5+
import HealthLink from '$lib/HealthLink.svelte';
6+
import type { SHLAdminParams } from '$lib/managementClient';
7+
let shlStore: Writable<SHLAdminParams[]> = getContext('shlStore');
8+
let shl: SHLAdminParams | undefined;
9+
$: {
10+
shl = $shlStore.filter((s) => s.id === $page.params.id)?.[0];
11+
}
12+
</script>
13+
14+
{#if shl}
15+
<HealthLink {shl} />
16+
{:else}
17+
SHLink {$page.params.id} Not Found
18+
{/if}

‎svelte.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export default {
1212
// these options are set automatically — see below
1313
pages: 'build',
1414
assets: 'build',
15-
fallback: null,
15+
fallback: "404.html",
1616
precompress: false,
1717
strict: true,
1818
paths: {

0 commit comments

Comments
 (0)
Please sign in to comment.