loop de requisiçoes na pag de meus dados

This commit is contained in:
m1guelmcf 2025-12-01 15:14:44 -03:00
parent c04b0989d2
commit a078204276
4 changed files with 100 additions and 79 deletions

View File

@ -34,18 +34,24 @@ export default function PatientProfile() {
const { user, isLoading: isAuthLoading } = useAuthLayout({
requiredRole: ["paciente", "admin", "medico", "gestor", "secretaria"],
});
const [patientData, setPatientData] = useState<PatientProfileData | null>(
null
);
const [patientData, setPatientData] = useState<PatientProfileData | null>(null);
const [isEditing, setIsEditing] = useState(false);
const [isSaving, setIsSaving] = useState(false);
const fileInputRef = useRef<HTMLInputElement>(null);
useEffect(() => {
if (user?.id) {
console.log("PatientProfile MONTADO");
}, []);
useEffect(() => {
const userId = user?.id;
if (!userId) return;
const fetchPatientDetails = async () => {
try {
const patientDetails = await patientsService.getById(user.id);
const patientDetails = await patientsService.getById(userId);
setPatientData({
name: patientDetails.full_name || user.name,
email: user.email,
@ -67,9 +73,10 @@ export default function PatientProfile() {
});
}
};
fetchPatientDetails();
}
}, [user]);
}, [user?.id, user?.name, user?.email, user?.avatarFullUrl]);
const handleInputChange = (
field: keyof PatientProfileData,

View File

@ -18,7 +18,9 @@ interface UseAuthLayoutOptions {
requiredRole?: string[];
}
export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
export function useAuthLayout(
{ requiredRole }: UseAuthLayoutOptions = {}
) {
const [user, setUser] = useState<UserLayoutData | null>(null);
const [isLoading, setIsLoading] = useState(true);
const router = useRouter();
@ -28,8 +30,16 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
try {
const fullUserData = await usersService.getMe();
if (!fullUserData.roles.some((role) => requiredRole?.includes(role))) {
console.error(`Acesso negado. Requer perfil '${requiredRole}', mas o usuário tem '${fullUserData.roles.join(", ")}'.`);
// só verifica papel se requiredRole existir
if (
requiredRole &&
!fullUserData.roles.some((role: string) =>
requiredRole.includes(role)
)
) {
console.error(
`Acesso negado. Requer perfil '${requiredRole}', mas o usuário tem '${fullUserData.roles.join(", ")}'.`
);
toast({
title: "Acesso Negado",
description: "Você não tem permissão para acessar esta página.",
@ -40,10 +50,9 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
}
const avatarPath = fullUserData.profile.avatar_url;
// *** A CORREÇÃO ESTÁ AQUI ***
// Adicionamos o nome do bucket 'avatars' na URL final.
const avatarFullUrl = avatarPath ? `https://yuanqfswhberkoevtmfr.supabase.co/storage/v1/object/public/avatars/${avatarPath}` : undefined;
const avatarFullUrl = avatarPath
? `https://yuanqfswhberkoevtmfr.supabase.co/storage/v1/object/public/avatars/${avatarPath}`
: undefined;
setUser({
id: fullUserData.user.id,
@ -51,7 +60,7 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
email: fullUserData.user.email,
roles: fullUserData.roles,
avatar_url: avatarPath,
avatarFullUrl: avatarFullUrl,
avatarFullUrl,
});
} catch (error) {
console.error("Falha na autenticação do layout:", error);
@ -62,7 +71,7 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
};
fetchUserData();
}, [router, requiredRole]);
}, [router]); // não depende mais de requiredRole
return { user, isLoading };
}

View File

@ -2,7 +2,11 @@ import { api } from "./api.mjs";
export const patientsService = {
list: () => api.get("/rest/v1/patients"),
getById: (id) => api.get(`/rest/v1/patients?id=eq.${id}`),
getById: (id) => {
console.log("getById chamado", id);
return api.get(`/rest/v1/patients?id=eq.${id}`);
},
create: (data) => api.post("/rest/v1/patients", data),
update: (id, data) => api.patch(`/rest/v1/patients?id=eq.${id}`, data),
delete: (id) => api.delete(`/rest/v1/patients?id=eq.${id}`),

View File

@ -3,6 +3,7 @@ import { api } from "./api.mjs";
export const usersService = {
// Função getMe corrigida para chamar a si mesma pelo nome
async getMe() {
console.log("getMe chamado");
const sessionData = await api.getSession();
if (!sessionData?.id) {
console.error("Sessão não encontrada ou usuário sem ID.", sessionData);