import React, { useState, useEffect } from "react";

import { Redirect } from "react-router-dom";

import { Button } from 'antd';

import _auth from '@netuno/auth-client';

import _service from '@netuno/service-client';

export default () => {
    if (!_auth.isLogged()) {
        return <Redirect to="/login"/>;
    }
    const [ loading, setLoading ] = useState(false);
    const [ userInfo, setUserInfo ] = useState(null);
    useEffect(()=> {
        _service({
            method: 'GET',
            url: "api/info",
            success: (response) => {
                setUserInfo(response.json);
            },
            fail: (e) => {
                console.log("Service User Info Error", e);
            }
        });
    }, []);
    const onLogout = ()=> {
        _auth.logout();
        setLoading(true);
        window.location = '/';
    }
    return (
        <div>
            <h1>Olá { userInfo && userInfo.name }...</h1>
            <Button onClick={onLogout} loading={loading}>Sair</Button>
        </div>
    );
};
