import React, { Component } from "react"; import { Input, Card, CardBody, Button } from "reactstrap"; import Router from "next/router"; import FormValidator from "@/components/Forms/Validator.js"; import { connect } from "react-redux"; import { login, getUser } from "@/actions/auth"; import { createLog } from "@/actions/log"; import axiosAPI from "@/config/axios"; import { getPT } from "@/actions/PT"; import { getCsrf } from "../../actions/security"; class Login extends Component { constructor(props) { super(props); this.state = { /* Group each form state in an object. Property name MUST match the form name */ error: null, formLogin: { username: "", password: "", }, loading: false, }; } /** * Validate input using onChange event * @param {String} formName The name of the form in the state object * @return {Function} a function used for the event */ validateOnChange = (event) => { const input = event.target; const form = input.form; const value = input.type === "checkbox" ? input.checked : input.value; const result = FormValidator.validate(input); this.setState({ [form.name]: { ...this.state[form.name], [input.name]: value, errors: { ...this.state[form.name].errors, [input.name]: result, }, }, }); }; onSubmit = async (e) => { try { const form = e.target; const inputs = [...form.elements].filter((i) => ["INPUT", "SELECT"].includes(i.nodeName) ); const { errors, hasError } = FormValidator.bulkValidate(inputs); this.setState({ [form.name]: { ...this.state[form.name], errors, }, }); console.log(hasError ? "Form has errors. Check!" : "Form Submitted!"); e.preventDefault(); if (!hasError) { const getTokenCsrf = await getCsrf(); const _csrf = getTokenCsrf.token; this.setState({ loading: true }); const { username, password } = this.state.formLogin; const auth = await login(username, password); this.props.setToken(auth.data.token); this.props.setUser(auth.data.user); axiosAPI.defaults.headers.common["Authorization"] = auth.data.token; if (auth.data.user.role.id === 2022) { await createLog(auth.data.token, { aktivitas: "PT berhasil Login", _csrf: _csrf }); return location.href = '/pt/pemantauan'; } else if ([2020, 2021, 2023, 2024].includes(auth.data.user.role.id)) { await createLog(auth.data.token, { aktivitas: "Berhasil Login", _csrf: _csrf }); return location.href = '/app/pemantauan'; } else { this.setState({ error: "Akun tidak ada" }); } this.setState({ loading: false }); } } catch (error) { this.setState({ loading: false }); this.setState({ error: error.response?.data.message || error.response }); } }; /* Simplify error check */ hasError = (formName, inputName, method) => { return ( this.state[formName] && this.state[formName].errors && this.state[formName].errors[inputName] && this.state[formName].errors[inputName][method] ); }; render() { return ( Logo {" "}
Login Menggunakan Akun PDDIKTI
{this.state.error &&
{this.state.error}
}
{this.hasError("formLogin", "username", "required") && ( Wajib diisi )}
Wajib diisi
{/*
* Required fields
Login Menggunakan Akun PDDIKTI */}
); } } const mapStateToProps = (state) => ({ user: state.user }); const mapDispatchToProps = (dispatch) => ({ setUser: (payload) => dispatch({ type: "SET_USER", payload }), setToken: (payload) => dispatch({ type: "SET_TOKEN", payload }), setPT: (payload) => dispatch({ type: "SET_PT", payload }), }); export default connect(mapStateToProps, mapDispatchToProps)(Login);