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 { Button, notification } from 'antd';

import Item from "./Item";

import "./index.less";

export default ()=> {
    const [loading, setLoading] = useState(false);
    const [communities, setCommunities] = useState([]);
    if (!_auth.isLogged()) {
        return <Redirect to="/login" />;
    }
    useEffect(()=> {
        _service({
            url: "api/communities",
            success: (response) => {
                setCommunities(response.json);
            },
            fail: (e) => {
                console.log("Service Error", e);
                notification["error"]({
                    message: 'Informações',
                    description: 'Não foi possível carregar a informação.',
                });
            }
        });
    }, []);
    return (
        <div className="communities">
            <h1>Comunidades...</h1>
            <ul className="communities__list">
            { communities.length > 0 && communities.map(
                (community) => {
                    return <Item data={ community }/>;
                }
            ) }
            </ul>
        </div>
    );
};
