File size: 4,938 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
import React, { useState } from "react";
import { Redirect } from "react-router-dom";
import { Link} from "react-router-dom";
import Layout from "../core/Layout";
import { signin, authenticate, isAuthenticated } from "../auth";
import LoadingOverlay from 'react-loading-overlay';
import avatar from './../image/login/avatar.png';
import sideimage from './../image/login/side.jpg';
import './../CSS/signin.css';
const Signin = () => {
const [values, setValues] = useState({
email: "",
password: "",
error: "",
loading: false,
redirectToReferrer: false
});
const { email, password, loading, error, redirectToReferrer } = values;
const { user } = isAuthenticated();
const handleChange = name => event => {
setValues({ ...values, error: false, [name]: event.target.value });
};
const clickSubmit = event => {
event.preventDefault();
setValues({ ...values, error: false, loading: true });
signin({ email, password }).then(data => {
if (data.error) {
setValues({ ...values, error: data.error, loading: false });
} else {
authenticate(data, () => {
setValues({
...values,
redirectToReferrer: true
});
});
}
});
};
const signUpForm = () => (
<div className="row display-flex mt-3">
<div className="col-sm-8 col-xs-10 col-md-4 offset-md-2 offset-sm-2 offset-xs-1 py-4 px-3 rounded-left" id="login-intro-form">
<h2 className="text-center text-white h2 font-weight-bold mt-2">Hello Globetrotter</h2>
<div className="login-account-wall p-4 mb-2">
<img className="rounded-circle mx-auto d-block" src={avatar} alt="" />
<form>
<div className="form-group mt-3">
<label className="text-white font-weight-bold">Email</label>
<input
onChange={handleChange("email")}
type="email"
className="form-control"
value={email}
/>
</div>
<div className="form-group mt-3">
<label className="text-white font-weight-bold">Password</label>
<input
onChange={handleChange("password")}
type="password"
className="form-control"
value={password}
/>
</div>
<button onClick={clickSubmit} className="btn btn-success btn-block rounded font-weight-bold text-center">
Submit
</button>
</form>
<div className="text-center">
<span className="text-center d-block text-danger font-weight-bold text-italic">or</span>
<Link to="/signup" className="text-white">Create an account </Link>
</div>
</div>
</div>
<div className="col-sm-8 col-xs-10 col-md-4 rounded-right py-4 px-3" id="login-intro">
<h2 className="login-login-title text-center mt-2 h2">Welcome toTravelYaari</h2>
<img className="img-fluid img-rounded" src={sideimage} alt="signup" />
<p className="text-center text-warning">We Are Happy to see you here. We are working to provide the best services to you.</p>
</div>
</div>
);
const showError = () => (
<div
className="alert alert-danger"
style={{ display: error ? "" : "none" }}
>
{error}
</div>
);
const showLoading = () =>
loading && (
// <div className="alert alert-info">
// <h2>Loading...</h2>
// </div>
<LoadingOverlay
active={loading}
spinner
text='Loading......'
className="loader">
</LoadingOverlay>
);
const redirectUser = () => {
if (redirectToReferrer) {
if (user && user.role === 1) {
return <Redirect to="/admin/dashboard" />;
} else {
return <Redirect to="/user/dashboard" />;
}
}
if (isAuthenticated()) {
return <Redirect to="/" />;
}
};
return (
<Layout
className="container-fluid"
>
{showLoading()}
{showError()}
{signUpForm()}
{redirectUser()}
</Layout>
);
};
export default Signin;
|