Load api data once

This commit is contained in:
Juan Pablo Vial
2025-11-12 17:31:55 -03:00
parent c99690e7b6
commit e278695137
2 changed files with 176 additions and 23 deletions

View File

@ -89,7 +89,8 @@
this.components.form.querySelector(`input[name="${this.prefix}_project_id"]`).value = project_id
this.get.brokers(project_id)
this.get.promotions(project_id).then(promotions => {
this.get.promotions(project_id).then(() => {
const promotions = reservations.data.promotions[project_id]
this.components.promotions.data.promotions = promotions.map(promotion => {
return {
text: promotion.description,
@ -99,7 +100,8 @@
})
this.components.promotions.draw.promotions()
})
this.get.units(project_id).then(unitTypes => {
this.get.units(project_id).then(() => {
const unitTypes = reservations.data.units[project_id]
Object.entries(unitTypes).map(([type, units]) => {
units = units.map(unit => {
return {
@ -132,7 +134,15 @@
}
return
}
const uri = `/api/region/${region_id}/comunas`
return reservations.get.comunas(region_id).then(comunas => {
this.data.comunas[region_id] = comunas
this.components.$comuna.dropdown('change values', this.data.comunas[region_id])
if (this.data.current_user !== null && this.data.current_user?.direccion?.comuna !== null) {
this.components.$comuna.dropdown('set selected', this.data.current_user.direccion.comuna.id)
}
})
/*const uri = `/api/region/${region_id}/comunas`
return APIClient.fetch(uri).then(response => response.json()).then(json => {
if (json.comunas.length === 0) {
return
@ -149,7 +159,7 @@
if (this.data.current_user !== null && this.data.current_user?.direccion?.comuna !== null) {
this.components.$comuna.dropdown('set selected', this.data.current_user.direccion.comuna.id)
}
})
})*/
},
brokers: project_id => {
if (project_id in this.data.brokers) {
@ -157,7 +167,11 @@
resolve(this.data.brokers[project_id])
})
}
const uri = `/api/proyecto/${project_id}/brokers`
return reservations.get.brokers(project_id).then(brokers => {
this.data.brokers[project_id] = brokers
this.fill.brokers()
})
/*const uri = `/api/proyecto/${project_id}/brokers`
return APIClient.fetch(uri).then(response => response.json()).then(json => {
if (json.contracts.length === 0) {
return
@ -187,7 +201,7 @@
})
this.fill.brokers()
})
})
})*/
},
broker: (broker_rut) => {
const uri = `/api/proyectos/broker/${broker_rut}`
@ -204,13 +218,16 @@
resolve(this.data.promotions[project_id])
})
}
const uri = `/api/proyecto/${project_id}/promotions`
return reservations.get.promotions(project_id).then(promotions => {
this.data.promotions[project_id] = promotions
})
/*const uri = `/api/proyecto/${project_id}/promotions`
return APIClient.fetch(uri).then(response => response.json()).then(json => {
if (json.promotions.length === 0) {
return this.data.promotions[project_id] = []
}
return this.data.promotions[project_id] = json.promotions
})
})*/
},
units: project_id => {
if (project_id in this.data.units) {
@ -218,7 +235,10 @@
resolve(this.data.units[project_id])
})
}
const uri = `/api/proyecto/${project_id}/unidades/disponibles`
return reservations.get.units(project_id).then(units => {
this.data.units[project_id] = units
})
/*const uri = `/api/proyecto/${project_id}/unidades/disponibles`
return APIClient.fetch(uri).then(response => response.json()).then(json => {
if (json.unidades.length === 0) {
this.data.units[project_id] = {}
@ -236,7 +256,7 @@
})
return this.data.units[project_id]
})
})*/
},
user: rut => {
if (this.data.current_user !== null && this.data.current_user?.rut === rut) {