index.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import React from "react";
  2. import ContentWrapper from '@/components/Layout/ContentWrapper';
  3. import { Container, Row, Col, Card, CardBody, CardFooter, CardHeader, Input, Button } from 'reactstrap';
  4. import FormValidator from "@/components/Forms/Validator.js";
  5. import { ptPublic } from "@/actions/PT";
  6. import AsyncSelect from "react-select/async";
  7. import { loginToPt } from "../../../actions/auth";
  8. import { connect } from "react-redux";
  9. import { ToastContainer, toast } from "react-toastify";
  10. import { getCsrf } from "../../../actions/security";
  11. const loadOptions = (inputValue, callback) => {
  12. setTimeout(async () => {
  13. const pt = await ptPublic({ search: inputValue });
  14. const data = pt.data.map((e) => ({
  15. value: e.id,
  16. label: e.nama,
  17. className: "State-ACT",
  18. }));
  19. callback(data);
  20. }, 1000);
  21. };
  22. class Verifikasi extends React.Component {
  23. constructor(props) {
  24. super(props);
  25. this.state = {
  26. formLogin: {
  27. password: ""
  28. },
  29. loading: false,
  30. inputValue: "",
  31. pt_id: ""
  32. };
  33. }
  34. validateOnChange = (event) => {
  35. const input = event.target;
  36. const form = input.form;
  37. const value = input.type === "checkbox" ? input.checked : input.value;
  38. const result = FormValidator.validate(input);
  39. this.setState({
  40. [form.name]: {
  41. ...this.state[form.name],
  42. [input.name]: value,
  43. errors: {
  44. ...this.state[form.name].errors,
  45. [input.name]: result,
  46. },
  47. },
  48. });
  49. };
  50. hasError = (formName, inputName, method) => {
  51. return (
  52. this.state[formName] &&
  53. this.state[formName].errors &&
  54. this.state[formName].errors[inputName] &&
  55. this.state[formName].errors[inputName][method]
  56. );
  57. };
  58. handleChangeSelectPerguruanTinggi = (selected_PT) => {
  59. this.setState({ pt_id: selected_PT.value });
  60. };
  61. onSubmit = async () => {
  62. const getToken = await getCsrf();
  63. const _csrf = getToken.token;
  64. this.setState({ loading: true });
  65. const { password } = this.state.formLogin;
  66. const { pt_id } = this.state
  67. const auth = await toast.promise(loginToPt(pt_id, password, _csrf), {
  68. pending: "Loading",
  69. success: "Success",
  70. error: "Akun tidak ada",
  71. });
  72. // const auth = await loginToPt(pt_id, password);
  73. this.props.setToken(auth.data.token);
  74. this.props.setUser(auth.data.user);
  75. if (auth.data.user.role.id === 2022) {
  76. location.href = "/pt/pemantauan"
  77. return;
  78. } else if ([2020, 2021, 2023].includes(auth.data.user.role.id)) {
  79. location.href = "/pt/pemantauan"
  80. return;
  81. }
  82. this.setState({ loading: false });
  83. }
  84. render() {
  85. return (
  86. <ContentWrapper>
  87. <div className="content-heading">
  88. <span className="font-color-white">
  89. Verifikasi
  90. </span>
  91. </div>
  92. <Container className="container-sm pt-5" >
  93. <Card
  94. style={{ margin: "20px", borderRadius: "15px" }}
  95. >
  96. <CardHeader className="text-center">
  97. <div className="card-title font-weight-bold mt-4 font-color-black"
  98. style={{ fontSize: "20px" }}
  99. >Login sebagai Perguruan Tinggi</div>
  100. </CardHeader>
  101. <CardBody>
  102. <form onSubmit={this.onSubmit} method="post" name="formLogin">
  103. <div className="form-group">
  104. <label className="col-form-label font-color-black mb-0 font-weight-bold">Password akun PDDIKTI</label>
  105. <Input
  106. style={{ borderRadius: "7px" }}
  107. type="password"
  108. id="id-password"
  109. name="password"
  110. invalid={this.hasError("formLogin", "password", "required")}
  111. onChange={this.validateOnChange}
  112. data-validate='["required"]'
  113. value={this.state.formLogin.password}
  114. />
  115. <span className="invalid-feedback">Wajib diisi</span>
  116. </div>
  117. <div className="form-group">
  118. <label className="col-form-label font-color-black mb-0 font-weight-bold">Perguruan Tinggi</label>
  119. <AsyncSelect
  120. style={{ borderRadius: "7px" }}
  121. cacheOptions
  122. loadOptions={loadOptions}
  123. defaultOptions
  124. onChange={(e) => {
  125. this.handleChangeSelectPerguruanTinggi(e);
  126. }}
  127. // onInputChange={this.handleInputChange}
  128. />
  129. <span className="invalid-feedback">Field is required</span>
  130. </div>
  131. <Button color className="btn-login float-right mt-3"
  132. style={{ borderRadius: "7px" }}
  133. onClick={this.onSubmit}
  134. >
  135. <span className="font-color-white">
  136. {this.state.loading ? (
  137. <div class="d-flex justify-content-center">
  138. <div
  139. class="spinner-border spinner-border-sm"
  140. role="status"
  141. ></div>
  142. </div>
  143. ) : (
  144. "Login"
  145. )}
  146. </span>
  147. </Button>
  148. </form>
  149. </CardBody>
  150. </Card>
  151. </Container>
  152. </ContentWrapper>)
  153. }
  154. }
  155. const mapStateToProps = (state) => ({ user: state.user });
  156. const mapDispatchToProps = (dispatch) => ({
  157. setUser: (payload) => dispatch({ type: "SET_USER", payload }),
  158. setToken: (payload) => dispatch({ type: "SET_TOKEN", payload }),
  159. setPT: (payload) => dispatch({ type: "SET_PT", payload }),
  160. });
  161. export default connect(mapStateToProps, mapDispatchToProps)(Verifikasi);