forked from RiseUP/riseup-squad20
fix(consulta) corrigir o erro na parte de cancelar consulta
This commit is contained in:
parent
68c38dba88
commit
ca282e721e
@ -867,17 +867,33 @@ export default function PacientePage() {
|
|||||||
try {
|
try {
|
||||||
const ok = typeof window !== 'undefined' ? window.confirm('Deseja realmente cancelar esta consulta?') : true
|
const ok = typeof window !== 'undefined' ? window.confirm('Deseja realmente cancelar esta consulta?') : true
|
||||||
if (!ok) return
|
if (!ok) return
|
||||||
// call API to delete
|
|
||||||
await deletarAgendamento(consulta.id)
|
// Prefer PATCH to mark appointment as cancelled (safer under RLS)
|
||||||
// Mark as deleted in cache so it won't appear again
|
try {
|
||||||
addDeletedAppointmentId(consulta.id)
|
await atualizarAgendamento(consulta.id, {
|
||||||
// remove from local list
|
cancelled_at: new Date().toISOString(),
|
||||||
|
status: 'cancelled',
|
||||||
|
cancellation_reason: 'Cancelado pelo paciente'
|
||||||
|
})
|
||||||
|
} catch (patchErr) {
|
||||||
|
// Fallback: try hard delete if server allows it
|
||||||
|
try {
|
||||||
|
await deletarAgendamento(consulta.id)
|
||||||
|
} catch (delErr) {
|
||||||
|
// Re-throw original patch error if both fail
|
||||||
|
throw patchErr || delErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// remove from local list so UI updates immediately
|
||||||
setAppointments((prev) => {
|
setAppointments((prev) => {
|
||||||
if (!prev) return prev
|
if (!prev) return prev
|
||||||
return prev.filter((a: any) => String(a.id) !== String(consulta.id))
|
return prev.filter((a: any) => String(a.id) !== String(consulta.id))
|
||||||
})
|
})
|
||||||
// if modal open for this appointment, close it
|
// if modal open for this appointment, close it
|
||||||
if (selectedAppointment && String(selectedAppointment.id) === String(consulta.id)) setSelectedAppointment(null)
|
if (selectedAppointment && String(selectedAppointment.id) === String(consulta.id)) setSelectedAppointment(null)
|
||||||
|
// Optionally persist to deleted cache to help client-side filtering
|
||||||
|
try { addDeletedAppointmentId(consulta.id) } catch(e) {}
|
||||||
setToast({ type: 'success', msg: 'Consulta cancelada.' })
|
setToast({ type: 'success', msg: 'Consulta cancelada.' })
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
console.error('[Consultas] falha ao cancelar agendamento', err)
|
console.error('[Consultas] falha ao cancelar agendamento', err)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user