forked from RiseUP/riseup-squad21
Merge pull request 'Troca-de-API' (#6) from StsDanilo/riseup-squad21:Troca-de-API into main
Troca de API
This commit is contained in:
commit
c4ae9c6789
@ -112,7 +112,7 @@ export default function HospitalLayout({ children }: HospitalLayoutProps) {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="min-h-screen bg-gray-50 flex">
|
<div className="min-h-screen bg-gray-50 flex">
|
||||||
{/* Sidebar */}
|
{/* Sidebar */}
|
||||||
<div
|
<div
|
||||||
className={`bg-white border-r border-gray-200 transition-all duration-300 ${sidebarCollapsed ? "w-16" : "w-64"} h-screen flex flex-col`}
|
className={`bg-white border-r border-gray-200 transition-all duration-300 ${sidebarCollapsed ? "w-16" : "w-64"} h-screen flex flex-col`}
|
||||||
>
|
>
|
||||||
|
|||||||
64
services/api.mjs
Normal file
64
services/api.mjs
Normal file
@ -0,0 +1,64 @@
|
|||||||
|
|
||||||
|
// váriaveis básicas
|
||||||
|
const BASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
||||||
|
const API_KEY = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
||||||
|
var tempToken;
|
||||||
|
|
||||||
|
async function login() {
|
||||||
|
const response = await fetch("https://yuanqfswhberkoevtmfr.supabase.co/auth/v1/token?grant_type=password", {
|
||||||
|
method: "POST",
|
||||||
|
headers: {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"apikey": API_KEY, // valor fixo
|
||||||
|
},
|
||||||
|
body: JSON.stringify({ email: "hugo@popcode.com.br", password: "hdoria" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
console.log("Resposta da API:", data);
|
||||||
|
console.log("Token:", data.access_token);
|
||||||
|
// salvar o token do usuário
|
||||||
|
//localStorage.setItem("token", data.access_token);
|
||||||
|
tempToken = data.access_token
|
||||||
|
|
||||||
|
return data;
|
||||||
|
}
|
||||||
|
await login()
|
||||||
|
|
||||||
|
async function request(endpoint, options = {}) {
|
||||||
|
//const token = localStorage.getItem("token"); // token do usuário, salvo no login
|
||||||
|
const token = tempToken;
|
||||||
|
|
||||||
|
const headers = {
|
||||||
|
"Content-Type": "application/json",
|
||||||
|
"apikey": API_KEY, // obrigatório sempre
|
||||||
|
...(token ? { Authorization: `Bearer ${token}` } : {}), // obrigatório em todas EXCETO login
|
||||||
|
...options.headers,
|
||||||
|
};
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${BASE_URL}${endpoint}`, {
|
||||||
|
...options,
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`Erro HTTP: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return await response.json();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Erro na requisição:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const api = {
|
||||||
|
get: (endpoint) => request(endpoint, { method: "GET" }),
|
||||||
|
post: (endpoint, body) =>
|
||||||
|
request(endpoint, { method: "POST", body: JSON.stringify(body) }),
|
||||||
|
patch: (endpoint, body) =>
|
||||||
|
request(endpoint, { method: "PATCH", body: JSON.stringify(body) }),
|
||||||
|
delete: (endpoint) => request(endpoint, { method: "DELETE" }),
|
||||||
|
};
|
||||||
0
services/doctorsApi.mjs
Normal file
0
services/doctorsApi.mjs
Normal file
9
services/patientsApi.mjs
Normal file
9
services/patientsApi.mjs
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
import { api } from "./api.mjs";
|
||||||
|
|
||||||
|
export const patientsService = {
|
||||||
|
list: () => api.get("/rest/v1/patients"),
|
||||||
|
getById: (id) => api.get(`/rest/v1/patients/${id}`),
|
||||||
|
create: (data) => api.post("/rest/v1/patients", data),
|
||||||
|
update: (id, data) => api.patch(`/rest/v1/patients/${id}`, data),
|
||||||
|
delete: (id) => api.delete(`/rest/v1/patients/${id}`),
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user