client/src/stores/assets.ts
2024-09-19 01:34:07 +02:00

53 lines
1.4 KiB
TypeScript

import { defineStore } from 'pinia'
import { type Asset } from '@/types'
import config from '@/config'
export const useAssetStore = defineStore('assets', {
state: () => ({
assets: [] as Asset[]
}),
actions: {
setAssets(assets: Asset[]) {
this.assets = assets
},
async fetchAssets() {
return fetch(config.server_endpoint + '/assets')
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
},
fetchAssetsByZoneId2(zoneId: number, successCallback, errorCallback) {
fetch(config.server_endpoint + '/assets/' + zoneId)
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
successCallback();
return true
})
.catch((error) => {
errorCallback();
console.error('Error fetching assets:', error)
return false
})
},
async fetchAssetsByZoneId(zoneId: number) {
return fetch(config.server_endpoint + '/assets/' + zoneId)
.then((response) => response.json())
.then((assets) => {
this.setAssets(assets)
return true
})
.catch((error) => {
console.error('Error fetching assets:', error)
return false
})
}
}
})