This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/website/components/FunctionCard/FunctionCard.jsx

153 lines
4.3 KiB
React
Raw Permalink Normal View History

2020-08-03 12:04:07 +02:00
import Link from 'next/link'
import { useState, forwardRef, memo } from 'react'
2020-08-03 12:04:07 +02:00
import date from 'date-and-time'
import Loader from '../Loader'
import { API_URL } from '../../utils/api'
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-12-28 13:07:21 +01:00
const handleError = (event) => {
2020-08-03 14:14:45 +02:00
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 (
2020-12-28 13:07:21 +01:00
<>
<Link
{...(props.isAdmin
? {
href: '/admin/[slug]',
as: `/admin/${props.slug}`
}
: {
href: isFormOrArticle
? '/functions/[slug]'
: `/functions/${props.slug}`,
as: `/functions/${props.slug}`
})}
2020-08-03 14:14:45 +02:00
>
2020-12-28 13:07:21 +01:00
{/* FunctionCard a une hauteur pendant chargement */}
<a
ref={ref}
style={
isLoading ? { height: '360px', justifyContent: 'center' } : null
}
2020-12-28 13:11:02 +01:00
className='FunctionCard col-sm-24 col-md-10 col-xl-7'
2020-08-03 14:14:45 +02:00
>
2020-12-28 13:11:02 +01:00
{isLoading && <Loader width='125px' height='125px' />}
2020-12-28 13:07:21 +01:00
<div
className={`FunctionCard__container ${isLoading ? 'd-none' : ''}`}
>
2020-12-28 13:11:02 +01:00
<div className='FunctionCard__top'>
2020-12-28 13:07:21 +01:00
<img
onLoad={handleLoad}
onError={handleError}
2020-12-28 13:11:02 +01:00
className='FunctionCard__image'
2020-12-28 13:07:21 +01:00
alt={props.title}
src={API_URL + props.image}
/>
2020-12-28 13:11:02 +01:00
<h2 className='FunctionCard__title'>{props.title}</h2>
<p className='FunctionCard__description text-center'>
2020-12-28 13:07:21 +01:00
{props.description}
</p>
</div>
2020-12-28 13:11:02 +01:00
<div className='FunctionCard__info'>
2020-12-28 13:07:21 +01:00
<p
2020-12-28 13:11:02 +01:00
className='FunctionCard__category'
2020-12-28 13:07:21 +01:00
style={{ backgroundColor: props.categorie.color }}
>
{props.categorie.name}
</p>
2020-12-28 13:11:02 +01:00
<p className='FunctionCard__publication-date'>
2020-12-28 13:07:21 +01:00
{date.format(new Date(props.createdAt), 'DD/MM/YYYY', false)}
</p>
</div>
2020-08-03 14:14:45 +02:00
</div>
2020-12-28 13:07:21 +01:00
</a>
</Link>
<style jsx>{`
.FunctionCard {
display: flex;
align-items: center;
position: relative;
flex-direction: column;
word-wrap: break-word;
box-shadow: 0px 0px 6px 6px rgba(0, 0, 0, 0.25);
border: 1px solid black;
border-radius: 1rem;
margin: 0 0 50px 0;
cursor: pointer;
transition: all 0.3s;
color: var(--text-color);
text-decoration: none !important;
}
.FunctionCard__container {
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
}
.FunctionCard:hover {
transform: translateY(-7px);
}
/* col-md */
@media (min-width: 768px) {
.FunctionCard {
margin: 0 30px 50px 30px;
}
}
/* col-xl */
@media (min-width: 1200px) {
.FunctionCard {
margin: 0 20px 50px 20px;
}
}
.FunctionCard__top {
display: flex;
flex-direction: column;
align-items: center;
flex-grow: 1;
}
.FunctionCard__image {
width: 150px;
}
.FunctionCard__title {
font-size: 1.4em;
margin: 0;
color: var(--important);
font-weight: 300;
}
.FunctionCard__description {
margin: 20px 0 10px 0;
}
.FunctionCard__info {
display: flex;
justify-content: center;
align-items: center;
width: 100%;
}
.FunctionCard__category {
border-radius: 0.5em;
padding: 0.5em;
margin-right: 20px;
font-size: 16.4px;
}
2020-12-28 13:11:02 +01:00
`}
</style>
2020-12-28 13:07:21 +01:00
</>
2020-08-03 14:14:45 +02:00
)
})
)
2020-08-03 12:04:07 +02:00
export default FunctionCard