enzostvs HF staff commited on
Commit
35140b4
β€’
1 Parent(s): 3d344de

user is able to delete his own generations

Browse files
src/lib/components/community/Card.svelte CHANGED
@@ -2,13 +2,19 @@
2
  import { env } from "$env/dynamic/public";
3
  import { goto } from "$app/navigation";
4
  import { page } from "$app/stores";
 
5
 
6
  import type { CommunityCard } from "$lib/type";
7
  import { galleryStore } from "$lib/stores/use-gallery";
 
8
  import Reactions from "./reactions/Reactions.svelte";
9
 
10
  export let card: CommunityCard;
11
- export let form: Record<string, string>;
 
 
 
 
12
 
13
  const handleClick = async () => {
14
  const request = await fetch(`/api/community/${card?.id}?${new URLSearchParams(form)}`);
@@ -23,6 +29,13 @@
23
  $page.url.searchParams.set('gallery', card?.id);
24
  goto(`?${$page.url.searchParams.toString()}`);
25
  };
 
 
 
 
 
 
 
26
  </script>
27
 
28
  <!-- svelte-ignore a11y-no-static-element-interactions -->
@@ -31,7 +44,7 @@
31
  class="cursor-pointer group bg-neutral-700 rounded-xl h-[400px] relative flex items-start justify-between flex-col p-5 transition-all duration-200 brightness-90 hover:brightness-100 z-[1] overflow-hidden"
32
  on:click={handleClick}
33
  >
34
- <div class="w-full h-full absolute top-0 left-0 -z-[1] rounded-xl overflow-hidden">
35
  <div class="w-full h-full bg-center bg-cover transition-all duration-200 group-hover:scale-110 " style="background-image: url('{env.PUBLIC_FILE_UPLOAD_DIR}/{card.image}');"></div>
36
  </div>
37
  <div class="group-hover:opacity-100 opacity-0 translate-y-full group-hover:translate-y-0 transition-all duration-200 flex flex-col gap-4 w-full">
@@ -40,5 +53,15 @@
40
  <p class="text-white/75 font-regular text-base">{card.model.id}</p>
41
  </div>
42
  </div>
43
- <Reactions reactions={card.reactions} gallery_id={card.id} />
 
 
 
 
 
 
 
 
 
 
44
  </div>
 
2
  import { env } from "$env/dynamic/public";
3
  import { goto } from "$app/navigation";
4
  import { page } from "$app/stores";
5
+ import Icon from "@iconify/svelte";
6
 
7
  import type { CommunityCard } from "$lib/type";
8
  import { galleryStore } from "$lib/stores/use-gallery";
9
+ import Loading from "$lib/components/Loading.svelte";
10
  import Reactions from "./reactions/Reactions.svelte";
11
 
12
  export let card: CommunityCard;
13
+ export let form: Record<string, string> | undefined = undefined;
14
+ export let displayReactions: boolean = true;
15
+ export let onDelete: ((id: string) => Promise<void>) | undefined = undefined;
16
+
17
+ let loading = false;
18
 
19
  const handleClick = async () => {
20
  const request = await fetch(`/api/community/${card?.id}?${new URLSearchParams(form)}`);
 
29
  $page.url.searchParams.set('gallery', card?.id);
30
  goto(`?${$page.url.searchParams.toString()}`);
31
  };
32
+
33
+ const handleDelete = async (id: string) => {
34
+ if (loading || !onDelete) return;
35
+ loading = true
36
+ await onDelete?.(id);
37
+ loading = false;
38
+ }
39
  </script>
40
 
41
  <!-- svelte-ignore a11y-no-static-element-interactions -->
 
44
  class="cursor-pointer group bg-neutral-700 rounded-xl h-[400px] relative flex items-start justify-between flex-col p-5 transition-all duration-200 brightness-90 hover:brightness-100 z-[1] overflow-hidden"
45
  on:click={handleClick}
46
  >
47
+ <div class="w-full h-full absolute top-0 left-0 -z-[1] rounded-xl overflow-hidden" class:!brightness-50={loading}>
48
  <div class="w-full h-full bg-center bg-cover transition-all duration-200 group-hover:scale-110 " style="background-image: url('{env.PUBLIC_FILE_UPLOAD_DIR}/{card.image}');"></div>
49
  </div>
50
  <div class="group-hover:opacity-100 opacity-0 translate-y-full group-hover:translate-y-0 transition-all duration-200 flex flex-col gap-4 w-full">
 
53
  <p class="text-white/75 font-regular text-base">{card.model.id}</p>
54
  </div>
55
  </div>
56
+ {#if displayReactions}
57
+ <Reactions reactions={card.reactions} gallery_id={card.id} />
58
+ {/if}
59
+ {#if onDelete}
60
+ <button class="absolute bottom-3 right-3 p-2.5 rounded-full bg-red-500 backdrop-blur-sm transition-all duration-200 hover:bg-red-700" on:click={() => handleDelete(card.id)}>
61
+ <Icon icon="ic:round-delete" class="text-white w-5 h-5" />
62
+ </button>
63
+ {/if}
64
+ {#if loading}
65
+ <Loading />
66
+ {/if}
67
  </div>
src/routes/api/community/[id]/+server.ts CHANGED
@@ -1,5 +1,6 @@
1
  import { json, type RequestEvent } from '@sveltejs/kit';
2
  import prisma from '$lib/prisma';
 
3
 
4
  /** @type {import('./$types').RequestHandler} */
5
 
@@ -116,3 +117,44 @@ export async function GET({ params, url } : RequestEvent) {
116
  previous: previous ? previous.id : undefined,
117
  })
118
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import { json, type RequestEvent } from '@sveltejs/kit';
2
  import prisma from '$lib/prisma';
3
+ import { tokenIsAvailable } from '$lib/utils';
4
 
5
  /** @type {import('./$types').RequestHandler} */
6
 
 
117
  previous: previous ? previous.id : undefined,
118
  })
119
  }
120
+
121
+ export async function DELETE({ params, cookies }: RequestEvent) {
122
+ const id = params.id;
123
+
124
+ const token = cookies.get('hf_access_token')
125
+ if (!token) {
126
+ return json({
127
+ error: "You must be logged"
128
+ }, { status: 401 })
129
+ }
130
+
131
+ const is_token_available = await tokenIsAvailable(token)
132
+ if (!is_token_available) {
133
+ return json({
134
+ error: "Invalid token"
135
+ }, { status: 401 })
136
+ }
137
+
138
+ const gallery = await prisma.gallery.findFirst({
139
+ where: {
140
+ id,
141
+ userId: is_token_available.sub
142
+ }
143
+ })
144
+
145
+ if (!gallery) {
146
+ return json({
147
+ error: "Gallery not found"
148
+ }, { status: 404 })
149
+ }
150
+
151
+ await prisma.gallery.delete({
152
+ where: {
153
+ id
154
+ }
155
+ })
156
+
157
+ return json({
158
+ success: true
159
+ })
160
+ }
src/routes/saved-generations/+page.svelte CHANGED
@@ -1,12 +1,21 @@
1
  <script lang="ts">
2
- import { browser } from "$app/environment";
3
-
4
  import Card from "$lib/components/community/Card.svelte";
5
  import GoTop from "$lib/components/GoTop.svelte";
6
 
7
  export let data
8
 
9
- $: elementScroll = browser ? document?.getElementById('app') : undefined;
 
 
 
 
 
 
 
 
 
 
 
10
  </script>
11
 
12
  <svelte:head>
@@ -20,7 +29,7 @@
20
  </h1>
21
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
22
  {#each data.cards as card}
23
- <Card card={card} />
24
  {/each}
25
  <GoTop />
26
  </div>
 
1
  <script lang="ts">
 
 
2
  import Card from "$lib/components/community/Card.svelte";
3
  import GoTop from "$lib/components/GoTop.svelte";
4
 
5
  export let data
6
 
7
+ const handleDelete = async (id: string) => {
8
+ const request = await fetch(`/api/community/${id}`, {
9
+ method: "DELETE"
10
+ });
11
+ const { success } = await request.json();
12
+ if (success) {
13
+ data = {
14
+ ...data,
15
+ cards: data.cards.filter((card: any) => card.id !== id)
16
+ }
17
+ }
18
+ }
19
  </script>
20
 
21
  <svelte:head>
 
29
  </h1>
30
  <div class="mx-auto grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 2xl:grid-cols-4 gap-5 mt-8 lg:mt-10">
31
  {#each data.cards as card}
32
+ <Card card={card} displayReactions={false} onDelete={handleDelete} />
33
  {/each}
34
  <GoTop />
35
  </div>