43 lines
1.5 KiB
TypeScript
43 lines
1.5 KiB
TypeScript
interface imagesList {
|
|
[key: string]: {
|
|
status: 'idle' | 'pending' | 'success' | 'error',
|
|
result?: string
|
|
}
|
|
}
|
|
export const useAuthorsStore = defineStore('authors', {
|
|
state: () => ({ list: {} as imagesList }),
|
|
actions: {
|
|
getItem(name: string) {
|
|
if (!this.list[name]) {
|
|
this.list[name] = {
|
|
status: 'idle',
|
|
}
|
|
}
|
|
if (
|
|
!Object.entries(this.list).filter(el => el[1].status == 'pending').length &&
|
|
Object.entries(this.list).filter(el => el[1].status == 'idle').length) {
|
|
this.loadItems()
|
|
}
|
|
return this.list[name]
|
|
},
|
|
async loadOneItem(name: string) {
|
|
const result_data = await apiCall<string[]>(`tgbot_items/get_name/${name}/`)
|
|
if (result_data.length > 0 && result_data[0] !== null) {
|
|
this.list[name].status = 'success'
|
|
this.list[name].result = result_data[0]
|
|
} else {
|
|
this.list[name].status = 'error'
|
|
}
|
|
if (Object.entries(this.list).filter(el => el[1].status == 'idle').length) {
|
|
this.loadItems()
|
|
}
|
|
},
|
|
loadItems() {
|
|
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.loadOneItem(el[0])
|
|
})
|
|
}
|
|
},
|
|
}) |