import Link from 'next/link'; import { Fragment, useState, useEffect } from 'react'; import Cookies from "universal-cookie"; import HeadTag from '../../components/HeadTag'; import FunctionsList from '../../components/FunctionsList/FunctionsList'; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; import { faTimes } from '@fortawesome/free-solid-svg-icons'; import Modal from '../../components/Modal'; import redirect from '../../utils/redirect'; import htmlParser from 'html-react-parser'; import Loader from '../../components/Loader'; import useAPI from '../../hooks/useAPI'; import api from '../../utils/api'; import '../../public/css/pages/admin.css'; const Admin = (props) => { const [, categories] = useAPI('/categories'); const [isOpen, setIsOpen] = useState(false); const [inputState, setInputState] = useState({ type: 'form' }); const [message, setMessage] = useState(""); const [isLoading, setIsLoading] = useState(false); useEffect(() => { if (categories.length > 0) { handleChange({ target: { name: "categorieId", value: categories[0].id } }); } }, [categories]); 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(); setIsLoading(true); const formData = new FormData(); formData.append('type', inputState.type); formData.append('categorieId', inputState.categorieId); formData.append('title', inputState.title); formData.append('slug', inputState.slug); formData.append('description', inputState.description); formData.append('image', inputState.image); api.post('/admin/functions', formData, { headers: { 'Authorization': props.user.token } }) .then((response) => { setMessage(`

Succès: ${response.data.message}

`); setIsLoading(false); }) .catch((error) => { setMessage(`

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

`); setIsLoading(false); }); } if (!props.user.isAdmin && typeof window != 'undefined') { return redirect({}, '/404'); } return ( {/* Création d'une fonction */} {(isOpen) ?

Crée une nouvelle fonction


{ (isLoading) ? : htmlParser(message) }
:

Administration

}
); } export async function getServerSideProps({ req }) { const cookies = new Cookies(req.headers.cookie); return { props: { user: { ...cookies.get('user') } } }; } export default Admin;