Skip to content

Commit

Permalink
adding api action
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhermeBley committed Jul 8, 2024
1 parent 475ec84 commit d0785e0
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
40 changes: 40 additions & 0 deletions src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/action.ts
Original file line number Diff line number Diff line change
@@ -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<UserCreationResult>{
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.']
}
})
}
19 changes: 14 additions & 5 deletions src/ui/Bl-gym-mobile/src/screens/CreateUserScreen/index.tsx
Original file line number Diff line number Diff line change
@@ -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('');
Expand All @@ -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 (
Expand Down

0 comments on commit d0785e0

Please sign in to comment.