FIX: form was being sent and also "submitted". Prefilled values gave errors when not set.

This commit is contained in:
Juan Pablo Vial
2025-12-03 19:15:17 -03:00
parent d89c1b5815
commit fd357ca1c1

View File

@ -145,7 +145,7 @@
</span> </span>
</div> </div>
<button class="ui button" type="submit">Agregar</button> <button class="ui button" type="submit" id="submit_button">Agregar</button>
</form> </form>
</div> </div>
@endsection @endsection
@ -885,14 +885,22 @@
venta.fill.payments() venta.fill.payments()
}, },
date: () => { date: () => {
const date = new Date('{{ $date?->format('Y-m-d') }}') const dateString = '{{ isset($date) ? ($date?->format('Y-m-d') ?? '') : '' }}'
if (dateString === '') {
return;
}
const date = new Date(dateString)
date.setDate(date.getDate() + 1) date.setDate(date.getDate() + 1)
venta.components.$date.calendar('set date', date) venta.components.$date.calendar('set date', date)
}, },
buyer: () => { buyer: () => {
const fullRut = '{{ isset($propietario) ? $propietario['full'] : '' }}'
if (fullRut === '') {
return;
}
const buyer = $(venta.ids.buyerId) const buyer = $(venta.ids.buyerId)
const rut = buyer.find('#rut') const rut = buyer.find('#rut')
rut.val('{{ $propietario['full'] }}') rut.val(fullRut)
let persona = venta.components.buyer.components.persona let persona = venta.components.buyer.components.persona
if (!('components' in persona)) { if (!('components' in persona)) {
persona = persona.persona persona = persona.persona
@ -928,7 +936,11 @@
} }
}, },
project: () => { project: () => {
$(venta.ids.projectId).dropdown('set selected', {{ $proyecto_id }}) const projectId = {{ isset($proyecto_id) ? $proyecto_id : 0 }};
if (projectId === 0) {
return;
}
$(venta.ids.projectId).dropdown('set selected', projectId);
const promise = venta.components.project.promises.unidades const promise = venta.components.project.promises.unidades
if (promise === null) { if (promise === null) {
@ -940,7 +952,11 @@
}) })
}, },
units: () => { units: () => {
const unitTypes = JSON.parse('{!! json_encode($unidades) !!}') const unitString = '{!! isset($unidades) ? json_encode($unidades) : '' !!}';
if (unitString === '') {
return;
}
const unitTypes = JSON.parse(unitString)
Object.entries(unitTypes).forEach(([type, units]) => { Object.entries(unitTypes).forEach(([type, units]) => {
units.forEach(unit_id => { units.forEach(unit_id => {
const unit = venta.components.project.add(type) const unit = venta.components.project.add(type)
@ -949,7 +965,11 @@
}) })
}, },
payments: () => { payments: () => {
const formaPago = JSON.parse('{!! json_encode($forma_pago) !!}') const formaPagoString = '{!! isset($forma_pago) ? json_encode($forma_pago) : '' !!}';
if (formaPagoString === '') {
return;
}
const formaPago = JSON.parse(formaPagoString)
Object.entries(formaPago).forEach(([key, value]) => { Object.entries(formaPago).forEach(([key, value]) => {
const payment = venta.components.payments[key] const payment = venta.components.payments[key]
const event = new Event('change', { const event = new Event('change', {
@ -975,6 +995,37 @@
} }
} }
}, },
submit: form => {
const body = new FormData(form)
const unidades = form.querySelectorAll("[name^='unidad']")
const emptyUnidades = []
unidades.forEach(unidad => {
if (unidad.value.trim() === '') {
emptyUnidades.push(unidad)
body.delete(unidad.name)
}
})
if (unidades.length === emptyUnidades.length) {
alert("No hay unidades seleccionadas")
return;
}
const uri = '{{$urls->api}}/ventas/add'
return APIClient.fetch(uri, {method: 'post', body}).then(response => {
if (!response) {
return false
}
return response.json().then(data => {
if (data.status) {
window.location = '{{$urls->base}}/venta/' + data.venta_id
return true
}
button.prop('disabled', false)
button.css('cursor', 'pointer')
showErrors(data.errors)
return false
})
})
},
setup({dateId, buyerId, buyerTypeId, buyerNId, unitsId, projectId, fromReservation = false}) { setup({dateId, buyerId, buyerTypeId, buyerNId, unitsId, projectId, fromReservation = false}) {
this.ids = { this.ids = {
dateId, dateId,
@ -1008,39 +1059,22 @@
this.components.$form = $(this.ids.form) this.components.$form = $(this.ids.form)
this.components.$form.submit(event => { this.components.$form.submit(event => {
event.preventDefault() event.preventDefault();
const button = $(event.currentTarget).find(".ui.button[type='submit']") const button = $(event.currentTarget).find(".ui.button[type='submit']");
button.prop('disabled', true) button.prop('disabled', true);
button.css('cursor', 'wait') button.css('cursor', 'wait');
const body = new FormData(event.currentTarget) const form = event.currentTarget;
const unidades = event.currentTarget.querySelectorAll("[name^='unidad']") venta.submit(form);
const emptyUnidades = [] return false;
unidades.forEach(unidad => { })
if (unidad.value.trim() === '') { document.getElementById('submit_button').addEventListener('click', clickEvent => {
emptyUnidades.push(unidad) clickEvent.preventDefault();
body.delete(unidad.name) const button = $(clickEvent.currentTarget)
} button.prop('disabled', true);
}) button.css('cursor', 'wait');
if (unidades.length === emptyUnidades.length) { const form = document.getElementById('add_form')
alert("No hay unidades seleccionadas") venta.submit(form)
return; return false;
}
const uri = '{{$urls->api}}/ventas/add'
return APIClient.fetch(uri, {method: 'post', body}).then(response => {
if (!response) {
return false
}
return response.json().then(data => {
if (data.status) {
window.location = '{{$urls->base}}/venta/' + data.venta_id
return true
}
button.prop('disabled', false)
button.css('cursor', 'pointer')
showErrors(data.errors)
return false
})
})
}) })
if (fromReservation) { if (fromReservation) {