diff --git a/src/components/AlbumView/Header.vue b/src/components/AlbumView/Header.vue index e66eb7e..cb6365c 100644 --- a/src/components/AlbumView/Header.vue +++ b/src/components/AlbumView/Header.vue @@ -50,7 +50,7 @@
- +
@@ -85,14 +85,15 @@ import useNavStore from "@/stores/nav"; import useAlbumStore from "@/stores/pages/album"; import { formatSeconds, useVisibility } from "@/utils"; import { isLight } from "@/composables/colors/album"; -import { playSources } from "@/composables/enums"; +import { favType, playSources } from "@/composables/enums"; import { Album } from "@/interfaces"; import { Routes } from "@/router/routes"; import HeartSvg from "../shared/HeartSvg.vue"; import PlayBtnRect from "../shared/PlayBtnRect.vue"; +import favoriteHandler from "@/composables/favoriteHandler"; -defineProps<{ +const props = defineProps<{ album: Album; }>(); @@ -115,6 +116,12 @@ function handleVisibilityState(state: boolean) { } useVisibility(albumheaderthing, handleVisibilityState); + +const is_fav = ref(props.album.is_favorite); + +function handleFav() { + favoriteHandler(is_fav, favType.album, props.album.albumhash); +} diff --git a/src/components/shared/SongItem.vue b/src/components/shared/SongItem.vue index 2b134d9..c84902a 100644 --- a/src/components/shared/SongItem.vue +++ b/src/components/shared/SongItem.vue @@ -14,7 +14,7 @@ {{ index }}
- +
@@ -83,10 +83,10 @@ import OptionSvg from "@/assets/icons/more.svg"; import ArtistName from "./ArtistName.vue"; import useQueueStore from "@/stores/queue"; -import { addFavorite, removeFavorite } from "@/composables/fetch/favorite"; import { favType } from "@/composables/enums"; import MasterFlag from "./MasterFlag.vue"; +import favoriteHandler from "@/composables/favoriteHandler"; const imguri = paths.images.thumb.small; const context_menu_showing = ref(false); @@ -120,24 +120,10 @@ function isCurrentPlaying() { return isCurrent() && queue.playing; } -const fav = ref(props.track.is_favorite); +const is_fav = ref(props.track.is_favorite); -async function addToFav(trackhash: string) { - if (fav.value) { - const removed = await removeFavorite(favType.track, trackhash); - - if (removed) { - fav.value = false; - } - - return; - } - - const added = await addFavorite(favType.track, trackhash); - - if (added) { - fav.value = true; - } +function addToFav(trackhash: string) { + favoriteHandler(is_fav, favType.track, trackhash); } diff --git a/src/composables/favoriteHandler.ts b/src/composables/favoriteHandler.ts new file mode 100644 index 0000000..a0df540 --- /dev/null +++ b/src/composables/favoriteHandler.ts @@ -0,0 +1,31 @@ +import { Ref } from "vue"; +import { favType } from "./enums"; +import { addFavorite, removeFavorite } from "./fetch/favorite"; + +/** + * Handles the favorite state of an item. + * @param flag The ref to track the is_favorite state + * @param type The type of item + * @param itemhash The hash of the item + */ +export default async function favoriteHandler( + flag: Ref, + type: favType, + itemhash: string +) { + if (flag.value) { + const removed = await removeFavorite(type, itemhash); + + if (removed) { + flag.value = false; + } + + return; + } + + const added = await addFavorite(type, itemhash); + + if (added) { + flag.value = true; + } +} diff --git a/src/interfaces.ts b/src/interfaces.ts index c9bce20..120ff22 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -54,6 +54,7 @@ export interface Album { is_soundtrack: boolean; is_single: boolean; is_EP: boolean; + is_favorite: boolean; genres: string[]; } diff --git a/src/stores/pages/artistDiscog.ts b/src/stores/pages/artistDiscog.ts index 7855c8b..481de45 100644 --- a/src/stores/pages/artistDiscog.ts +++ b/src/stores/pages/artistDiscog.ts @@ -43,7 +43,7 @@ export default defineStore("artistDiscography", { this.page = page; }, fetchAlbums(artisthash: string) { - getArtistAlbums(artisthash, true) + getArtistAlbums(artisthash, 0, true) .then((data) => { this.albums = data.albums; this.eps = data.eps;