File size: 1,786 Bytes
eb29a95
f05d33c
e8410db
eb29a95
e8410db
 
f05d33c
e8410db
 
eb29a95
 
e8410db
eb29a95
 
 
 
 
 
 
7398577
eb29a95
 
 
 
e8410db
eb29a95
 
f05d33c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7398577
f05d33c
 
 
 
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
<script lang="ts">
  import type { ReactionType } from "$lib/type";
  import Add from "$lib/components/community/reactions/Add.svelte";
  import Reaction from "$lib/components/community/reactions/Reaction.svelte";
  import { get } from 'svelte/store';
  import { userStore } from "$lib/stores/use-user";
  import UserIsLogged from "$lib/components/UserIsLogged.svelte";

  let user = get(userStore);

  export let reactions: ReactionType[] = [];
  export let gallery_id: string;

  const groupReactionsByEmoji = (reactions: ReactionType[]) => {
    const grouped = new Set(reactions.map((reaction) => reaction.emoji));
    return Array.from(grouped).map((emoji) => {
      return {
        emoji,
        count: reactions.filter((reaction) => reaction.emoji === emoji).length,
        liked: !!reactions.find((reaction) => reaction.emoji === emoji && reaction.userId === user?.sub)
      };
    });
  };

  $: groupedReactions = groupReactionsByEmoji(reactions);
</script>

<UserIsLogged>
  <div class="flex items-center justify-start gap-2">
    {#each groupedReactions as reaction}
      <Reaction
        emoji={reaction.emoji}
        count={reaction?.count}
        liked={reaction?.liked}
        {gallery_id}
        onReact={(emoji, id, deleted) => {
          if (deleted) {
            reactions = reactions.filter((reaction) => reaction.id !== id);
          } else {
            reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
          }
        }}
      />
    {/each}
    <Add
      count={groupedReactions?.length}
      reactions={groupedReactions}
      {gallery_id}
      onAdd={(emoji, id) => {
        reactions = [...reactions, { emoji, userId: user?.sub, galleryId: gallery_id, id }];
      }}
    />
  </div>
</UserIsLogged>