import Link from 'next/link' import { useContext, useState, useEffect } from 'react' import { UserContext } from '../../contexts/UserContext' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faPen, faTimes } from '@fortawesome/free-solid-svg-icons' import date from 'date-and-time' import HeadTag from '../../components/HeadTag' import FunctionCard from '../../components/FunctionCard/FunctionCard' import Modal from '../../components/Modal' import redirect from '../../utils/redirect' import htmlParser from 'html-react-parser' import Loader from '../../components/Loader' import ReactMarkdown from 'react-markdown' import CodeBlock from '../../components/CodeBlock' import api from '../../utils/api' import { API_URL } from '../../utils/config/config' import '../../public/css/pages/profile.css' const Profile = (props) => { const { isAuth, user, logoutUser } = useContext(UserContext) const [isOpen, setIsOpen] = useState(false) 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]) const toggleModal = () => setIsOpen(!isOpen) const handleChange = (event, isTypeCheck = false) => { const inputStateNew = { ...inputState } inputStateNew[event.target.name] = (event.target.files != null) ? 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 != null) { setIsLoading(true) const formData = new window.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) api.put('/users/', formData, { headers: { Authorization: token } }) .then(() => { setIsLoading(false) logoutUser() redirect({}, '/users/login?isSuccessEdit=true') }) .catch((error) => { setMessage(`

Erreur: ${error.response.data.message}

`) setIsLoading(false) }) } } return ( <> {/* Édition du profil */} {(isOpen) ? (

Éditer le profil

(Vous devrez vous reconnecter après la sauvegarde)
Si vous changez votre adresse email, vous devrez la confirmer comme à l'inscription (vérifier vos emails).

handleChange(event, true)} type='checkbox' name='isPublicEmail' checked={inputState.isPublicEmail} className='custom-control-input' id='isPublicEmail' />