2020-04-06 23:06:21 +02:00
|
|
|
import Link from 'next/link';
|
2020-04-09 11:32:33 +02:00
|
|
|
import { Fragment, useContext, useState, useEffect } from 'react';
|
2020-04-08 00:47:24 +02:00
|
|
|
import { UserContext } from '../../contexts/UserContext';
|
|
|
|
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
|
|
|
|
import { faPen, faTimes } from '@fortawesome/free-solid-svg-icons';
|
2020-04-06 23:06:21 +02:00
|
|
|
import date from 'date-and-time';
|
|
|
|
import HeadTag from '../../components/HeadTag';
|
|
|
|
import FunctionCard from '../../components/FunctionCard/FunctionCard';
|
2020-04-08 00:47:24 +02:00
|
|
|
import Modal from '../../components/Modal';
|
2020-04-06 23:06:21 +02:00
|
|
|
import redirect from '../../utils/redirect';
|
2020-04-08 00:47:24 +02:00
|
|
|
import htmlParser from 'html-react-parser';
|
|
|
|
import Loader from '../../components/Loader';
|
2020-04-06 23:06:21 +02:00
|
|
|
import api from '../../utils/api';
|
|
|
|
import { API_URL } from '../../utils/config';
|
|
|
|
import '../../public/css/pages/profile.css';
|
|
|
|
|
|
|
|
const Profile = (props) => {
|
|
|
|
|
2020-04-08 00:47:24 +02:00
|
|
|
const { isAuth, user, logoutUser } = useContext(UserContext);
|
2020-04-08 15:26:18 +02:00
|
|
|
const [isOpen, setIsOpen] = useState(false);
|
2020-04-09 11:32:33 +02:00
|
|
|
const [inputState, setInputState] = useState({});
|
|
|
|
const [message, setMessage] = useState("");
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isAuth) {
|
|
|
|
setInputState({ name: user.name, email: user.email, biography: user.biography, isPublicEmail: user.isPublicEmail });
|
|
|
|
}
|
|
|
|
}, [isAuth]);
|
2020-04-08 00:47:24 +02:00
|
|
|
|
|
|
|
const toggleModal = () => setIsOpen(!isOpen);
|
|
|
|
|
|
|
|
const handleChange = (event, isTypeCheck = false) => {
|
|
|
|
const inputStateNew = { ...inputState };
|
|
|
|
inputStateNew[event.target.name] = (event.target.files != undefined) ? event.target.files[0] : (isTypeCheck) ? event.target.checked : event.target.value;
|
|
|
|
setInputState(inputStateNew);
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleSubmit = (event) => {
|
|
|
|
event.preventDefault();
|
|
|
|
const token = user.token;
|
|
|
|
if (isAuth && token != undefined) {
|
|
|
|
setIsLoading(true);
|
|
|
|
const formData = new FormData();
|
|
|
|
formData.append('name', inputState.name);
|
|
|
|
formData.append('email', inputState.email);
|
|
|
|
formData.append('biography', inputState.biography);
|
|
|
|
formData.append('isPublicEmail', inputState.isPublicEmail);
|
|
|
|
formData.append('logo', inputState.logo);
|
|
|
|
|
2020-04-10 20:20:07 +02:00
|
|
|
api.put('/users/', formData, { headers: { 'Authorization': token } })
|
2020-04-08 00:47:24 +02:00
|
|
|
.then(() => {
|
|
|
|
setIsLoading(false);
|
|
|
|
logoutUser();
|
|
|
|
redirect({}, '/login?isSuccessEdit=true');
|
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setMessage(`<p class="form-error"><b>Erreur:</b> ${error.response.data.message}</p>`);
|
|
|
|
setIsLoading(false);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 23:06:21 +02:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<HeadTag title={`${props.name} - FunctionProject`} description={`Profil utilisateur de ${props.name}. ${(props.biography != undefined) ? props.biography : ""}`} />
|
|
|
|
|
2020-04-08 00:47:24 +02:00
|
|
|
{/* Édition du profil */}
|
2020-04-11 23:29:22 +02:00
|
|
|
{(isOpen) ?
|
|
|
|
<Modal toggleModal={toggleModal}>
|
|
|
|
<div className="Profile__container container-fluid">
|
|
|
|
<div className="Profile__row row">
|
|
|
|
<div className="col-24">
|
|
|
|
<div className="Profile__Modal-top-container row">
|
|
|
|
<div className="col-24">
|
|
|
|
<span onClick={toggleModal} style={{ cursor: 'pointer', position: 'absolute', left: 0 }}>
|
|
|
|
<FontAwesomeIcon icon={faTimes} style={{ width: '1.5rem', color: 'red' }} />
|
|
|
|
</span>
|
|
|
|
<h2 className="text-center">Éditer le profil</h2>
|
|
|
|
<p className="text-center"><em>(Vous devrez vous reconnecter après la sauvegarde) <br/> Si vous changez votre adresse email, vous devrez la confirmer comme à l'inscription (vérifier vos emails).</em></p>
|
|
|
|
</div>
|
2020-04-08 00:47:24 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
<div className="col-24">
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="form-label" htmlFor="name">Nom :</label>
|
|
|
|
<input value={inputState.name} onChange={handleChange} type="text" name="name" id="name" className="form-control" placeholder="Divlo" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="form-label" htmlFor="email">Email :</label>
|
|
|
|
<input value={inputState.email} onChange={handleChange} type="email" name="email" id="email" className="form-control" placeholder="email@gmail.com" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group custom-control custom-switch">
|
|
|
|
<input onChange={(event) => handleChange(event, true)} type="checkbox" name="isPublicEmail" checked={inputState.isPublicEmail} className="custom-control-input" id="isPublicEmail" />
|
|
|
|
<label className="custom-control-label" htmlFor="isPublicEmail">Email Public</label>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="form-label" htmlFor="biography">Biographie :</label>
|
|
|
|
<textarea style={{ height: 'auto' }} value={inputState.biography} onChange={handleChange} name="biography" id="biography" className="form-control" rows="5"></textarea>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group">
|
|
|
|
<label className="form-label" htmlFor="logo">Logo <em>(400x400 recommandé)</em> :</label>
|
|
|
|
<p style={{ margin: 0 }}><em>Si aucun fichier est choisi, le logo ne sera pas modifié.</em></p>
|
|
|
|
<input onChange={handleChange} accept="image/jpeg,image/jpg,image/png,image/gif" type="file" name="logo" id="logo" />
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group text-center">
|
|
|
|
<button type="submit" className="btn btn-dark">Sauvegarder</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className="form-result text-center">
|
|
|
|
{
|
|
|
|
(isLoading) ?
|
|
|
|
<Loader />
|
|
|
|
:
|
|
|
|
htmlParser(message)
|
|
|
|
}
|
2020-04-08 00:47:24 +02:00
|
|
|
</div>
|
2020-04-11 23:29:22 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Modal>
|
2020-04-08 00:47:24 +02:00
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
:
|
2020-04-08 00:47:24 +02:00
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
<div className="container-fluid Profile__container">
|
|
|
|
<div className="row Profile__row">
|
|
|
|
<div className="col-20">
|
|
|
|
<div className="text-center">
|
|
|
|
<h1>Profil de <span className="important">{props.name}</span></h1>
|
|
|
|
</div>
|
|
|
|
<div className="row justify-content-center">
|
2020-04-08 00:47:24 +02:00
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
<div className="col-24 text-center">
|
|
|
|
<img className="Profile__logo" src={API_URL + props.logo} alt={props.name} />
|
2020-04-08 00:47:24 +02:00
|
|
|
</div>
|
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
<div className="col-24 text-center">
|
|
|
|
{(props.biography != undefined) && <p>{props.biography}</p>}
|
|
|
|
{(props.email != undefined) && <p><span className="important">Email :</span> {props.email}</p>}
|
2020-04-12 12:40:56 +02:00
|
|
|
<p><span className="important">Date de création du compte :</span> {date.format(new Date(props.createdAt), 'DD/MM/YYYY à HH:mm', true)}</p>
|
2020-04-08 00:47:24 +02:00
|
|
|
</div>
|
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
{(isAuth && user.name === props.name) &&
|
|
|
|
<button onClick={toggleModal} style={{ marginBottom: '25px' }} className="btn btn-dark">
|
|
|
|
<FontAwesomeIcon icon={faPen} style={{cursor: 'pointer', width: '1rem'}} />
|
|
|
|
Éditez le profil
|
|
|
|
</button>
|
2020-04-08 00:47:24 +02:00
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-11 23:29:22 +02:00
|
|
|
|
|
|
|
{(props.favoritesArray.length > 0) &&
|
|
|
|
<div className="row justify-content-center">
|
|
|
|
<div className="col-24 text-center">
|
2020-04-22 22:01:18 +02:00
|
|
|
<h2>Dernières fonctions ajoutées aux <span className="important">favoris</span> :</h2>
|
2020-04-11 23:29:22 +02:00
|
|
|
</div>
|
|
|
|
<div className="col-24">
|
|
|
|
<div className="row justify-content-center">
|
|
|
|
{props.favoritesArray.map((favorite) => {
|
|
|
|
return (
|
|
|
|
<FunctionCard key={favorite.id} { ...favorite } />
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</div>
|
|
|
|
</div>
|
2020-04-06 23:06:21 +02:00
|
|
|
</div>
|
2020-04-11 23:29:22 +02:00
|
|
|
}
|
2020-04-08 00:47:24 +02:00
|
|
|
|
2020-04-11 23:29:22 +02:00
|
|
|
{(props.commentsArray.length > 0) &&
|
2020-04-06 23:06:21 +02:00
|
|
|
<div className="row justify-content-center">
|
2020-04-11 23:29:22 +02:00
|
|
|
<div className="col-24 text-center">
|
2020-04-22 22:01:18 +02:00
|
|
|
<h2>Derniers <span className="important">commentaires</span> :</h2>
|
2020-04-11 23:29:22 +02:00
|
|
|
</div>
|
|
|
|
<div className="col-24 text-center">
|
|
|
|
{props.commentsArray.map((comment) => (
|
|
|
|
<div key={comment.id} className="row Profile__row Profile__comment">
|
|
|
|
<div className="col-20">
|
|
|
|
<p>
|
|
|
|
Posté sur la fonction
|
|
|
|
<Link href={(comment.function.type === 'form' || comment.function.type === 'article') ? "/functions/[slug]" : `/functions/${comment.function.slug}`} as={`/functions/${comment.function.slug}`}>
|
|
|
|
<a>{comment.function.title}</a>
|
|
|
|
</Link>
|
|
|
|
le {date.format(new Date(comment.createdAt), 'DD/MM/YYYY à HH:mm', true)}
|
|
|
|
</p>
|
|
|
|
<p>"{comment.message}"</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
))}
|
|
|
|
</div>
|
2020-04-06 23:06:21 +02:00
|
|
|
</div>
|
2020-04-11 23:29:22 +02:00
|
|
|
}
|
2020-04-22 22:01:18 +02:00
|
|
|
|
|
|
|
{(props.quotesArray.length > 0) &&
|
|
|
|
<div className="row justify-content-center">
|
|
|
|
<div className="col-24 text-center">
|
|
|
|
<h2>Dernières <span className="important">citations</span> proposées (et validées) :</h2>
|
|
|
|
<p>Citations pour la fonction <Link href="/functions/randomQuote"><a>Générateur de citations</a></Link>.</p>
|
|
|
|
</div>
|
|
|
|
<div className="col-24 table-column">
|
|
|
|
<table style={{ marginBottom: '50px' }}>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th className="table-row" scope="col">Citation/Proverbe</th>
|
|
|
|
<th className="table-row" scope="col">Auteur</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{props.quotesArray.map((currentQuote, index) => {
|
|
|
|
return (
|
|
|
|
<tr key={index}>
|
|
|
|
<td className="table-row text-center">{currentQuote.quote}</td>
|
|
|
|
<td className="table-row text-center">{currentQuote.author}</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
}
|
2020-04-06 23:06:21 +02:00
|
|
|
</div>
|
2020-04-11 23:29:22 +02:00
|
|
|
}
|
2020-04-06 23:06:21 +02:00
|
|
|
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getServerSideProps(context) {
|
|
|
|
const { name } = context.params;
|
|
|
|
return api.get(`/users/${name}`)
|
|
|
|
.then((response) => ({ props: response.data }))
|
|
|
|
.catch(() => redirect(context, '/404'));
|
|
|
|
}
|
|
|
|
|
|
|
|
export default Profile;
|