favorite: v0.1
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import type { Box, Artist, Track } from '~/../types/types'
|
||||
import { FAVORITES_BOX_ID, useFavoritesStore } from '~/store/favorites'
|
||||
|
||||
// stores/data.ts
|
||||
import { defineStore } from 'pinia'
|
||||
@@ -44,6 +45,21 @@ export const useDataStore = defineStore('data', {
|
||||
artist: artistObj
|
||||
}
|
||||
})
|
||||
const favBox: Box = {
|
||||
id: FAVORITES_BOX_ID,
|
||||
type: 'playlist',
|
||||
name: 'Favoris',
|
||||
duration: 0,
|
||||
tracks: [],
|
||||
description: '',
|
||||
color1: '#0f172a',
|
||||
color2: '#1e293b',
|
||||
color3: '#334155',
|
||||
state: 'box-list'
|
||||
}
|
||||
if (!this.boxes.find((b) => b.id === FAVORITES_BOX_ID)) {
|
||||
this.boxes = [favBox, ...this.boxes]
|
||||
}
|
||||
this.isLoaded = true
|
||||
const uiStore = useUiStore()
|
||||
uiStore.closeBox()
|
||||
@@ -59,6 +75,12 @@ export const useDataStore = defineStore('data', {
|
||||
},
|
||||
// Obtenir toutes les pistes d'une box donnée
|
||||
getTracksByboxId: (state) => (id: string) => {
|
||||
if (id === FAVORITES_BOX_ID) {
|
||||
const fav = useFavoritesStore()
|
||||
return fav.trackIds
|
||||
.map((tid) => state.tracks.find((t) => t.id === tid))
|
||||
.filter((t): t is Track => !!t)
|
||||
}
|
||||
return state.tracks.filter((track) => track.boxId === id)
|
||||
},
|
||||
// Filtrer les artistes selon certains critères
|
||||
|
||||
52
app/store/favorites.ts
Normal file
52
app/store/favorites.ts
Normal file
@@ -0,0 +1,52 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import type { Track } from '~/../types/types'
|
||||
|
||||
export const FAVORITES_BOX_ID = 'FAV'
|
||||
const STORAGE_KEY = 'evilspins:favorites:v1'
|
||||
|
||||
export const useFavoritesStore = defineStore('favorites', {
|
||||
state: () => ({
|
||||
trackIds: [] as number[]
|
||||
}),
|
||||
actions: {
|
||||
load() {
|
||||
if (!process.client) return
|
||||
try {
|
||||
const raw = localStorage.getItem(STORAGE_KEY)
|
||||
if (raw) {
|
||||
const arr = JSON.parse(raw)
|
||||
if (Array.isArray(arr)) this.trackIds = arr.filter((x) => typeof x === 'number')
|
||||
}
|
||||
} catch {}
|
||||
},
|
||||
save() {
|
||||
if (!process.client) return
|
||||
try {
|
||||
localStorage.setItem(STORAGE_KEY, JSON.stringify(this.trackIds))
|
||||
} catch {}
|
||||
},
|
||||
toggle(track: Track) {
|
||||
const id = track.id
|
||||
const idx = this.trackIds.indexOf(id)
|
||||
if (idx >= 0) this.trackIds.splice(idx, 1)
|
||||
else this.trackIds.unshift(id)
|
||||
this.save()
|
||||
},
|
||||
add(track: Track) {
|
||||
if (!this.trackIds.includes(track.id)) {
|
||||
this.trackIds.unshift(track.id)
|
||||
this.save()
|
||||
}
|
||||
},
|
||||
remove(trackId: number) {
|
||||
const idx = this.trackIds.indexOf(trackId)
|
||||
if (idx >= 0) {
|
||||
this.trackIds.splice(idx, 1)
|
||||
this.save()
|
||||
}
|
||||
},
|
||||
isFavorite(trackId: number) {
|
||||
return this.trackIds.includes(trackId)
|
||||
}
|
||||
}
|
||||
})
|
||||
Reference in New Issue
Block a user