forked from RiseUP/riseup-squad23
102 lines
2.9 KiB
JavaScript
102 lines
2.9 KiB
JavaScript
import React, { useEffect, useState } from 'react';
|
|
import FormNovaConsulta from '../components/AgendarConsulta/FormNovaConsulta';
|
|
import API_KEY from '../components/utils/apiKeys';
|
|
import { useAuth } from '../components/utils/AuthProvider';
|
|
import dayjs from 'dayjs';
|
|
import { UserInfos } from '../components/utils/Functions-Endpoints/General';
|
|
|
|
const AgendamentoCadastroManager = ({ setPageConsulta, Dict, onSaved }) => {
|
|
const { getAuthorizationHeader, user } = useAuth();
|
|
const [agendamento, setAgendamento] = useState({ status: 'confirmed' });
|
|
const [idUsuario, setIDusuario] = useState('0');
|
|
|
|
// patient_id do paciente logado (ou fallback para o Pedro)
|
|
|
|
const patientId = 'bf7d8323-05e1-437a-817c-f08eb5f174ef';
|
|
|
|
const authHeader = getAuthorizationHeader();
|
|
|
|
useEffect(() => {
|
|
if (!Dict) {
|
|
setAgendamento({ status: 'confirmed' });
|
|
} else {
|
|
setAgendamento(Dict);
|
|
}
|
|
|
|
const ColherInfoUsuario = async () => {
|
|
try {
|
|
const result = await UserInfos(authHeader);
|
|
setIDusuario(result?.profile?.id);
|
|
} catch (e) {
|
|
console.error('Erro ao buscar infos do usuário:', e);
|
|
}
|
|
};
|
|
|
|
if (authHeader) {
|
|
ColherInfoUsuario();
|
|
}
|
|
}, [Dict, authHeader]);
|
|
|
|
const handleSave = async (DictForm) => {
|
|
if (!authHeader) {
|
|
alert('Sem autorização. Faça login novamente.');
|
|
return;
|
|
}
|
|
|
|
const myHeaders = new Headers();
|
|
myHeaders.append('apikey', API_KEY);
|
|
myHeaders.append('Authorization', authHeader);
|
|
myHeaders.append('Content-Type', 'application/json');
|
|
|
|
const raw = JSON.stringify({
|
|
patient_id: patientId, // paciente logado
|
|
doctor_id: DictForm.doctor_id,
|
|
scheduled_at: `${DictForm.dataAtendimento}T${DictForm.horarioInicio}:00`,
|
|
duration_minutes: 30,
|
|
appointment_type: DictForm.tipo_consulta,
|
|
patient_notes: '',
|
|
insurance_provider: DictForm.convenio,
|
|
status: 'confirmed', // ou 'confirmed'
|
|
created_by: idUsuario,
|
|
created_at: dayjs().toISOString(),
|
|
});
|
|
|
|
const requestOptions = {
|
|
method: 'POST',
|
|
headers: myHeaders,
|
|
body: raw,
|
|
redirect: 'follow',
|
|
};
|
|
|
|
try {
|
|
const response = await fetch(
|
|
'https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/appointments',
|
|
requestOptions
|
|
);
|
|
if (response.ok) {
|
|
if (onSaved) onSaved(); // pai recarrega e fecha
|
|
else setPageConsulta(false);
|
|
} else {
|
|
console.error('Erro ao criar agendamento:', await response.text());
|
|
alert('Falha ao criar agendamento.');
|
|
}
|
|
} catch (error) {
|
|
console.error('Erro de rede:', error);
|
|
alert('Erro de rede ao salvar agendamento.');
|
|
}
|
|
};
|
|
|
|
return (
|
|
<div>
|
|
<FormNovaConsulta
|
|
onSave={handleSave}
|
|
agendamento={agendamento}
|
|
setAgendamento={setAgendamento}
|
|
onCancel={() => setPageConsulta(false)}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default AgendamentoCadastroManager;
|