|
1 | 1 | <script lang="ts">
|
| 2 | + import { invalidateAll } from '$app/navigation'; |
2 | 3 | import Button from '$lib/components/Button.svelte';
|
3 | 4 | import IconButton from '$lib/components/IconButton.svelte';
|
| 5 | + import TextField from '$lib/components/TextField.svelte'; |
4 | 6 | import { onMount } from 'svelte';
|
5 | 7 | import Article from './Article.svelte';
|
6 | 8 | import ArticleCard from './ArticleCard.svelte';
|
7 | 9 |
|
8 | 10 | let { data } = $props();
|
9 | 11 | let isMounted = $state(false);
|
| 12 | + let loading = $state(false); |
| 13 | + let addArticleUrl = $state(''); |
| 14 | + let addArticleError = $state<string | undefined>(undefined); |
10 | 15 |
|
11 | 16 | onMount(() => {
|
12 | 17 | isMounted = true;
|
|
61 | 66 | <main class="gap-x-0 sm:columns-2 xl:columns-3">
|
62 | 67 | {#if data.articles.length > 0}
|
63 | 68 | {#each data.articles as article, index}
|
64 |
| - <Article {...article} {index} authCookie={data.authCookie} /> |
| 69 | + <Article {...article} {index} /> |
65 | 70 | {/each}
|
66 | 71 | {:else}
|
67 | 72 | <ArticleCard {isMounted}>
|
|
83 | 88 | </Button>
|
84 | 89 | </ArticleCard>
|
85 | 90 |
|
86 |
| - <!-- TODO: enable once add article ui is created --> |
87 | 91 | <ArticleCard {isMounted}>
|
88 | 92 | <h2 class="flex items-start gap-x-2 font-title text-2xl">Not a developer? No Problem!</h2>
|
89 | 93 |
|
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> |
94 | 125 |
|
95 | 126 | <!-- TODO: include an option to quickly add an article here by url -->
|
96 | 127 | </ArticleCard>
|
|
0 commit comments