|
3 | 3 | import { Button } from './ui/button';
|
4 | 4 | import ItemAction from './ItemAction.svelte';
|
5 | 5 | import * as Select from '$lib/components/ui/select';
|
6 |
| - import type { Item } from '$lib/api/model'; |
| 6 | + import * as Pagination from '$lib/components/ui/pagination'; |
| 7 | + import type { Feed, Item } from '$lib/api/model'; |
| 8 | + import { listItems, type ListFilter } from '$lib/api/item'; |
| 9 | + import { toast } from 'svelte-sonner'; |
| 10 | + import { allFeeds as fetchAllFeeds } from '$lib/api/feed'; |
7 | 11 |
|
8 |
| - export let data: Item[]; |
9 |
| - $: allFeeds = getFeeds(data); |
10 |
| - let selectedFeed = 'all'; |
11 |
| - $: filteredItems = filterFeed(data, selectedFeed); |
| 12 | + export let filter: ListFilter = { offset: 0, count: 10 }; |
12 | 13 |
|
13 |
| - function getFeeds(allItems: Item[]) { |
14 |
| - const feeds = new Map<number, { id: number; name: string }>(); |
15 |
| - allItems.map((v) => feeds.set(v.feed.id, v.feed)); |
16 |
| - return Array.from(feeds.values()); |
17 |
| - } |
| 14 | + if (filter.offset === undefined) filter.offset = 0; |
| 15 | + if (filter.count === undefined) filter.count = 10; |
| 16 | +
|
| 17 | + fetchAllFeeds() |
| 18 | + .then((v) => { |
| 19 | + allFeeds = v; |
| 20 | + }) |
| 21 | + .catch((e) => { |
| 22 | + toast.error('Failed to fetch feeds data: ' + e); |
| 23 | + }); |
| 24 | +
|
| 25 | + let data: Item[] = []; |
| 26 | + let allFeeds: Feed[] = []; |
| 27 | + let currentPage = 1; |
| 28 | + let total = 0; |
| 29 | +
|
| 30 | + $: filter.offset = (currentPage - 1) * (filter?.count || 10); |
| 31 | + $: fetchItems(filter); |
18 | 32 |
|
19 |
| - function filterFeed(allItems: Item[], feedID: string) { |
20 |
| - if (feedID === 'all') return allItems; |
21 |
| - return allItems.filter((v) => v.feed.id === parseInt(feedID)); |
| 33 | + async function fetchItems(filter: ListFilter) { |
| 34 | + try { |
| 35 | + const resp = await listItems(filter); |
| 36 | + data = resp.items; |
| 37 | + total = resp.total; |
| 38 | + } catch (e) { |
| 39 | + toast.error((e as Error).message); |
| 40 | + } |
22 | 41 | }
|
23 | 42 | </script>
|
24 | 43 |
|
|
27 | 46 | items={allFeeds.map((v) => {
|
28 | 47 | return { value: v.id.toString(), label: v.name };
|
29 | 48 | })}
|
30 |
| - onSelectedChange={(v) => v && (selectedFeed = v.value)} |
| 49 | + onSelectedChange={(v) => { |
| 50 | + if (!v) return; |
| 51 | + const feedID = parseInt(v.value); |
| 52 | + filter.feed_id = feedID > 0 ? feedID : undefined; |
| 53 | + filter.offset = 0; |
| 54 | + }} |
31 | 55 | >
|
32 | 56 | <Select.Trigger class="w-[180px]">
|
33 | 57 | <Select.Value placeholder="Filter by Feed" />
|
|
40 | 64 | </Select.Content>
|
41 | 65 | </Select.Root>
|
42 | 66 | </div>
|
| 67 | + |
43 | 68 | <ul class="mt-4">
|
44 |
| - {#each filteredItems as item} |
| 69 | + {#each data as item} |
45 | 70 | <li class="group rounded-md">
|
46 | 71 | <Button
|
47 | 72 | href={'/items?id=' + item.id}
|
|
60 | 85 | </div>
|
61 | 86 |
|
62 | 87 | <div class="w-full hidden group-hover:inline-flex justify-end">
|
63 |
| - <ItemAction |
64 |
| - data={{ id: item.id, link: item.link, unread: item.unread, bookmark: item.bookmark }} |
65 |
| - /> |
| 88 | + <ItemAction bind:data={item} /> |
66 | 89 | </div>
|
67 | 90 | </div>
|
68 | 91 | </Button>
|
|
71 | 94 | No data
|
72 | 95 | {/each}
|
73 | 96 | </ul>
|
| 97 | + |
| 98 | +{#if total > (filter?.count || 10)} |
| 99 | + <Pagination.Root |
| 100 | + count={total} |
| 101 | + perPage={filter.count} |
| 102 | + bind:page={currentPage} |
| 103 | + let:pages |
| 104 | + let:currentPage |
| 105 | + class="mt-8" |
| 106 | + > |
| 107 | + <Pagination.Content class="flex-wrap"> |
| 108 | + <Pagination.Item> |
| 109 | + <Pagination.PrevButton /> |
| 110 | + </Pagination.Item> |
| 111 | + {#each pages as page (page.key)} |
| 112 | + {#if page.type === 'ellipsis'} |
| 113 | + <Pagination.Item> |
| 114 | + <Pagination.Ellipsis /> |
| 115 | + </Pagination.Item> |
| 116 | + {:else} |
| 117 | + <Pagination.Item> |
| 118 | + <Pagination.Link {page} isActive={currentPage == page.value}> |
| 119 | + {page.value} |
| 120 | + </Pagination.Link> |
| 121 | + </Pagination.Item> |
| 122 | + {/if} |
| 123 | + {/each} |
| 124 | + <Pagination.Item> |
| 125 | + <Pagination.NextButton /> |
| 126 | + </Pagination.Item> |
| 127 | + </Pagination.Content> |
| 128 | + </Pagination.Root> |
| 129 | +{/if} |
0 commit comments