File size: 2,949 Bytes
eb29a95
 
 
 
 
 
 
 
 
 
a1d7896
eb29a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a1d7896
 
 
eb29a95
 
 
a1d7896
 
 
eb29a95
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<script lang="ts">
	import { browser } from "$app/environment";
	import InfiniteScroll from "svelte-infinite-scroll";

	import Button from "$lib/components/Button.svelte";
	import Card from "$lib/components/community/Card.svelte";
	import Input from "$lib/components/fields/Input.svelte";
	import Radio from "$lib/components/fields/Radio.svelte";
	import { COMMUNITY_FILTER_OPTIONS } from "$lib/utils/index.js";
	import GoTop from "$lib/components/GoTop.svelte";
	import UserIsLogged from "$lib/components/UserIsLogged.svelte";

	export let data

	let form = {
		filter: "new",
		page: "0",
		search: ""
	}

	$: elementScroll = browser ? document?.getElementById('app') : undefined;

	const handleFetchMore = async () => {
		form = {...form, page: (Number(form.page) + 1).toString()};
		refetch(true);
	}
	const handleChangeFilter = async (filter: string) => {
		form = { ...form, filter, page: (0).toString()};
		refetch(false)
	}
	let timeout: any;
	const handleChangeSearch = async (search: string) => {
		clearTimeout(timeout);
		form = { ...form, search, page: (0).toString()};
		timeout = setTimeout(() => refetch(false), 500);
	}

	const refetch = async (add: boolean) => {
		const request = await fetch(`/api/community?${new URLSearchParams(form)}`);
		const response = await request.json();
		if (add) data = {...data, cards: [...data.cards, ...response.cards ]};
		else data = response;
	}
</script>

<svelte:head>
	<title>Community Gallery</title>
	<meta name="description" content="Svelte demo app" />
</svelte:head>

<main class="px-6 py-10 lg:px-10 lg:py-12">
	<h1 class="text-white font-semibold text-2xl">
		Community Gallery ({data.total_items})
	</h1>
	<div class="flex items-center justify-between mt-5">
		<Radio options={COMMUNITY_FILTER_OPTIONS} value="{form.filter}" onChange={handleChangeFilter} />
		<div class="items-center justify-end gap-5 hidden lg:flex">
			<UserIsLogged>
				<Button icon="ic:round-plus" theme="dark" size="lg">Upload own Image</Button>
			</UserIsLogged>
			<Button icon="fluent:glance-horizontal-sparkles-16-filled" href="/generate" theme="pink" size="lg">Generate</Button>
		</div>
		<div class="items-center justify-end gap-3 flex lg:hidden">
			<UserIsLogged>
				<Button icon="ic:round-plus" theme="dark" size="md">Upload own Image</Button>
			</UserIsLogged>
			<Button icon="fluent:glance-horizontal-sparkles-16-filled" href="/generate" theme="pink" size="md">Generate</Button>
		</div>
	</div>
	<div class="mt-5 max-w-sm">
		<Input value={form.search} placeholder="Search an image" onChange={handleChangeSearch} />
	</div>
	<div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-5 gap-5 mt-8 lg:mt-10">
		{#each data.cards as card}
			<Card card={card} />
		{/each}
		<InfiniteScroll
			elementScroll="{elementScroll ?? undefined}"
			threshold={100}
			hasMore={data.total_items > data.cards.length}
			on:loadMore={handleFetchMore}
		/>
		<GoTop />
	</div>
</main>