import React from 'react'
import Map from './Map'
import { levelColor, iconCategory } from '../utils/FunctionUtils'
import { urlContents, displayArchi } from '../utils/ParametersUtils'
import InsertDriveFileIcon from '@material-ui/icons/InsertDriveFile'
import { fetchCmsAndCategory } from '../utils/ActuFetchUtils'
// News list
class ListesActu extends React.Component {
constructor(props) {
super(props)
this.state={
cms: [],
category: [],
hiddenArchi: true
}
}
componentDidMount() {
// get all newq
fetchCmsAndCategory()
.then(objState => this.setState(objState))
}
// return a map if need
getMap(actu) {
if (actu.x && actu.y) {
return (
)
}
return ''
}
// return a liste to download files if need
getFile(actu) {
if (actu.cmsDocument.length !== 0) {
return actu.cmsDocument.map((file, index) => {
const extention = file.name.split(/\./)[1]
if (extention === 'jpg' || extention === 'png' || extention === 'jpeg') {
return this.getImg(file, index)
}
return (
)
})
}
return ''
}
// return images if need
getImg(img, index) {
return (
)
}
// return the liste of news
getListActu(actus) {
return actus.map((actu, index) => (
{
iconCategory(actu, this.state.category.filter(cat => cat.id === actu.idCategory)[0].icon)
}
{actu.title}
Rédigé par {actu.author} { actu.dateDebut ? 'le '+new Date(actu.dateDebut).toLocaleString() : ''}
{actu.subtitle}
{this.getFile(actu)}
{this.getMap(actu)}
))
}
// diplay archived news
displayArchi() {
if (!this.state.hiddenArchi) {
const idCategories = this.state.category.map(cat => cat.id)
const actus = this.state.cms.filter(actu => (actu.status === 3 || actu.dateFin) && idCategories.includes(actu.idCategory)).sort((a, b) => {
if (b.dateDebut && a.dateDebut) {
return b.dateDebut - a.dateDebut
}
return b.updateDate - a.updateDate
})
if (actus.length !== 0) {
return (
{this.getListActu(actus)}
)
}
return (
Aucune actualité archivée
)
}
return ''
}
render() {
const display = () => {
if (displayArchi) {
return (
this.setState({ hiddenArchi: !this.state.hiddenArchi })} className='displayArchi'>
Afficher les articles historisés
)
}
return ''
}
const idCategories = this.state.category.map(cat => cat.id)
const actus = this.state.cms.filter(actu => actu.status === 1 && !actu.dateFin && idCategories.includes(actu.idCategory)).sort((a, b) => {
if (b.dateDebut && a.dateDebut) {
return b.dateDebut - a.dateDebut
}
return b.updateDate - a.updateDate
})
return (
{this.getListActu(actus)}
{display()}
{this.displayArchi()}
)
}
}
export default ListesActu