2020-04-10 22:50:24 +02:00
import { useState , useEffect , useContext } from 'react' ;
2020-03-24 09:54:50 +01:00
import Link from 'next/link' ;
2020-04-10 22:50:24 +02:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' ;
import { faStar } from '@fortawesome/free-solid-svg-icons' ;
import { faStar as farStar } from '@fortawesome/free-regular-svg-icons' ;
2020-04-12 12:40:56 +02:00
import date from 'date-and-time' ;
2020-04-23 17:31:36 +02:00
import { UserContext } from '../../contexts/UserContext' ;
import api from '../../utils/api' ;
import { API _URL } from '../../utils/config' ;
import '../FunctionCard/FunctionCard.css' ;
2020-03-24 09:54:50 +01:00
2020-04-10 22:50:24 +02:00
const FunctionComponentTop = ( props ) => {
const { isAuth , user } = useContext ( UserContext ) ;
const [ isFavorite , setIsFavorite ] = useState ( false ) ;
useEffect ( ( ) => {
if ( isAuth && user . token != undefined ) {
fetchFavorite ( ) ;
}
} , [ isAuth ] ) ;
const fetchFavorite = async ( ) => {
try {
const favoriteResponse = await api . get ( ` /favorites/ ${ props . id } ` , { headers : { 'Authorization' : user . token } } ) ;
setIsFavorite ( favoriteResponse . data . isFavorite ) ;
} catch { }
}
const toggleFavorite = async ( ) => {
if ( isAuth && user . token != undefined ) {
try {
if ( isFavorite ) {
const response = await api . delete ( ` /favorites/ ${ props . id } ` , { headers : { 'Authorization' : user . token } } ) ;
if ( response . status === 200 ) return setIsFavorite ( false ) ;
}
const response = await api . post ( ` /favorites/ ${ props . id } ` , { } , { headers : { 'Authorization' : user . token } } ) ;
if ( response . status === 201 ) return setIsFavorite ( true ) ;
} catch { }
}
}
return (
< div className = "container-fluid" >
< div className = "row justify-content-center text-center" >
< div className = "FunctionComponent__top col-24" >
{ ( 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-12 12:40:56 +02:00
< img className = "FunctionComponent__image" src = { API _URL + props . image } alt = { props . title } / >
2020-04-10 22:50:24 +02:00
< h1 className = "FunctionComponent__title title-important" > { props . title } < / h 1 >
< p className = "FunctionComponent__description" > { props . description } < / p >
< div className = "FunctionCard__info" >
< Link href = { ` /functions?categoryId= ${ props . categorieId } ` } >
< a className = "FunctionCard__category" style = { { backgroundColor : props . categorie . color , color : 'inherit' } } > { props . categorie . name } < / a >
< / L i n k >
2020-04-12 12:40:56 +02:00
< p className = "FunctionCard__publication-date" > { date . format ( new Date ( props . createdAt ) , 'DD/MM/YYYY' , true ) } < / p >
2020-04-10 22:50:24 +02:00
< / d i v >
2020-03-24 09:54:50 +01:00
< / d i v >
< / d i v >
< / d i v >
2020-04-10 22:50:24 +02:00
) ;
}
2020-03-24 09:54:50 +01:00
export default FunctionComponentTop ;