index.js 7.6 KB

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