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

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

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

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

import Item from './Item';

import './index.less';

export default ()=> {
    if (!_auth.isLogged()) {
        return <Redirect to="/login" />;
    }
    const [ data, setData ] = useState([]);
    useEffect(() => {
        _service({
            url: 'public/communities',
            success: (response) => {
                setData(response.json);
            },
            fail: (e) => {
                console.error('Service Communities Error', e);
            }
        });
    }, []);
    return (
        <div>
            <h1>Comunidades</h1>
            <div class="communities">
                <ul class="communities__list">
                    { data.map((community)=> (
                        <Item data={ community } />
                    ))}
                </ul>
            </div>
        </div>
    );
};