-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathBookmarkButton.vue
More file actions
139 lines (121 loc) · 2.85 KB
/
Copy pathBookmarkButton.vue
File metadata and controls
139 lines (121 loc) · 2.85 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<script setup>
/** UI */
import Button from "@/components/ui/Button.vue"
/** Services */
import { capitilize } from "@/services/utils"
/** Store */
import { useBookmarksStore } from "@/store/bookmarks.store"
import { useCacheStore } from "@/store/cache.store"
import { useModalsStore } from "@/store/modals.store"
import { useNotificationsStore } from "@/store/notifications.store"
const bookmarksStore = useBookmarksStore()
const cacheStore = useCacheStore()
const modalsStore = useModalsStore()
const notificationsStore = useNotificationsStore()
const props = defineProps({
type: {
type: String,
required: true,
},
id: {
type: [String, Number],
required: true,
},
})
const router = useRouter()
const bookmark = ref(null)
const isBookmarked = ref(false)
const isButtonHovered = ref(false)
const bookmarkText = computed(() => {
if (isButtonHovered.value && isBookmarked.value) return "Remove"
return isBookmarked.value ? "Saved" : "Save"
})
const handleBookmark = () => {
if (!isBookmarked.value) {
let newBookmark = {
id: props.id,
type: capitilize(props.type),
ts: new Date().getTime(),
}
isBookmarked.value = bookmarksStore.addBookmark(newBookmark)
if (isBookmarked.value) {
notificationsStore.create({
notification: {
type: "success",
icon: "check",
title: `${capitilize(props.type)} added to bookmarks`,
description: "View all bookmarks on dedicated page",
autoDestroy: true,
actions: [
{
name: "Open Bookmarks",
callback: () => {
router.push("/bookmarks")
},
},
],
},
})
cacheStore.current.bookmark = newBookmark
modalsStore.open("edit_alias")
}
} else {
let notification = {}
if (bookmarksStore.removeBookmark(props.type, props.id)) {
notification = {
type: "success",
icon: "check",
title: `${capitilize(props.type)} removed from bookmarks`,
autoDestroy: true,
}
isBookmarked.value = false
} else {
notification = {
type: "error",
icon: "close",
title: `Failed to remove the bookmark`,
autoDestroy: true,
}
}
notificationsStore.create({
notification: notification,
})
}
}
onMounted(() => {
isBookmarked.value = bookmarksStore.getBookmark(props.type, props.id) ? true : false
})
</script>
<template>
<Flex align="center" gap="8">
<Button
@click="handleBookmark"
@mouseenter="isButtonHovered = true"
@mouseleave="isButtonHovered = false"
type="secondary"
size="mini"
>
<Icon
:name="isButtonHovered && isBookmarked ? 'close' : isBookmarked ? 'bookmark-check' : 'bookmark-plus'"
size="12"
:color="isBookmarked && !isButtonHovered ? 'green' : 'primary'"
/>
{{ bookmarkText }}
</Button>
</Flex>
</template>
<style module>
.items {
overflow: hidden;
}
.item {
& a {
display: flex;
&:hover {
& span {
color: var(--txt-primary);
}
}
}
}
</style>