Adição do botão de entrega manual
This commit is contained in:
76
app/javascript/controllers/apontamento_manual_controller.js
Normal file
76
app/javascript/controllers/apontamento_manual_controller.js
Normal file
@@ -0,0 +1,76 @@
|
||||
// app/javascript/controllers/apontamento_manual_controller.js
|
||||
// Stimulus — Apontamento 100% MANUAL (sem consultar a base de rastreio).
|
||||
// O admin informa motorista, NF (texto livre), observação/local e os pilares.
|
||||
// Usado quando o motorista foi ao local mas a NF ficou com outro motorista no
|
||||
// rastreio (a busca por NF nunca o encontraria).
|
||||
|
||||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
export default class extends Controller {
|
||||
static targets = ["modal", "motorista", "nf", "obs", "erro", "tipoCheck", "valorExtra"]
|
||||
static values = { apontarManualUrl: String }
|
||||
|
||||
abrir() {
|
||||
this.erroTarget.classList.add("hidden")
|
||||
this.modalTarget.classList.remove("hidden")
|
||||
this.motoristaTarget.focus()
|
||||
}
|
||||
|
||||
fechar() {
|
||||
this.modalTarget.classList.add("hidden")
|
||||
}
|
||||
|
||||
// Fecha ao clicar no fundo (fora do card)
|
||||
fundo(event) {
|
||||
if (event.target === this.modalTarget) this.fechar()
|
||||
}
|
||||
|
||||
async adicionar(event) {
|
||||
event?.preventDefault()
|
||||
|
||||
const motorista = this.motoristaTarget.value.trim()
|
||||
if (!motorista) { this.mostrarErro("Informe o motorista."); return }
|
||||
|
||||
const tipos = this.tipoCheckTargets.filter(c => c.checked).map(c => c.value)
|
||||
if (!tipos.length) { this.mostrarErro("Selecione ao menos uma classificação."); return }
|
||||
|
||||
// Valor customizado da Extraordinária (opcional)
|
||||
const valor = this.hasValorExtraTarget ? this.valorExtraTarget.value.trim() : ""
|
||||
|
||||
const btn = event.currentTarget
|
||||
btn.disabled = true
|
||||
try {
|
||||
const resp = await fetch(this.apontarManualUrlValue, {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
"Accept": "application/json",
|
||||
"X-CSRF-Token": document.querySelector('meta[name="csrf-token"]').content
|
||||
},
|
||||
body: JSON.stringify({
|
||||
motorista: motorista,
|
||||
nf: this.nfTarget.value.trim(),
|
||||
obs: this.obsTarget.value.trim(),
|
||||
tipos: tipos,
|
||||
valor: valor
|
||||
})
|
||||
})
|
||||
const data = await resp.json()
|
||||
if (!resp.ok || data.ok === false) {
|
||||
this.mostrarErro(data.erro || "Não foi possível adicionar o apontamento.")
|
||||
return
|
||||
}
|
||||
// Recarrega para refletir o motorista/valores atualizados.
|
||||
window.location.reload()
|
||||
} catch (e) {
|
||||
this.mostrarErro("Erro ao adicionar o apontamento. Tente novamente.")
|
||||
} finally {
|
||||
btn.disabled = false
|
||||
}
|
||||
}
|
||||
|
||||
mostrarErro(msg) {
|
||||
this.erroTarget.textContent = msg
|
||||
this.erroTarget.classList.remove("hidden")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user