loop de requisiçoes na pag de meus dados
This commit is contained in:
parent
c04b0989d2
commit
a078204276
@ -34,18 +34,24 @@ export default function PatientProfile() {
|
|||||||
const { user, isLoading: isAuthLoading } = useAuthLayout({
|
const { user, isLoading: isAuthLoading } = useAuthLayout({
|
||||||
requiredRole: ["paciente", "admin", "medico", "gestor", "secretaria"],
|
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 [isEditing, setIsEditing] = useState(false);
|
||||||
const [isSaving, setIsSaving] = useState(false);
|
const [isSaving, setIsSaving] = useState(false);
|
||||||
const fileInputRef = useRef<HTMLInputElement>(null);
|
const fileInputRef = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (user?.id) {
|
console.log("PatientProfile MONTADO");
|
||||||
|
}, []);
|
||||||
|
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const userId = user?.id;
|
||||||
|
if (!userId) return;
|
||||||
|
|
||||||
const fetchPatientDetails = async () => {
|
const fetchPatientDetails = async () => {
|
||||||
try {
|
try {
|
||||||
const patientDetails = await patientsService.getById(user.id);
|
const patientDetails = await patientsService.getById(userId);
|
||||||
setPatientData({
|
setPatientData({
|
||||||
name: patientDetails.full_name || user.name,
|
name: patientDetails.full_name || user.name,
|
||||||
email: user.email,
|
email: user.email,
|
||||||
@ -67,9 +73,10 @@ export default function PatientProfile() {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchPatientDetails();
|
fetchPatientDetails();
|
||||||
}
|
}, [user?.id, user?.name, user?.email, user?.avatarFullUrl]);
|
||||||
}, [user]);
|
|
||||||
|
|
||||||
const handleInputChange = (
|
const handleInputChange = (
|
||||||
field: keyof PatientProfileData,
|
field: keyof PatientProfileData,
|
||||||
|
|||||||
@ -18,7 +18,9 @@ interface UseAuthLayoutOptions {
|
|||||||
requiredRole?: string[];
|
requiredRole?: string[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
|
export function useAuthLayout(
|
||||||
|
{ requiredRole }: UseAuthLayoutOptions = {}
|
||||||
|
) {
|
||||||
const [user, setUser] = useState<UserLayoutData | null>(null);
|
const [user, setUser] = useState<UserLayoutData | null>(null);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
@ -28,8 +30,16 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
|
|||||||
try {
|
try {
|
||||||
const fullUserData = await usersService.getMe();
|
const fullUserData = await usersService.getMe();
|
||||||
|
|
||||||
if (!fullUserData.roles.some((role) => requiredRole?.includes(role))) {
|
// só verifica papel se requiredRole existir
|
||||||
console.error(`Acesso negado. Requer perfil '${requiredRole}', mas o usuário tem '${fullUserData.roles.join(", ")}'.`);
|
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({
|
toast({
|
||||||
title: "Acesso Negado",
|
title: "Acesso Negado",
|
||||||
description: "Você não tem permissão para acessar esta página.",
|
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;
|
const avatarPath = fullUserData.profile.avatar_url;
|
||||||
|
const avatarFullUrl = avatarPath
|
||||||
// *** A CORREÇÃO ESTÁ AQUI ***
|
? `https://yuanqfswhberkoevtmfr.supabase.co/storage/v1/object/public/avatars/${avatarPath}`
|
||||||
// Adicionamos o nome do bucket 'avatars' na URL final.
|
: undefined;
|
||||||
const avatarFullUrl = avatarPath ? `https://yuanqfswhberkoevtmfr.supabase.co/storage/v1/object/public/avatars/${avatarPath}` : undefined;
|
|
||||||
|
|
||||||
setUser({
|
setUser({
|
||||||
id: fullUserData.user.id,
|
id: fullUserData.user.id,
|
||||||
@ -51,7 +60,7 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
|
|||||||
email: fullUserData.user.email,
|
email: fullUserData.user.email,
|
||||||
roles: fullUserData.roles,
|
roles: fullUserData.roles,
|
||||||
avatar_url: avatarPath,
|
avatar_url: avatarPath,
|
||||||
avatarFullUrl: avatarFullUrl,
|
avatarFullUrl,
|
||||||
});
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Falha na autenticação do layout:", error);
|
console.error("Falha na autenticação do layout:", error);
|
||||||
@ -62,7 +71,7 @@ export function useAuthLayout({ requiredRole }: UseAuthLayoutOptions = {}) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
fetchUserData();
|
fetchUserData();
|
||||||
}, [router, requiredRole]);
|
}, [router]); // não depende mais de requiredRole
|
||||||
|
|
||||||
return { user, isLoading };
|
return { user, isLoading };
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,11 @@ import { api } from "./api.mjs";
|
|||||||
|
|
||||||
export const patientsService = {
|
export const patientsService = {
|
||||||
list: () => api.get("/rest/v1/patients"),
|
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),
|
create: (data) => api.post("/rest/v1/patients", data),
|
||||||
update: (id, data) => api.patch(`/rest/v1/patients?id=eq.${id}`, data),
|
update: (id, data) => api.patch(`/rest/v1/patients?id=eq.${id}`, data),
|
||||||
delete: (id) => api.delete(`/rest/v1/patients?id=eq.${id}`),
|
delete: (id) => api.delete(`/rest/v1/patients?id=eq.${id}`),
|
||||||
|
|||||||
@ -3,6 +3,7 @@ import { api } from "./api.mjs";
|
|||||||
export const usersService = {
|
export const usersService = {
|
||||||
// Função getMe corrigida para chamar a si mesma pelo nome
|
// Função getMe corrigida para chamar a si mesma pelo nome
|
||||||
async getMe() {
|
async getMe() {
|
||||||
|
console.log("getMe chamado");
|
||||||
const sessionData = await api.getSession();
|
const sessionData = await api.getSession();
|
||||||
if (!sessionData?.id) {
|
if (!sessionData?.id) {
|
||||||
console.error("Sessão não encontrada ou usuário sem ID.", sessionData);
|
console.error("Sessão não encontrada ou usuário sem ID.", sessionData);
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user