52 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			52 lines
		
	
	
		
			1.8 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  // Avoid re-fetching if already loaded
 | |
| 
 | |
|       // Fetch your data once (e.g., from an API or local JSON)
 | |
|       const { data: compilations } = await useFetch('/api/compilations')
 | |
|       const { data: artists } = await useFetch('/api/artists')
 | |
|       const { data: tracks } = await useFetch('/api/tracks')
 | |
| 
 | |
|       // Set the data in the store
 | |
|       this.compilations = compilations.value
 | |
|       this.artists = artists.value
 | |
|       this.tracks = tracks.value
 | |
|       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: string) => {
 | |
|       return state.tracks.filter(track => track.artistId === artistId)
 | |
|     },
 | |
|   },
 | |
| })
 |