From ec444dd9e945a1b5c95668f33ab1068d94bda6a2 Mon Sep 17 00:00:00 2001 From: Vinicius Souza Date: Tue, 29 Oct 2024 19:19:01 -0300 Subject: [PATCH] feat: use axios on sign up screen --- src/screens/SignUp.tsx | 21 ++++++++++++++------- src/services/api.ts | 5 +++++ 2 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 src/services/api.ts diff --git a/src/screens/SignUp.tsx b/src/screens/SignUp.tsx index ebfa986..ffebf8f 100644 --- a/src/screens/SignUp.tsx +++ b/src/screens/SignUp.tsx @@ -4,6 +4,8 @@ import { Controller, useForm } from 'react-hook-form'; import * as yup from 'yup'; import { yupResolver } from '@hookform/resolvers/yup'; +import { api } from '@services/api'; + import BackgroundImg from '@assets/background.png'; import Logo from '@assets/logo.svg'; @@ -11,6 +13,7 @@ import { AuthNavigatorRoutesProps } from '@routes/auth.routes'; import { Input } from '@components/Input'; import { Button } from '@components/Button'; +import axios from 'axios'; type FormData = { name: string; @@ -40,13 +43,17 @@ export function SignUp() { resolver: yupResolver(signUpSchema), }); - function handleSignUp({ name, email, password, confirmPassword }: FormData) { - console.debug({ - name, - email, - password, - confirmPassword, - }); + async function handleSignUp({ name, email, password }: FormData) { + try { + const response = await api.post('/users', { name, email, password }); + console.debug(response.data); + } catch (error) { + if (axios.isAxiosError(error)) { + console.error(error.response?.data); + return; + } + throw error; + } } const handleGoBack = () => { diff --git a/src/services/api.ts b/src/services/api.ts new file mode 100644 index 0000000..41b31e4 --- /dev/null +++ b/src/services/api.ts @@ -0,0 +1,5 @@ +import axios from 'axios'; + +export const api = axios.create({ + baseURL: 'http://192.168.0.53:3333', +});