aboutsummaryrefslogtreecommitdiff
path: root/src/components/FavoriteButton.vue
blob: ef180a6572255bcda31f58c17a6076753c1db918 (plain) (blame)
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
<script setup lang="ts">
import { FavoriteFilled } from '@vicons/material';

import { useToolStore } from '@/tools/tools.store';
import type { Tool } from '@/tools/tools.types';

const props = defineProps<{ tool: Tool }>();

const toolStore = useToolStore();

const { tool } = toRefs(props);

const isFavorite = computed(() => toolStore.isToolFavorite({ tool }));
const buttonType = computed(() => (isFavorite.value ? 'primary' : 'default'));

function toggleFavorite(event: MouseEvent) {
  event.preventDefault();

  if (toolStore.isToolFavorite({ tool })) {
    toolStore.removeToolFromFavorites({ tool });
    return;
  }

  toolStore.addToolToFavorites({ tool });
}
</script>

<template>
  <n-tooltip trigger="hover">
    <template #trigger>
      <c-button
        variant="text"
        circle
        :type="buttonType"
        :style="{ opacity: isFavorite ? 1 : 0.2 }"
        @click="toggleFavorite"
      >
        <n-icon :component="FavoriteFilled" />
      </c-button>
    </template>
    {{ isFavorite ? 'Remove from favorites' : 'Add to favorites' }}
  </n-tooltip>
</template>