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

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

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

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

import Core from "../../Core";

import { Button, notification } from 'antd';

export default ()=> {
    const [loading, setLoading] = useState(false);
    const [data, setData] = useState(null);
    if (!_auth.isLogged()) {
        return <Redirect to="/login" />;
    }
    useEffect(()=> {
        _service({
            url: "info",
            success: (response) => {
                setData(response.json);
            },
            fail: (e) => {
                console.log("Service Error", e);
                notification["error"]({
                    message: 'Informações',
                    description: 'Não foi possível carregar a informação.',
                });
            }
        });
    }, []);
    const onLogout = ()=> {
        _auth.logout();
        setLoading(true);
        Core.events.logout();
        //window.location = '/login';
    }
    return (
        <div>
            <h1>Olá! { data && <h3>{data.name}</h3> }...</h1>
            <Button onClick={onLogout} loading={loading}>Sair</Button>
        </div>
    );
};
