95 lines
2.7 KiB
JavaScript
95 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";
|
|
|
|
const UserSignUp = () => {
|
|
const dispatch = useDispatch();
|
|
|
|
const [username, setUsername] = useState("");
|
|
const [password, setPassword] = useState("");
|
|
const [passwordConfirm, setPasswordConfirm] = useState("");
|
|
|
|
const isLogged = useSelector((state) => state.userStore.get("isLogged"));
|
|
const isLoading = useSelector((state) => state.userStore.get("loading"));
|
|
const error = useSelector((state) => state.userStore.get("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>
|
|
);
|
|
};
|
|
|
|
export default UserSignUp;
|