LoginContainer.tsx 1.57 KB
import React, { useState, useEffect } from 'react';
import { RouteComponentProps } from 'react-router-dom';

import { useRecoilState } from 'recoil';
import * as recoilUtil from '../../util/recoilUtil';

import LoginPresenter from './LoginPresenter';

import { authApi } from '../../api';


type LoginProps = RouteComponentProps

const LoginContainer = (props : LoginProps) => {

    const [loginForm, setLoginForm] = useState({
        userId : '',
        password : '',
    });
    const [token, setToken] = useRecoilState(recoilUtil.token);
    const [userType, setUserType] = useRecoilState(recoilUtil.userType);

    const onSetUserId = (e : React.ChangeEvent<HTMLInputElement>) => {
        setLoginForm({
            ...loginForm,
            userId : e.target.value,
        });
    };

    const onSetPassword = (e : React.ChangeEvent<HTMLInputElement>) => {
        setLoginForm({
            ...loginForm,
            password : e.target.value,
        });
    };

    const onLogin = async () => {
        try {
            const result : any = await authApi.login(loginForm);
            if(result.statusText === 'OK') {
                setToken(result.data.token);
                props.history.push('/');
            }
        } catch(e) {
            console.log(e);
        }

    };

    useEffect(() => {
        console.log('loginPage');
    }, []);

    return (
        <LoginPresenter
            loginForm = {loginForm}
            onSetUserId = {onSetUserId}
            onSetPassword = {onSetPassword}
            onLogin = {onLogin}
        />
    )
};

export default LoginContainer;