import React, {useEffect, useState} from 'react'
import Modal from 'antd/lib/modal'
import Typography from 'antd/lib/typography';
import "./styles.less"

const { Title  } = Typography;

const JanelaPublicacoes = ({ok, pessoa, isVisible, cancel}) => {
    const [data, setData] = useState([])
    useEffect(() => {
        netuno.service({
            url: '/services/pessoas/publicacoes',
            method: 'POST',
            credentials: 'include',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify({
                uid: pessoa.uid
            }),
            success: (response)=> {
                if (response.json) {
                    setData(response.json)
                } else {
                    fail();
                }
            },
            fail: ()=> {
                fail();
            }
        });
    }, [isVisible])
    const fail = ()=> {
        setLoader(false)
        notification["error"]({
            message: 'Error',
            description: 'Data loading error...',
            style: {
                marginTop: 100,
            }
        });
    };
    return(
        <Modal
            title={`Publicações de ${pessoa.nome} ${pessoa.sobrenome}`}
            visible={isVisible}
            onOk={() => ok(false)}
            onCancel={() => cancel(false)}>
            {data.map((publicao) => {
                return(
                    <div className="publicacao-container" key={publicao.uid}>
                        <hr />
                        <div>
                            <Title level={5}>{publicao.comunidade}</Title>
                            <span>{publicao.momento.replace(":00.0", "")}</span>
                        </div>
                        <p>{publicao.mensagem}</p>
                    </div>
                )
            })}
        </Modal>
    )
}

export default JanelaPublicacoes