Login.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import React, { Component } from "react";
  2. import { Input, Card, CardBody, Button } from "reactstrap";
  3. import Router from "next/router";
  4. import FormValidator from "@/components/Forms/Validator.js";
  5. import { connect } from "react-redux";
  6. import { login, getUser } from "@/actions/auth";
  7. import axiosAPI from "@/config/axios";
  8. import { getPT } from "@/actions/PT";
  9. class Login extends Component {
  10. constructor(props) {
  11. super(props);
  12. this.state = {
  13. /* Group each form state in an object.
  14. Property name MUST match the form name */
  15. error: null,
  16. formLogin: {
  17. username: "",
  18. password: "",
  19. },
  20. };
  21. }
  22. /**
  23. * Validate input using onChange event
  24. * @param {String} formName The name of the form in the state object
  25. * @return {Function} a function used for the event
  26. */
  27. validateOnChange = (event) => {
  28. const input = event.target;
  29. const form = input.form;
  30. const value = input.type === "checkbox" ? input.checked : input.value;
  31. const result = FormValidator.validate(input);
  32. this.setState({
  33. [form.name]: {
  34. ...this.state[form.name],
  35. [input.name]: value,
  36. errors: {
  37. ...this.state[form.name].errors,
  38. [input.name]: result,
  39. },
  40. },
  41. });
  42. };
  43. onSubmit = async (e) => {
  44. try {
  45. const form = e.target;
  46. const inputs = [...form.elements].filter((i) => ["INPUT", "SELECT"].includes(i.nodeName));
  47. const { errors, hasError } = FormValidator.bulkValidate(inputs);
  48. this.setState({
  49. [form.name]: {
  50. ...this.state[form.name],
  51. errors,
  52. },
  53. });
  54. console.log(hasError ? "Form has errors. Check!" : "Form Submitted!");
  55. e.preventDefault();
  56. if (!hasError) {
  57. const { username, password } = this.state.formLogin;
  58. const auth = await login(username, password);
  59. this.props.setToken(auth.data.token);
  60. this.props.setUser(auth.data.user);
  61. axiosAPI.defaults.headers.common["Authorization"] = auth.data.token;
  62. if (auth.data.user.role.id === 2022) {
  63. Router.push({ pathname: "/pt/pemantauan" });
  64. } else if ([2020, 2021].includes(auth.data.user.role.id)) {
  65. Router.push({ pathname: "/app/pemantauan" });
  66. } else {
  67. this.setState({ error: "Akun tidak ada" })
  68. }
  69. }
  70. } catch (error) {
  71. this.setState({ error: error.response.data.message || error.response });
  72. }
  73. };
  74. /* Simplify error check */
  75. hasError = (formName, inputName, method) => {
  76. return this.state[formName] && this.state[formName].errors && this.state[formName].errors[inputName] && this.state[formName].errors[inputName][method];
  77. };
  78. render() {
  79. return (
  80. <Card className="card card-flat">
  81. <img className="img-login-1" src="/static/img/logo-login.png" alt="Logo" />
  82. <CardBody className="card-body">
  83. {" "}
  84. <h5 className="card-title text-left py-2 bg-gray border-radius-login"><img className="icon-triangle" src="/static/img/icon-caution.png"></img><b>Login Menggunakan Akun PDDIKTI </b></h5>
  85. {this.state.error}
  86. <form onSubmit={this.onSubmit} method="post" name="formLogin">
  87. <div className="form-group">
  88. <label className="col-form-label">Username <span className="star-color">*</span></label>
  89. <Input type="text" name="username" invalid={this.hasError("formLogin", "username", "required")} onChange={this.validateOnChange} data-validate='["required"]' value={this.state.formLogin.username} />
  90. {this.hasError("formLogin", "username", "required") && <span className="invalid-feedback">Field is required</span>}
  91. </div>
  92. <div className="form-group">
  93. <label className="col-form-label">Password <span className="star-color">*</span></label>
  94. <Input
  95. type="password"
  96. id="id-password"
  97. name="password"
  98. invalid={this.hasError("formLogin", "password", "required")}
  99. onChange={this.validateOnChange}
  100. data-validate='["required"]'
  101. value={this.state.formLogin.password}
  102. />
  103. <span className="invalid-feedback">Field is required</span>
  104. </div>
  105. {/* <div className="required">* Required fields</div>
  106. <span>Login Menggunakan Akun PDDIKTI</span> */}
  107. <Button color="info" type="submit" block className=" mt-3 btn-login">
  108. <text className="text-login"><b>Login</b></text>
  109. </Button>
  110. </form>
  111. </CardBody>
  112. </Card>
  113. );
  114. }
  115. }
  116. const mapStateToProps = (state) => ({ user: state.user });
  117. const mapDispatchToProps = (dispatch) => ({
  118. setUser: (payload) => dispatch({ type: "SET_USER", payload }),
  119. setToken: (payload) => dispatch({ type: "SET_TOKEN", payload }),
  120. setPT: (payload) => dispatch({ type: "SET_PT", payload }),
  121. });
  122. export default connect(mapStateToProps, mapDispatchToProps)(Login);