Skip to content

Commit

Permalink
#10 add handleSubmit and error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
SandraBergstrom committed Jun 21, 2023
1 parent 0bfcd28 commit 417c8d4
Showing 1 changed file with 42 additions and 5 deletions.
47 changes: 42 additions & 5 deletions src/pages/auth/SignUpForm.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
import React, { useState } from "react";
import { Link } from "react-router-dom";
import { Link, useHistory } from "react-router-dom";

import styles from "../../styles/SignInUpForm.module.css";
import btnStyles from "../../styles/Button.module.css";
import appStyles from "../../App.module.css";

import { Form, Button, Image, Col, Row, Container } from "react-bootstrap";
import { useHistory } from "react-router-dom";
import {
Form,
Button,
Image,
Col,
Row,
Container,
Alert,
} from "react-bootstrap";
import axios from "axios";

const SignUpForm = () => {
const [signUpData, setSignUpData] = useState({
Expand All @@ -16,13 +24,27 @@ const SignUpForm = () => {
});
const { username, password1, password2 } = signUpData;

const [errors, setErrors] = useState({});

const history = useHistory();

const handleChange = (event) => {
setSignUpData({
...signUpData,
[event.target.name]: event.target.value,
});
};

const handleSubmit = async (event) => {
event.preventDefault();
try {
await axios.post("/dj-rest-auth/registration/", signUpData);
history.push("/signin");
} catch (err) {
setErrors(err.response?.data);
}
};

return (
<Row className={styles.Row}>
<Col
Expand All @@ -39,18 +61,23 @@ const SignUpForm = () => {
<Col className="my-auto py-2 p-md-2" md={6}>
<Container className={`${appStyles.Content} p-4 `}>
<h1 className={styles.Header}>sign up</h1>
<Form>
<Form onSubmit={handleSubmit}>
<Form.Group className="mb-3" controlId="username">
<Form.Label className="d-none">Username</Form.Label>
<Form.Control
className={styles.Input}
type="text"
placeholder="Username"
name="username"
name="username"I
value={username}
onChange={handleChange}
/>
</Form.Group>
{errors.username?.map((message, idx) => (
<Alert variant="warning" key={idx}>
{message}
</Alert>
))}

<Form.Group className="mb-3" controlId="password1">
<Form.Label className="d-none">Password</Form.Label>
Expand All @@ -63,6 +90,11 @@ const SignUpForm = () => {
onChange={handleChange}
/>
</Form.Group>
{errors.password1?.map((message, idx) => (
<Alert variant="warning" key={idx}>
{message}
</Alert>
))}

<Form.Group className="mb-3" controlId="password2">
<Form.Label className="d-none">Confirm password</Form.Label>
Expand All @@ -75,6 +107,11 @@ const SignUpForm = () => {
onChange={handleChange}
/>
</Form.Group>
{errors.password2?.map((message, idx) => (
<Alert variant="warning" key={idx}>
{message}
</Alert>
))}

<Button
className={`${btnStyles.Button} ${btnStyles.Wide} ${btnStyles.Bright}`}
Expand Down

0 comments on commit 417c8d4

Please sign in to comment.