import { connect } from "react-redux"; // import Router, { withRouter } from "next/router"; import { login } from "@/store/actions/user"; import dataUser from "../json/dataUser"; import React, { Component } from "react"; import BasePage from "@/components/Layout/BasePage"; import { Row, Col, Input, Card, CardHeader, CardBody, CardFooter, CustomInput } from "reactstrap"; import FormValidator from "@/components/Forms/Validator.js"; class Login extends Component { state = { /* Group each form state in an object. Property name MUST match the form name */ formLogin: { username: "", password: "", }, }; /** * 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 = (e) => { 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!"); if (!hasError) { const { username, password } = this.state.formLogin; let user = dataUser.filter((e) => e.username === username && e.password === password); if (user.length) { user = user[0]; console.log(user); } this.dispatch(login(username, password)); e.preventDefault(); } }; /* 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 (
