52 lines
1.9 KiB
TypeScript
52 lines
1.9 KiB
TypeScript
interface imagesList {
|
|
[key: string]: {
|
|
status: 'idle' | 'pending' | 'success' | 'error',
|
|
type: 'tg' | 'aws',
|
|
result?: string
|
|
}
|
|
}
|
|
export const useImagesStore = defineStore('images', {
|
|
state: () => ({ list: {} as imagesList }),
|
|
actions: {
|
|
getImage(name: string, type: 'tg' | 'aws') {
|
|
if (!this.list[name]) {
|
|
this.list[name] = {
|
|
type,
|
|
status: 'idle',
|
|
}
|
|
}
|
|
if (!Object.values(this.list).filter(el => el.status == 'pending').length) {
|
|
this.loadImages()
|
|
}
|
|
return this.list[name]
|
|
},
|
|
async loadOneImage(name: string) {
|
|
let file_url_data
|
|
|
|
if (this.list[name].type == 'tg') {
|
|
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'
|
|
}
|
|
} else if (this.list[name].type == 'aws') {
|
|
file_url_data = await apiCall<string[]>(`tgbot_items/get_image_s3/${name}/`)
|
|
this.list[name].result = URL.createObjectURL(file_url_data);
|
|
this.list[name].status = 'success'
|
|
}
|
|
|
|
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])
|
|
})
|
|
}
|
|
},
|
|
}) |