diff --git a/dashboard_js/app/axios.js b/dashboard_js/app/axios.js index 1cfa2dd..3bc7c7a 100644 --- a/dashboard_js/app/axios.js +++ b/dashboard_js/app/axios.js @@ -1,5 +1,25 @@ import axios from 'axios' -export default axios.create({ +const instance = axios.create({ baseURL: import.meta.env.VITE_API_URL, }) + +instance.interceptors.response.use( + // Any status code that lie within the range of 2xx cause this function to trigger + function (response) { + return response + }, + + // Any status codes that falls outside the range of 2xx cause this function to trigger + function ({ response }) { + const error = new Error(response.data?.message) + + error.code = response.data?.__type + error.name = response.data?.__type + error.statusCode = response.status + + return Promise.reject(error) + }, +) + +export default instance diff --git a/dashboard_js/app/root.jsx b/dashboard_js/app/root.jsx index 75c0636..4f042c1 100644 --- a/dashboard_js/app/root.jsx +++ b/dashboard_js/app/root.jsx @@ -15,7 +15,6 @@ import amplifyconfig from './amplifyconfiguration.json' import stylesheet from './app.css?url' Amplify.configure(amplifyconfig) -const queryClient = new QueryClient() export const links = () => [ { rel: 'preconnect', href: 'https://fonts.googleapis.com' }, @@ -51,6 +50,8 @@ export function Layout({ children }) { } export default function App() { + const queryClient = new QueryClient() + return ( @@ -86,6 +87,7 @@ export function ErrorBoundary({ error }) {

{message}

{details}

+ {stack && (
           {stack}
diff --git a/dashboard_js/app/routes/auth/login/index.jsx b/dashboard_js/app/routes/auth/login/index.jsx
index 9010cf7..44ab321 100644
--- a/dashboard_js/app/routes/auth/login/index.jsx
+++ b/dashboard_js/app/routes/auth/login/index.jsx
@@ -52,17 +52,36 @@ function SignIn() {
       const { data } = await axios.get(`/search/lookup/${username}`)
       return data
     },
+    onSuccess: (data) => {
+      setSearchParams((searchParams) => {
+        searchParams.set('state', encode(data))
+        return searchParams
+      })
+    },
   })
 
-  const onSubmit = async ({ username }) => {
-    const data = await mutateAsync({ username })
-
-    setSearchParams((searchParams) => {
-      searchParams.set('state', encode(data))
-      return searchParams
+  const formError = (message) => {
+    setError('username', {
+      type: 'manual',
+      message,
     })
   }
 
+  const onSubmit = async ({ username }) => {
+    try {
+      await mutateAsync({ username })
+    } catch ({ message }) {
+      switch (message) {
+        case 'User not found.':
+          return formError(
+            'Não encontramos sua conta. Por favor, verifique se seu Email ou CPF estão corretos.',
+          )
+        default:
+          return formError(message)
+      }
+    }
+  }
+
   return (
     
       Digite o seu email ou cpf para continuar