diff --git a/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/action.ts b/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/action.ts index e69de29..3529d70 100644 --- a/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/action.ts +++ b/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/action.ts @@ -0,0 +1,40 @@ +import axios from "../../api/GymApi"; + +interface UserCreationResult{ + Success: boolean, + Errors: string[] +} + +export function handleCreateUser( + firstName: string, + lastName: string, + email: string, + password: string, + phoneNumber: string | null +): Promise{ + return axios.post('api/user', { + firstName, + lastName, + email, + password, + phoneNumber, + }).then(response => { + + if (response.status == 200) + return { + Success: true, + Errors: [] + } + + if (response.status == 403) + return { + Success: false, + Errors: ['Usuário já existente.'] + } + + return { + Success: false, + Errors: ['Falha ao criar usuário.'] + } + }) +} \ No newline at end of file diff --git a/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/index.tsx b/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/index.tsx index 608c97a..106a298 100644 --- a/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/index.tsx +++ b/src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/index.tsx @@ -1,6 +1,7 @@ import React, { useState } from 'react'; -import { View, Text, TextInput, Button, StyleSheet, Alert } from 'react-native'; +import { View, Text, TextInput, Button, Alert } from 'react-native'; import styles from '../LoginScreen/styles'; +import { handleCreateUser } from './action'; const CreateUserScreen = () => { const [firstName, setFirstName] = useState(''); @@ -9,13 +10,21 @@ const CreateUserScreen = () => { const [password, setPassword] = useState(''); const [phoneNumber, setPhoneNumber] = useState(''); - const handleSubmit = () => { + const handleSubmit = async () => { if (!firstName || !lastName || !email || !password || !phoneNumber) { - Alert.alert('Error', 'All fields are required.'); + Alert.alert('Falha', 'Adicione os campos necessários.'); return; } - // Handle user creation logic here - Alert.alert('Success', 'User created successfully!'); + + var result = await handleCreateUser(firstName, lastName, email, password, phoneNumber); + + if (result.Success){ + // + // Successfully created, going to the next page + // + } + + Alert.alert("Falha", result.Errors.join('\n')); }; return (