|
| 1 | +<script setup> |
| 2 | +/** UI */ |
| 3 | +import Button from "@/components/ui/Button.vue" |
| 4 | +
|
| 5 | +/** Services */ |
| 6 | +import { capitilize } from "~/services/utils" |
| 7 | +
|
| 8 | +/** Store */ |
| 9 | +import { useBookmarksStore } from "@/store/bookmarks" |
| 10 | +import { useCacheStore } from "@/store/cache" |
| 11 | +import { useModalsStore } from "@/store/modals" |
| 12 | +import { useNotificationsStore } from "@/store/notifications" |
| 13 | +const bookmarksStore = useBookmarksStore() |
| 14 | +const cacheStore = useCacheStore() |
| 15 | +const modalsStore = useModalsStore() |
| 16 | +const notificationsStore = useNotificationsStore() |
| 17 | +
|
| 18 | +const props = defineProps({ |
| 19 | + type: { |
| 20 | + type: String, |
| 21 | + required: true, |
| 22 | + }, |
| 23 | + id: { |
| 24 | + type: [String, Number], |
| 25 | + required: true, |
| 26 | + }, |
| 27 | +}) |
| 28 | +
|
| 29 | +const router = useRouter() |
| 30 | +
|
| 31 | +const bookmark = ref(null) |
| 32 | +const isBookmarked = ref(false) |
| 33 | +const isButtonHovered = ref(false) |
| 34 | +const bookmarkText = computed(() => { |
| 35 | + if (isButtonHovered.value && isBookmarked.value) return "Remove" |
| 36 | +
|
| 37 | + return isBookmarked.value ? "Saved" : "Save" |
| 38 | +}) |
| 39 | +
|
| 40 | +const handleBookmark = () => { |
| 41 | + if (!isBookmarked.value) { |
| 42 | +
|
| 43 | + let newBookmark = { |
| 44 | + id: props.id, |
| 45 | + type: capitilize(props.type), |
| 46 | + ts: new Date().getTime(), |
| 47 | + } |
| 48 | +
|
| 49 | + isBookmarked.value = bookmarksStore.addBookmark(newBookmark) |
| 50 | +
|
| 51 | + if (isBookmarked.value) { |
| 52 | + notificationsStore.create({ |
| 53 | + notification: { |
| 54 | + type: "success", |
| 55 | + icon: "check", |
| 56 | + title: `${capitilize(props.type)} added to bookmarks`, |
| 57 | + description: "View all bookmarks on dedicated page", |
| 58 | + autoDestroy: true, |
| 59 | + actions: [ |
| 60 | + { |
| 61 | + name: "Open Bookmarks", |
| 62 | + callback: () => { |
| 63 | + router.push("/bookmarks") |
| 64 | + }, |
| 65 | + }, |
| 66 | + ], |
| 67 | + }, |
| 68 | + }) |
| 69 | +
|
| 70 | + cacheStore.current.bookmark = newBookmark |
| 71 | + modalsStore.open("edit_alias") |
| 72 | + } |
| 73 | +
|
| 74 | + } else { |
| 75 | + let notification = {} |
| 76 | +
|
| 77 | + if (bookmarksStore.removeBookmark(props.type, props.id)) { |
| 78 | + notification = { |
| 79 | + type: "success", |
| 80 | + icon: "check", |
| 81 | + title: `${capitilize(props.type)} removed from bookmarks`, |
| 82 | + autoDestroy: true, |
| 83 | + } |
| 84 | +
|
| 85 | + isBookmarked.value = false |
| 86 | + } else { |
| 87 | + notification = { |
| 88 | + type: "error", |
| 89 | + icon: "close", |
| 90 | + title: `Failed to remove the bookmark`, |
| 91 | + autoDestroy: true, |
| 92 | + } |
| 93 | + } |
| 94 | +
|
| 95 | + notificationsStore.create({ |
| 96 | + notification: notification, |
| 97 | + }) |
| 98 | + } |
| 99 | +} |
| 100 | +
|
| 101 | +onMounted(() => { |
| 102 | + isBookmarked.value = bookmarksStore.getBookmark(props.type, props.id) ? true : false |
| 103 | +}) |
| 104 | +
|
| 105 | +</script> |
| 106 | + |
| 107 | +<template> |
| 108 | + <Flex align="center" gap="8"> |
| 109 | + <Button |
| 110 | + @click="handleBookmark" |
| 111 | + @mouseenter="isButtonHovered = true" |
| 112 | + @mouseleave="isButtonHovered = false" |
| 113 | + type="secondary" |
| 114 | + size="mini" |
| 115 | + > |
| 116 | + <Icon |
| 117 | + :name="isButtonHovered && isBookmarked ? 'close' : isBookmarked ? 'bookmark-check' : 'bookmark-plus'" |
| 118 | + size="12" |
| 119 | + :color="isBookmarked && !isButtonHovered ? 'green' : 'primary'" |
| 120 | + /> |
| 121 | + {{ bookmarkText }} |
| 122 | + </Button> |
| 123 | + </Flex> |
| 124 | +</template> |
| 125 | + |
| 126 | +<style module> |
| 127 | +.items { |
| 128 | + overflow: hidden; |
| 129 | +} |
| 130 | +
|
| 131 | +.item { |
| 132 | + & a { |
| 133 | + display: flex; |
| 134 | +
|
| 135 | + &:hover { |
| 136 | + & span { |
| 137 | + color: var(--txt-primary); |
| 138 | + } |
| 139 | + } |
| 140 | + } |
| 141 | +} |
| 142 | +</style> |
0 commit comments