Grégoire Delattre 9a37677d52
All checks were successful
continuous-integration/drone/push Build is passing
continuous-integration/drone/pr Build is passing
Update redux state management
Use immer with native javascript objects instead of immutablejs.
2020-04-07 18:08:47 +02:00

93 lines
2.7 KiB
JavaScript

import React, { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { Redirect } from "react-router-dom";
import { userSignUp } from "../../actions/users";
export const UserSignUp = () => {
const dispatch = useDispatch();
const [username, setUsername] = useState("");
const [password, setPassword] = useState("");
const [passwordConfirm, setPasswordConfirm] = useState("");
const isLogged = useSelector((state) => state.user.isLogged);
const isLoading = useSelector((state) => state.user.loading);
const error = useSelector((state) => state.user.error);
if (isLogged) {
return <Redirect to="/" />;
}
const handleSubmit = (e) => {
e.preventDefault();
dispatch(
userSignUp({
username: username,
password: password,
password_confirm: passwordConfirm, // eslint-disable-line camelcase
})
);
};
return (
<div className="Row">
<div className="col-10 offset-1 col-md-6 offset-md-3">
<h2>Sign up</h2>
<hr />
{error && error !== "" && (
<div className="alert alert-danger">{error}</div>
)}
<form className="form-horizontal" onSubmit={(e) => handleSubmit(e)}>
<div className="form-group">
<label className="control-label">Username</label>
<input
autoFocus="autofocus"
className="form-control"
value={username}
onChange={(e) => setUsername(e.target.value)}
/>
</div>
<div className="form-group">
<label className="control-label">Password</label>
<input
className="form-control"
type="password"
value={password}
onChange={(e) => setPassword(e.target.value)}
/>
</div>
<div className="form-group">
<label className="control-label">Password confirm</label>
<input
className="form-control"
type="password"
value={passwordConfirm}
onChange={(e) => setPasswordConfirm(e.target.value)}
/>
</div>
<div>
{isLoading && (
<button className="btn btn-primary pull-right">
<i className="fa fa-spinner fa-spin"></i>
</button>
)}
{isLoading || (
<span>
<input
className="btn btn-primary pull-right"
type="submit"
value="Sign up"
/>
</span>
)}
</div>
</form>
</div>
</div>
);
};