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>
</div>
<button class="ui button" type="submit">Agregar</button>
<button class="ui button" type="submit" id="submit_button">Agregar</button>
</form>
</div>
@endsection
@ -885,14 +885,22 @@
venta.fill.payments()
},
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)
venta.components.$date.calendar('set date', date)
},
buyer: () => {
const fullRut = '{{ isset($propietario) ? $propietario['full'] : '' }}'
if (fullRut === '') {
return;
}
const buyer = $(venta.ids.buyerId)
const rut = buyer.find('#rut')
rut.val('{{ $propietario['full'] }}')
rut.val(fullRut)
let persona = venta.components.buyer.components.persona
if (!('components' in persona)) {
persona = persona.persona
@ -928,7 +936,11 @@
}
},
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
if (promise === null) {
@ -940,7 +952,11 @@
})
},
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]) => {
units.forEach(unit_id => {
const unit = venta.components.project.add(type)
@ -949,7 +965,11 @@
})
},
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]) => {
const payment = venta.components.payments[key]
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}) {
this.ids = {
dateId,
@ -1008,39 +1059,22 @@
this.components.$form = $(this.ids.form)
this.components.$form.submit(event => {
event.preventDefault()
const button = $(event.currentTarget).find(".ui.button[type='submit']")
button.prop('disabled', true)
button.css('cursor', 'wait')
const body = new FormData(event.currentTarget)
const unidades = event.currentTarget.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
})
})
event.preventDefault();
const button = $(event.currentTarget).find(".ui.button[type='submit']");
button.prop('disabled', true);
button.css('cursor', 'wait');
const form = event.currentTarget;
venta.submit(form);
return false;
})
document.getElementById('submit_button').addEventListener('click', clickEvent => {
clickEvent.preventDefault();
const button = $(clickEvent.currentTarget)
button.prop('disabled', true);
button.css('cursor', 'wait');
const form = document.getElementById('add_form')
venta.submit(form)
return false;
})
if (fromReservation) {