client/src/stores/assets.ts

36 lines
852 B
TypeScript

import { defineStore } from 'pinia'
import { type Asset, type Object } from '@/types'
import config from '@/config'
export const useAssetStore = defineStore('assets', {
state: () => ({
assets: [] as Asset[],
tiles: [] as string[],
objects: [] as Object[]
}),
actions: {
setAssets(assets: Asset[]) {
this.assets = assets
},
addAsset(asset: Asset) {
this.assets.push(asset)
},
setTiles(tiles: string[]) {
this.tiles = tiles
},
setObjects(objects: Object[]) {
this.objects = objects
},
fetchAssets() {
fetch(config.server_endpoint + '/assets')
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
})
.catch((error) => {
console.error('Error fetching assets:', error)
})
}
}
})