mirror of
https://github.com/felipegcoutinho/openmonetis.git
synced 2026-05-09 11:01:45 +00:00
feat(dados-client): adotar react query em leituras do app
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
export { PrivacyProvider } from "./privacy-provider";
|
||||
export { QueryProvider } from "./query-provider";
|
||||
export { ThemeProvider } from "./theme-provider";
|
||||
|
||||
23
src/shared/components/providers/query-provider.tsx
Normal file
23
src/shared/components/providers/query-provider.tsx
Normal file
@@ -0,0 +1,23 @@
|
||||
"use client";
|
||||
|
||||
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
|
||||
import { useState } from "react";
|
||||
|
||||
function makeQueryClient() {
|
||||
return new QueryClient({
|
||||
defaultOptions: {
|
||||
queries: {
|
||||
refetchOnWindowFocus: false,
|
||||
retry: 1,
|
||||
},
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function QueryProvider({ children }: { children: React.ReactNode }) {
|
||||
const [queryClient] = useState(makeQueryClient);
|
||||
|
||||
return (
|
||||
<QueryClientProvider client={queryClient}>{children}</QueryClientProvider>
|
||||
);
|
||||
}
|
||||
26
src/shared/lib/fetch-json.ts
Normal file
26
src/shared/lib/fetch-json.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
export async function fetchJson<T>(
|
||||
input: RequestInfo | URL,
|
||||
init?: RequestInit,
|
||||
): Promise<T> {
|
||||
const response = await fetch(input, {
|
||||
cache: "no-store",
|
||||
...init,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
let message = `Erro na requisição (${response.status})`;
|
||||
|
||||
try {
|
||||
const payload = (await response.json()) as { error?: string };
|
||||
if (payload.error) {
|
||||
message = payload.error;
|
||||
}
|
||||
} catch {
|
||||
// noop
|
||||
}
|
||||
|
||||
throw new Error(message);
|
||||
}
|
||||
|
||||
return response.json() as Promise<T>;
|
||||
}
|
||||
Reference in New Issue
Block a user