enzostvs HF staff commited on
Commit
3b75cc4
β€’
1 Parent(s): f3ad218

load data client side

Browse files
src/routes/+page.svelte CHANGED
@@ -3,7 +3,7 @@
3
  import InfiniteScroll from "svelte-infinite-scroll";
4
  import { page } from "$app/stores";
5
 
6
- import { goto, invalidate } from "$app/navigation";
7
  import Button from "$lib/components/Button.svelte";
8
  import Card from "$lib/components/models/Card.svelte";
9
  import Input from "$lib/components/fields/Input.svelte";
@@ -13,42 +13,55 @@
13
  import Dialog from "$lib/components/dialog/Dialog.svelte";
14
  import SubmitModel from "$lib/components/models/Submit.svelte";
15
  import Drawer from "$lib/components/models/drawer/Drawer.svelte";
 
 
16
 
17
- export let data
18
-
19
- $: form = {
20
- filter: data?.filter,
21
- search: data?.search,
 
 
 
 
 
 
22
  }
23
- let current_page = "0";
24
  let submitModelDialog = false;
25
 
 
 
 
 
26
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
27
 
28
  const handleFetchMore = async () => {
29
- current_page = (Number(current_page) + 1).toString();
30
  refetch(true);
31
  }
32
  const handleChangeFilter = async (filter: string) => {
 
33
  $page.url.searchParams.set('filter', filter);
34
  await goto(`?${$page.url.searchParams.toString()}`);
35
- await invalidate(url => url.pathname.includes("/api/models"));
36
  }
37
  let timeout: any;
38
  const handleChangeSearch = async (search: string) => {
39
  clearTimeout(timeout);
 
40
  timeout = setTimeout(async () => {
41
  if (search === "") $page.url.searchParams.delete('search');
42
  else $page.url.searchParams.set('search', search);
43
  await goto(`?${$page.url.searchParams.toString()}`);
44
- await invalidate(url => url.pathname.includes("/api/models"));
45
  }, 500);
46
  }
47
 
48
  const refetch = async (add: boolean) => {
49
- const request = await fetch(`/api/models?${new URLSearchParams({...form, page: current_page })}`);
50
  const response = await request.json();
51
- if (add) data = {...data, models: [...data.models, ...response.cards]};
52
  else {
53
  data.models = response.cards;
54
  data.total_items = response.total_items;
 
3
  import InfiniteScroll from "svelte-infinite-scroll";
4
  import { page } from "$app/stores";
5
 
6
+ import { goto } from "$app/navigation";
7
  import Button from "$lib/components/Button.svelte";
8
  import Card from "$lib/components/models/Card.svelte";
9
  import Input from "$lib/components/fields/Input.svelte";
 
13
  import Dialog from "$lib/components/dialog/Dialog.svelte";
14
  import SubmitModel from "$lib/components/models/Submit.svelte";
15
  import Drawer from "$lib/components/models/drawer/Drawer.svelte";
16
+ import { onMount } from "svelte";
17
+ import type { ModelCard } from "$lib/type";
18
 
19
+ let data: {
20
+ models: ModelCard[],
21
+ total_items: number,
22
+ } = {
23
+ models: [],
24
+ total_items: 0,
25
+ }
26
+ let form: Record<string, string> = {
27
+ filter: $page.url.searchParams.get('filter') ?? "hotest",
28
+ search: "",
29
+ page: "0"
30
  }
 
31
  let submitModelDialog = false;
32
 
33
+ onMount(() => {
34
+ refetch(false);
35
+ });
36
+
37
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
38
 
39
  const handleFetchMore = async () => {
40
+ form.page = (Number(form.page) + 1).toString();
41
  refetch(true);
42
  }
43
  const handleChangeFilter = async (filter: string) => {
44
+ form.filter = filter;
45
  $page.url.searchParams.set('filter', filter);
46
  await goto(`?${$page.url.searchParams.toString()}`);
47
+ refetch(false);
48
  }
49
  let timeout: any;
50
  const handleChangeSearch = async (search: string) => {
51
  clearTimeout(timeout);
52
+ form.search = search;
53
  timeout = setTimeout(async () => {
54
  if (search === "") $page.url.searchParams.delete('search');
55
  else $page.url.searchParams.set('search', search);
56
  await goto(`?${$page.url.searchParams.toString()}`);
57
+ refetch(false);
58
  }, 500);
59
  }
60
 
61
  const refetch = async (add: boolean) => {
62
+ const request = await fetch(`/api/models?${new URLSearchParams({...form })}`);
63
  const response = await request.json();
64
+ if (add) data.models = [...data.models, ...response.cards] as any[];
65
  else {
66
  data.models = response.cards;
67
  data.total_items = response.total_items;
src/routes/+page.ts CHANGED
@@ -3,8 +3,6 @@ import { modelStore } from "$lib/stores/use-model";
3
  /** @type {import('./$types').PageServerLoad} */
4
  export async function load({ fetch, url }) {
5
  const model_param = url.searchParams.get("model")
6
- const search_param = url.searchParams.get("search") ?? undefined
7
- const filter_param = url.searchParams.get("filter") ?? "hotest"
8
 
9
  if (model_param) {
10
  const model_request = await fetch(`/api/models/${model_param?.replace("/", "@")}?full=true`, {
@@ -19,18 +17,6 @@ export async function load({ fetch, url }) {
19
  open: true
20
  });
21
  }
22
-
23
- const response = await fetch(`/api/models${url.search}`, {
24
- method: "GET",
25
- headers: {
26
- "Content-Type": "application/json"
27
- }
28
- })
29
- const models = await response.json()
30
  return {
31
- models: models?.cards ?? [],
32
- total_items: models?.total_items ?? 0,
33
- search: search_param ?? "",
34
- filter: filter_param,
35
  }
36
  }
 
3
  /** @type {import('./$types').PageServerLoad} */
4
  export async function load({ fetch, url }) {
5
  const model_param = url.searchParams.get("model")
 
 
6
 
7
  if (model_param) {
8
  const model_request = await fetch(`/api/models/${model_param?.replace("/", "@")}?full=true`, {
 
17
  open: true
18
  });
19
  }
 
 
 
 
 
 
 
 
20
  return {
 
 
 
 
21
  }
22
  }
src/routes/gallery/+page.svelte CHANGED
@@ -11,9 +11,17 @@
11
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
12
  import { get } from "svelte/store";
13
  import { userStore } from "$lib/stores/use-user";
 
 
14
 
15
- export let data
16
  let user = get(userStore);
 
 
 
 
 
 
 
17
 
18
  let form = {
19
  filter: "new",
@@ -21,6 +29,10 @@
21
  search: ""
22
  }
23
 
 
 
 
 
24
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
25
 
26
  const handleFetchMore = async () => {
 
11
  import GalleryDrawer from "$lib/components/community/drawer/Drawer.svelte";
12
  import { get } from "svelte/store";
13
  import { userStore } from "$lib/stores/use-user";
14
+ import { onMount } from "svelte";
15
+ import type { CommunityCard } from "$lib/type";
16
 
 
17
  let user = get(userStore);
18
+ let data: {
19
+ cards: CommunityCard[],
20
+ total_items: number,
21
+ } = {
22
+ cards: [],
23
+ total_items: 0,
24
+ }
25
 
26
  let form = {
27
  filter: "new",
 
29
  search: ""
30
  }
31
 
32
+ onMount(() => {
33
+ refetch(false);
34
+ });
35
+
36
  $: elementScroll = browser ? document?.getElementById('app') : undefined;
37
 
38
  const handleFetchMore = async () => {
src/routes/gallery/+page.ts DELETED
@@ -1,10 +0,0 @@
1
- export async function load({ fetch }) {
2
- const response = await fetch("/api/community?page=0&filter=new", {
3
- method: "GET",
4
- headers: {
5
- "Content-Type": "application/json"
6
- }
7
- })
8
- const data = await response.json()
9
- return data
10
- }