Skip to content

Commit b008ddd

Browse files
committed
feat: articles page now includes a field to add an article
1 parent 423c3c2 commit b008ddd

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

src/routes/+page.svelte

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11
<script lang="ts">
2+
import { invalidateAll } from '$app/navigation';
23
import Button from '$lib/components/Button.svelte';
34
import IconButton from '$lib/components/IconButton.svelte';
5+
import TextField from '$lib/components/TextField.svelte';
46
import { onMount } from 'svelte';
57
import Article from './Article.svelte';
68
import ArticleCard from './ArticleCard.svelte';
79
810
let { data } = $props();
911
let isMounted = $state(false);
12+
let loading = $state(false);
13+
let addArticleUrl = $state('');
14+
let addArticleError = $state<string | undefined>(undefined);
1015
1116
onMount(() => {
1217
isMounted = true;
@@ -61,7 +66,7 @@
6166
<main class="gap-x-0 sm:columns-2 xl:columns-3">
6267
{#if data.articles.length > 0}
6368
{#each data.articles as article, index}
64-
<Article {...article} {index} authCookie={data.authCookie} />
69+
<Article {...article} {index} />
6570
{/each}
6671
{:else}
6772
<ArticleCard {isMounted}>
@@ -83,14 +88,40 @@
8388
</Button>
8489
</ArticleCard>
8590

86-
<!-- TODO: enable once add article ui is created -->
8791
<ArticleCard {isMounted}>
8892
<h2 class="flex items-start gap-x-2 font-title text-2xl">Not a developer? No Problem!</h2>
8993

90-
<!-- <form method="POST" use:enhance></form> -->
91-
<!-- <a class="text-justify hover:underline" href="/articles/add">
92-
Click here to add a new article
93-
</a> -->
94+
<div class="flex flex-col gap-y-4">
95+
<TextField
96+
label="Article URL"
97+
required
98+
type="url"
99+
placeholder="https://extra.extra/"
100+
error={addArticleError}
101+
bind:value={addArticleUrl}
102+
/>
103+
104+
<Button
105+
{loading}
106+
onclick={async () => {
107+
loading = true;
108+
const result = await fetch(`/articles/add`, {
109+
method: 'POST',
110+
body: JSON.stringify({ url: addArticleUrl })
111+
});
112+
loading = false;
113+
114+
if (result.status === 200) {
115+
await invalidateAll();
116+
addArticleError = undefined;
117+
} else {
118+
addArticleError = `${await result.text()} - ${result.status}`;
119+
}
120+
}}
121+
>
122+
Add
123+
</Button>
124+
</div>
94125

95126
<!-- TODO: include an option to quickly add an article here by url -->
96127
</ArticleCard>

0 commit comments

Comments
 (0)