Search update and optimization

This commit is contained in:
2023-11-23 23:35:19 -03:00
parent bf03e85975
commit e1ef31dccd
10 changed files with 232 additions and 36 deletions

View File

@ -45,6 +45,7 @@
draw() {
const tipo = this.unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
let unidad = tipo.charAt(0).toUpperCase() + tipo.slice(1) + ' ' + this.unidad.descripcion
let precio = 0
let propietario = ''
let fecha = ''
let fecha_entrega = ''
@ -61,8 +62,10 @@
if (typeof this.venta.entrega !== 'undefined') {
fecha_entrega = dateFormatter.format(new Date(this.venta.entrega.fecha))
}
precio = this.venta.valor
} else {
unidad += '<i class="ban icon"></i>'
precio = this.unidad.current_precio.valor
}
const numberFormat = new Intl.NumberFormat('es-CL', {minimumFractionDigits: 2, maximumFractionDigits: 2})
@ -81,7 +84,7 @@
).append(
$('<td></td>').addClass('right aligned').html(superficie + ' m&#0178;')
).append(
$('<td></td>').addClass('right aligned').html(numberFormat.format(this.unidad.precio))
$('<td></td>').addClass('right aligned').html(numberFormat.format(precio))
).append(
$('<td></td>').html(fecha)
).append(
@ -104,29 +107,84 @@
const uri = '{{$urls->api}}/search'
this.data = []
return fetch(uri, {method: 'post', body: data}).then(response => {
this.draw().clear()
if (response.ok) {
return response.json()
}
}).catch(error => {
this.draw().clear()
this.draw().error(error)
}).then(data => {
if (typeof data.results !== 'undefined' && data.results.length > 0) {
data.results.forEach(row => {
if (typeof row.proyecto_tipo_unidad === 'undefined') {
const r = new Row({unidad: row.propiedad.departamentos[0], proyecto: row.proyecto})
r.venta = row
this.data.push(r)
} else {
this.data.push(new Row({unidad: row, proyecto: row.proyecto_tipo_unidad.proyecto}))
}
})
this.draw().table()
this.draw().clear()
if (typeof data.results === 'undefined' || data.results.length === 0) {
this.draw().empty()
return
}
this.draw().empty()
const progress = this.draw().progress(data.results.length)
const promises = []
data.results.forEach(row => {
if (row.tipo === 'venta') {
promises.push(this.get().venta(row.id).then(json => {
const venta = json.venta
progress.progress('increment')
const r = new Row({unidad: venta.propiedad.unidades[0], proyecto: venta.proyecto})
r.venta = venta
this.data.push(r)
}).catch(error => {
progress.progress('increment')
console.error(row)
console.error(error)
}))
return
}
promises.push(this.get().unidad(row.id).then(json => {
const unidad = json.unidad
progress.progress('increment')
this.data.push(new Row({unidad: unidad, proyecto: unidad.proyecto_tipo_unidad.proyecto}))
}).catch(error => {
progress.progress('increment')
console.error(row)
console.error(error)
}))
})
Promise.all(promises).then(() => {
this.sort()
this.draw().clear()
this.draw().table()
})
})
},
unidad: id => {
const url = '{{$urls->api}}/ventas/unidad/' + id
return fetch(url).then(response => {
if (response.ok) {
return response.json()
}
})
},
venta: id => {
const url = '{{$urls->api}}/venta/' + id
return fetch(url).then(response => {
if (response.ok) {
return response.json()
}
})
}
}
},
sort: function() {
this.data.sort((a, b) => {
const p = a.proyecto.descripcion.localeCompare(b.proyecto.descripcion)
if (p === 0) {
const t = a.unidad.proyecto_tipo_unidad.tipo_unidad.descripcion
.localeCompare(b.unidad.proyecto_tipo_unidad.tipo_unidad.descripcion)
if (t === 0) {
return a.unidad.descripcion.localeCompare(b.unidad.descripcion)
}
return t
}
return p
})
},
draw: function() {
return {
clear: () => {
@ -213,6 +271,35 @@
$('<div></div>').addClass('content').html('No se han encontrado resultados.')
)
)
},
error: error => {
this.draw().separator()
$(this.id).append(
$('<div></div>').addClass('ui icon error message').append(
$('<i></i>').addClass('exclamation triangle icon')
).append(
$('<div></div>').addClass('content').html(error)
)
)
},
progress: cantidad => {
this.draw().separator()
const progress = $('<div></div>').addClass('ui active progress').append(
$('<div></div>').addClass('bar').append(
$('<div></div>').addClass('centered progress')
)
).append(
$('<div></div>').addClass('label').html('Cargando datos')
)
progress.progress({
total: cantidad,
label: 'ratio',
text: {
ratio: '{value} de {total} ({percent}%)'
}
})
$(this.id).append(progress)
return progress
}
}
},