import { useState } from 'react' import Cookies from 'universal-cookie' import date from 'date-and-time' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { faPen, faTrash, faTimes } from '@fortawesome/free-solid-svg-icons' import { PhotoshopPicker } from 'react-color' import HeadTag from '../../components/HeadTag' 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' const defaultCategoryState = { name: '', color: '#ffffff' } const AddEditCategory = props => { const [inputState, setInputState] = useState(props.defaultInputState) const [message, setMessage] = useState('') const [isLoading, setIsLoading] = useState(false) 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 apiCallCategory = () => { if (props.isEditing) { return api.put( `/admin/categories/${inputState.id}`, { name: inputState.name, color: inputState.color }, { headers: { Authorization: props.user.token } } ) } return api.post('/admin/categories', inputState, { headers: { Authorization: props.user.token } }) } const handleSubmit = event => { event.preventDefault() setIsLoading(true) apiCallCategory() .then(() => { setIsLoading(false) window.location.reload(true) }) .catch(error => { setMessage( `
Erreur: ${error.response.data.message}
` ) setIsLoading(false) }) } return (id | name | color | createdAt | updatedAt | Modifier | Supprimer |
---|---|---|---|---|---|---|
{category.id} | {category.name} | {category.color} | {date.format( new Date(category.createdAt), 'DD/MM/YYYY à HH:mm', false )} | {date.format( new Date(category.updatedAt), 'DD/MM/YYYY à HH:mm', false )} |
handleEditCategory({
name: category.name,
color: category.color,
id: category.id
})}
>
|
handleRemoveCategory(category.id)}
>
|