2020-08-03 12:04:07 +02:00
|
|
|
import { useState, useEffect, useContext } from 'react'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
|
|
|
|
import { faStar } from '@fortawesome/free-solid-svg-icons'
|
|
|
|
import { faStar as farStar } from '@fortawesome/free-regular-svg-icons'
|
|
|
|
import date from 'date-and-time'
|
|
|
|
import { UserContext } from '../../contexts/UserContext'
|
2020-08-03 14:37:05 +02:00
|
|
|
import api, { API_URL } from '../../utils/api'
|
2020-03-24 09:54:50 +01:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const FunctionComponentTop = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const { isAuth, user } = useContext(UserContext)
|
|
|
|
const [isFavorite, setIsFavorite] = useState(false)
|
2020-04-10 22:50:24 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
useEffect(() => {
|
|
|
|
if (isAuth && user.token != null) {
|
|
|
|
fetchFavorite()
|
2020-04-10 22:50:24 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}, [isAuth])
|
2020-04-10 22:50:24 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const fetchFavorite = async () => {
|
|
|
|
try {
|
2020-08-03 14:14:45 +02:00
|
|
|
const favoriteResponse = await api.get(`/favorites/${props.id}`, {
|
|
|
|
headers: { Authorization: user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
setIsFavorite(favoriteResponse.data.isFavorite)
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const toggleFavorite = async () => {
|
|
|
|
if (isAuth && user.token != null) {
|
|
|
|
try {
|
|
|
|
if (isFavorite) {
|
2020-08-03 14:14:45 +02:00
|
|
|
const response = await api.delete(`/favorites/${props.id}`, {
|
|
|
|
headers: { Authorization: user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
if (response.status === 200) return setIsFavorite(false)
|
2020-04-10 22:50:24 +02:00
|
|
|
}
|
2020-08-03 14:14:45 +02:00
|
|
|
const response = await api.post(
|
|
|
|
`/favorites/${props.id}`,
|
|
|
|
{},
|
|
|
|
{ headers: { Authorization: user.token } }
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
if (response.status === 201) return setIsFavorite(true)
|
|
|
|
} catch {}
|
2020-04-10 22:50:24 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
2020-04-10 22:50:24 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleError = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
event.target.src = API_URL + '/images/functions/default.png'
|
|
|
|
}
|
2020-05-01 02:27:30 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
|
|
|
<div className='container-fluid'>
|
|
|
|
<div className='row justify-content-center text-center'>
|
|
|
|
<div className='FunctionComponent__top col-24'>
|
2020-08-03 14:14:45 +02:00
|
|
|
{isAuth && (
|
|
|
|
<FontAwesomeIcon
|
|
|
|
onClick={toggleFavorite}
|
|
|
|
{...(isFavorite ? { icon: faStar } : { icon: farStar })}
|
|
|
|
title={
|
|
|
|
isFavorite
|
|
|
|
? 'Retirer la fonction des favoris'
|
|
|
|
: 'Ajouter la fonction aux favoris'
|
|
|
|
}
|
|
|
|
className='FunctionComponent__star-favorite'
|
|
|
|
/>
|
|
|
|
)}
|
2020-04-10 22:50:24 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
<img
|
|
|
|
onError={handleError}
|
|
|
|
className='FunctionComponent__image'
|
|
|
|
src={API_URL + props.image}
|
|
|
|
alt={props.title}
|
|
|
|
/>
|
|
|
|
<h1 className='FunctionComponent__title title-important'>
|
|
|
|
{props.title}
|
|
|
|
</h1>
|
2020-08-03 12:04:07 +02:00
|
|
|
<p className='FunctionComponent__description'>{props.description}</p>
|
|
|
|
<div className='FunctionCard__info'>
|
|
|
|
<Link href={`/functions?categoryId=${props.categorieId}`}>
|
2020-08-03 14:14:45 +02:00
|
|
|
<a
|
|
|
|
className='FunctionCard__category'
|
|
|
|
style={{
|
|
|
|
backgroundColor: props.categorie.color,
|
|
|
|
color: 'inherit'
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{props.categorie.name}
|
|
|
|
</a>
|
2020-08-03 12:04:07 +02:00
|
|
|
</Link>
|
2020-08-03 14:14:45 +02:00
|
|
|
<p className='FunctionCard__publication-date'>
|
2020-08-04 11:42:21 +02:00
|
|
|
{date.format(new Date(props.createdAt), 'DD/MM/YYYY', false)}
|
2020-08-03 14:14:45 +02:00
|
|
|
</p>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-03-24 09:54:50 +01:00
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
2020-04-10 22:50:24 +02:00
|
|
|
}
|
2020-03-24 09:54:50 +01:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default FunctionComponentTop
|