Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Frontend: Backend connection for sign up page #438

Merged
merged 2 commits into from
Oct 30, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 36 additions & 4 deletions learnify/web/src/pages/SignUpForm.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React from 'react';
import React, { useState } from 'react';
import { NavLink, useNavigate } from 'react-router-dom';
import { useForm } from "react-hook-form";
import { yupResolver } from '@hookform/resolvers/yup'
Expand Down Expand Up @@ -43,10 +43,41 @@ function SignUpForm() {

const navigate = useNavigate();

const onSubmit = (data, event) => {
console.log(data, event);
const [message, setMessage] = useState("");

const registerUser = async (username, email, password) => {
await fetch("http://3.75.151.200:3000/auth/signup", {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can put this URL into a .env file and use it from that instead of using it statically on the code since we will need to use this value several times on the front-end project. Also, the backend will deploy the project several times and probably change the URL.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably we will use it like;
await fetch("{process.env.BACKEND_URL}/auth/signup"

method: "POST",
body: JSON.stringify({
username: username,
email: email,
password: password,
}),
headers: {
'Content-type': 'application/json; charset=UTF-8',
},
})
.then((response) => {
console.log(response.status);
console.log(response.statusText);
if (response.ok) {
navigate('/verify-email', {replace: true});
return response.json();
} else {
response.json().then( json => {
setMessage(json.resultMessage)
});
throw Error(response.statusText);
}
})
.catch((err) => {
console.log(err.message);
})
};

const onSubmit = (registerData, event) => {
event.preventDefault();
navigate('/verify-email', {replace: true});
registerUser(registerData.username, registerData.email, registerData.password);
}

return(
Expand Down Expand Up @@ -149,6 +180,7 @@ function SignUpForm() {
class="btn-orange">
Register
</button>
<div className="submit-button-error">{message ? <p>{message}</p> : null}</div>
</div>
</form>
</div>
Expand Down
7 changes: 7 additions & 0 deletions learnify/web/src/pages/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@ body{
background-color: #FF731D;
}

.submit-button-error{
width:100%;
margin-top:.25rem;
font-size:.875em;
color:#dc3545;
padding-left: 10px;
}

*:disabled {
background-color: dimgrey;
Expand Down