43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
interface imagesList {
|
|
[key: string]: {
|
|
status: 'idle' | 'pending' | 'success' | 'error',
|
|
result?: string
|
|
}
|
|
}
|
|
export const useImagesStore = defineStore('images', {
|
|
state: () => ({ list: {} as imagesList }),
|
|
actions: {
|
|
getImage(name: string) {
|
|
if (!this.list[name]) {
|
|
this.list[name] = {
|
|
status: 'idle',
|
|
// result: 'https://media.istockphoto.com/id/1490517357/photo/scientists-are-researching-and-analyzing-harmful-contaminants-in-the-laboratory.webp?s=170667a&w=0&k=20&c=Fh4t-P_b-a1QxwyBUzUa0AuLp8FLNyLy4hl4HUm82Ao='
|
|
}
|
|
}
|
|
if (!Object.values(this.list).filter(el => el.status == 'pending').length) {
|
|
this.loadImages()
|
|
}
|
|
return this.list[name]
|
|
},
|
|
async loadOneImage(name: string) {
|
|
const file_url_data = await apiCall<string[]>(`tgbot/items/get_image/${name}/`)
|
|
|
|
if (file_url_data.length > 0 && file_url_data[0] !== null) {
|
|
this.list[name].status = 'success'
|
|
this.list[name].result = file_url_data[0]
|
|
} else {
|
|
this.list[name].status = 'error'
|
|
}
|
|
if (Object.entries(this.list).filter(el => el[1].status == 'idle').length) {
|
|
this.loadImages()
|
|
}
|
|
},
|
|
loadImages() {
|
|
const elements = Object.entries(this.list).filter(el => el[1].status == 'idle')
|
|
elements.slice(0, 2).map(el => {
|
|
this.list[el[0]].status = 'pending'
|
|
this.loadOneImage(el[0])
|
|
})
|
|
}
|
|
},
|
|
}) |