59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import type { Compilation, Artist, Track } from '~/../types/types'
|
|
|
|
// stores/data.ts
|
|
import { defineStore } from 'pinia'
|
|
|
|
export const useDataStore = defineStore('data', {
|
|
state: () => ({
|
|
compilations: [] as Compilation[], // Store your compilation data here
|
|
artists: [] as Artist[], // Store artist data here
|
|
tracks: [] as Track[], // Store track data here
|
|
isLoaded: false, // Remember if data is already loaded
|
|
}),
|
|
|
|
actions: {
|
|
async loadData() {
|
|
if (this.isLoaded) return
|
|
|
|
const { data: compilations } = await useFetch<Compilation[]>('/api/compilations')
|
|
const { data: artists } = await useFetch<Artist[]>('/api/artists')
|
|
const { data: rawTracks } = await useFetch<{ id: number, compilationId: string, title: string, artist: number, start: number, url: string, coverId: string }[]>('/api/tracks')
|
|
|
|
// Stocker les données de base
|
|
this.compilations = compilations.value ?? []
|
|
this.artists = artists.value ?? []
|
|
|
|
// Mapper les tracks pour remplacer l'artistId par l'objet Artist
|
|
const artistMap = new Map(this.artists.map(a => [a.id, a]))
|
|
|
|
this.tracks = (rawTracks.value ?? []).map(track => ({
|
|
...track,
|
|
artist: artistMap.get(track.artist) ?? { id: track.artist, name: 'Unknown', url: '', coverId: '' }
|
|
}))
|
|
|
|
this.isLoaded = true
|
|
}
|
|
},
|
|
|
|
getters: {
|
|
// Obtenir tous les compilations
|
|
getAllCompilations: (state) => state.compilations,
|
|
getCompilationById: (state) => {
|
|
return (id: string) => {
|
|
return state.compilations.find(compilation => compilation.id === id)
|
|
}
|
|
},
|
|
// Obtenir toutes les pistes d'une compilation donnée
|
|
getTracksByCompilationId: (state) => (compilationId: string) => {
|
|
return state.tracks.filter(track => track.compilationId === compilationId)
|
|
},
|
|
// Filtrer les artistes selon certains critères
|
|
getArtistById: (state) => (id: number) => state.artists.find(artist => artist.id === id),
|
|
|
|
// Obtenir toutes les pistes d'un artiste donné
|
|
getTracksByArtistId: (state) => (artistId: number) => {
|
|
return state.tracks.filter(track => track.artist.id === artistId)
|
|
},
|
|
},
|
|
})
|