| 1 | <template> |
| 2 | <Icon |
| 3 | :name="loadingBookmark ? LoadingIcon : isBookmark ? StarActiveIcon : StarIcon" |
| 4 | :size="16" |
| 5 | class="toggler-bookmark" |
| 6 | :class="{ active: isBookmark }" |
| 7 | @click.stop="toggleBookmark()" |
| 8 | /> |
| 9 | </template> |
| 10 | |
| 11 | <script setup lang="ts"> |
| 12 | import type { SocAlert } from "@/types/soc/alert.d" |
| 13 | import { useMessage } from "naive-ui" |
| 14 | import { ref, toRefs } from "vue" |
| 15 | import Api from "@/api" |
| 16 | import Icon from "@/components/common/Icon.vue" |
| 17 | |
| 18 | const props = defineProps<{ |
| 19 | alert: SocAlert |
| 20 | isBookmark?: boolean |
| 21 | }>() |
| 22 | |
| 23 | const emit = defineEmits<{ |
| 24 | (e: "bookmark", value: boolean): void |
| 25 | }>() |
| 26 | |
| 27 | const { alert, isBookmark } = toRefs(props) |
| 28 | |
| 29 | const StarActiveIcon = "carbon:star-filled" |
| 30 | const StarIcon = "carbon:star" |
| 31 | const LoadingIcon = "eos-icons:loading" |
| 32 | |
| 33 | const loadingBookmark = ref(false) |
| 34 | const message = useMessage() |
| 35 | |
| 36 | function toggleBookmark() { |
| 37 | if (alert.value?.alert_id) { |
| 38 | loadingBookmark.value = true |
| 39 | |
| 40 | const method = isBookmark.value ? "removeAlertBookmark" : "addAlertBookmark" |
| 41 | |
| 42 | Api.soc[method](alert.value.alert_id.toString()) |
| 43 | .then(res => { |
| 44 | if (res.data.success) { |
| 45 | emit("bookmark", method !== "removeAlertBookmark") |
| 46 | message.success(res.data?.message || "Stream started.") |
| 47 | } else { |
| 48 | message.warning(res.data?.message || "An error occurred. Please try again later.") |
| 49 | } |
| 50 | }) |
| 51 | .catch(err => { |
| 52 | message.error(err.response?.data?.message || "An error occurred. Please try again later.") |
| 53 | }) |
| 54 | .finally(() => { |
| 55 | loadingBookmark.value = false |
| 56 | }) |
| 57 | } |
| 58 | } |
| 59 | </script> |
| 60 | |
| 61 | <style lang="scss" scoped> |
| 62 | .toggler-bookmark { |
| 63 | &.active { |
| 64 | color: var(--primary-color); |
| 65 | } |
| 66 | &:hover { |
| 67 | color: var(--primary-color); |
| 68 | } |
| 69 | } |
| 70 | </style> |