Skip to content

Commit

Permalink
feat: add user registration service (devhatt#293)
Browse files Browse the repository at this point in the history
* feat: add user registration service

* chore: transform userRegister file into a userService method
  • Loading branch information
JpBurgarelli authored Aug 20, 2024
1 parent a12dea8 commit a538cf6
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/components/RegisterForm/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import googleIcon from './images/google-icon.svg';
import facebookIcon from './images/facebook-icon.svg';
import './index.scss';
import { UserService } from '../../services/userService';

const events = ['register'];

Expand Down Expand Up @@ -151,7 +152,7 @@ export default function RegisterForm() {
repeatPassword.mount($fields);
registerButton.mount($formButton);

registerButton.listen('click', () => {
registerButton.listen('click', async () => {
const nameValue = name.getContent().getValue();
const surnameValue = surname.getContent().getValue();
const birthValue = birth.getContent().getValue();
Expand Down Expand Up @@ -270,6 +271,16 @@ export default function RegisterForm() {
passwordValid &&
repeatPasswordValid
) {
await UserService.registerUser({
name: nameValue,
surname: surnameValue,
birth: birthValue,
local: localValue,
email: emailValue,
phone: phoneValue,
password: passwordValue,
});

this.register();
}
});
Expand Down
29 changes: 29 additions & 0 deletions src/services/userService.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export const UserService = {
return `An error occurred: ${error}`;
}
},

login: async (email, password) => {
try {
const response = await fetch(`${url}/api/user/login`, {
Expand All @@ -38,4 +39,32 @@ export const UserService = {
};
}
},

registerUser: async (userData) => {
try {
const response = await fetch(`${url}/naoSeiARota`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(userData),
});

if (!response.ok) {
throw new Error('Failed to register user');
}

const result = await response.json();
return {
success: true,
data: result,
};
} catch (error) {
console.error(`An error occurred: ${error}`);
return {
success: false,
message: error.message,
};
}
},
};

0 comments on commit a538cf6

Please sign in to comment.