2020-08-03 12:04:07 +02:00
|
|
|
import Link from 'next/link'
|
2020-08-03 12:21:35 +02:00
|
|
|
import { useState, forwardRef, memo } from 'react'
|
2020-08-03 12:04:07 +02:00
|
|
|
import date from 'date-and-time'
|
|
|
|
import Loader from '../Loader'
|
2020-08-03 14:37:05 +02:00
|
|
|
import { API_URL } from '../../utils/api'
|
2020-08-03 12:04:07 +02:00
|
|
|
import './FunctionCard.css'
|
2020-03-19 16:13:51 +01:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const FunctionCard = memo(
|
|
|
|
forwardRef((props, ref) => {
|
|
|
|
const [isLoading, setIsLoading] = useState(true)
|
2020-08-03 12:04:07 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleLoad = () => {
|
|
|
|
setIsLoading(false)
|
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleError = event => {
|
|
|
|
event.target.src = API_URL + '/images/functions/default.png'
|
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const isFormOrArticle = props.type === 'form' || props.type === 'article'
|
2020-08-03 12:04:07 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
return (
|
|
|
|
<Link
|
|
|
|
{...(props.isAdmin
|
2020-08-03 12:04:07 +02:00
|
|
|
? {
|
|
|
|
href: '/admin/[slug]',
|
|
|
|
as: `/admin/${props.slug}`
|
|
|
|
}
|
|
|
|
: {
|
2020-08-03 14:14:45 +02:00
|
|
|
href: isFormOrArticle
|
|
|
|
? '/functions/[slug]'
|
|
|
|
: `/functions/${props.slug}`,
|
2020-08-03 12:04:07 +02:00
|
|
|
as: `/functions/${props.slug}`
|
2020-08-03 14:14:45 +02:00
|
|
|
})}
|
|
|
|
>
|
|
|
|
{/* FunctionCard a une hauteur pendant chargement */}
|
|
|
|
<a
|
|
|
|
ref={ref}
|
|
|
|
style={
|
|
|
|
isLoading ? { height: '360px', justifyContent: 'center' } : null
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
2020-08-03 14:14:45 +02:00
|
|
|
className='FunctionCard col-sm-24 col-md-10 col-xl-7'
|
|
|
|
>
|
|
|
|
{isLoading && <Loader width='125px' height='125px' />}
|
2020-08-03 12:21:35 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
<div
|
|
|
|
className={`FunctionCard__container ${isLoading ? 'd-none' : ''}`}
|
|
|
|
>
|
|
|
|
<div className='FunctionCard__top'>
|
|
|
|
<img
|
|
|
|
onLoad={handleLoad}
|
|
|
|
onError={handleError}
|
|
|
|
className='FunctionCard__image'
|
|
|
|
alt={props.title}
|
|
|
|
src={API_URL + props.image}
|
|
|
|
/>
|
|
|
|
<h2 className='FunctionCard__title'>{props.title}</h2>
|
|
|
|
<p className='FunctionCard__description text-center'>
|
|
|
|
{props.description}
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
<div className='FunctionCard__info'>
|
|
|
|
<p
|
|
|
|
className='FunctionCard__category'
|
|
|
|
style={{ backgroundColor: props.categorie.color }}
|
|
|
|
>
|
|
|
|
{props.categorie.name}
|
|
|
|
</p>
|
|
|
|
<p className='FunctionCard__publication-date'>
|
|
|
|
{date.format(new Date(props.createdAt), 'DD/MM/YYYY', true)}
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-08-03 14:14:45 +02:00
|
|
|
</a>
|
|
|
|
</Link>
|
|
|
|
)
|
|
|
|
})
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
|
|
|
|
export default FunctionCard
|