index.js 7.0 KB

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