// import React, { Component,useState } from 'react'; import React, { Component} from 'react'; import BasePage from '@/components/Layout/BasePage'; import Link from 'next/link'; import { Input, CustomInput } from 'reactstrap'; import * as actions from '../store/actions/actions'; import FormValidator from '@/components/Forms/Validator.js'; import { dispatch } from 'd3'; import Router, { withRouter } from 'next/router' // import useUser from '../lib/useUser'; // import fetchJson from '../lib/fetchJson'; const year = new Date().getFullYear() class Login extends Component { // mutateUser = useUser({ // redirectTo: '/profile-sg', // redirectIfFound: true, // }) // [errorMsg, setErrorMsg] = useState('') state = { formLogin: { uname: '', 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!'); e.preventDefault(); if (!hasError) { //this.props.logged('signin') //Router.push('/singleview'); // const body = { // username: this.state.formLogin.uname, // password: this.state.formLogin.password // } // try { // mutateUser( // await fetchJson('/api/login', { // method: 'POST', // headers: { 'Content-Type': 'application/json' }, // body: JSON.stringify(body), // }) // ) // } catch (error) { // console.error('An unexpected error happened:', error) // setErrorMsg(error.data.message) // } //main route Router.push('/app/projects'); }; } /* 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 (
); } } Login.Layout = BasePage; export default Login;