2020-08-03 12:04:07 +02:00
|
|
|
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'
|
|
|
|
import '../../public/css/pages/admin.css'
|
|
|
|
|
|
|
|
const defaultCategoryState = { name: '', color: '#ffffff' }
|
2020-04-13 18:26:45 +02:00
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const AddEditCategory = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const [inputState, setInputState] = useState(props.defaultInputState)
|
|
|
|
const [message, setMessage] = useState('')
|
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
|
|
|
|
const handleChange = (event, isTypeCheck = false) => {
|
|
|
|
const inputStateNew = { ...inputState }
|
2020-08-03 14:37:05 +02:00
|
|
|
inputStateNew[event.target.name] =
|
|
|
|
event.target.files != null
|
|
|
|
? event.target.files[0]
|
|
|
|
: isTypeCheck
|
|
|
|
? event.target.checked
|
|
|
|
: event.target.value
|
2020-08-03 12:04:07 +02:00
|
|
|
setInputState(inputStateNew)
|
|
|
|
}
|
|
|
|
|
|
|
|
const apiCallCategory = () => {
|
2020-08-03 14:37:05 +02:00
|
|
|
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 }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const handleSubmit = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
event.preventDefault()
|
|
|
|
setIsLoading(true)
|
|
|
|
apiCallCategory()
|
|
|
|
.then(() => {
|
|
|
|
setIsLoading(false)
|
|
|
|
window.location.reload(true)
|
|
|
|
})
|
2020-08-03 14:37:05 +02:00
|
|
|
.catch(error => {
|
|
|
|
setMessage(
|
|
|
|
`<p class="form-error"><b>Erreur:</b> ${error.response.data.message}</p>`
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
setIsLoading(false)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className='Admin__Modal__container container-fluid'>
|
|
|
|
<div className='Admin__Modal__row row'>
|
|
|
|
<div className='col-24'>
|
|
|
|
<div className='Admin__Modal-top-container row'>
|
|
|
|
<div className='col-24'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<span
|
|
|
|
onClick={props.handleToggleModal}
|
|
|
|
style={{ cursor: 'pointer', position: 'absolute', left: 0 }}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faTimes}
|
|
|
|
style={{ width: '1.5rem', color: 'red' }}
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</span>
|
2020-08-03 14:37:05 +02:00
|
|
|
<h2 className='text-center'>
|
|
|
|
{props.isEditing
|
|
|
|
? 'Modifier la catégorie'
|
|
|
|
: 'Crée une nouvelle catégorie'}
|
|
|
|
</h2>
|
2020-04-13 18:26:45 +02:00
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-04-13 18:26:45 +02:00
|
|
|
</div>
|
2020-04-12 12:40:56 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='col-24'>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<label className='form-label' htmlFor='name'>
|
|
|
|
Nom :
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
value={inputState.name}
|
|
|
|
onChange={handleChange}
|
|
|
|
type='text'
|
|
|
|
name='name'
|
|
|
|
id='name'
|
|
|
|
className='form-control'
|
|
|
|
placeholder='(e.g : ✨ Utilitaires)'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-04-12 12:40:56 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<label className='form-label' htmlFor='title'>
|
|
|
|
Couleur :
|
|
|
|
</label>
|
|
|
|
<PhotoshopPicker
|
|
|
|
color={inputState.color}
|
|
|
|
onChange={color =>
|
|
|
|
handleChange({ target: { name: 'color', value: color.hex } })}
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
2020-04-12 12:40:56 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
<button type='submit' className='btn btn-dark'>
|
|
|
|
Envoyer
|
|
|
|
</button>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className='form-result text-center'>
|
2020-08-03 14:37:05 +02:00
|
|
|
{isLoading ? <Loader /> : htmlParser(message)}
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const manageCategories = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const [, categories] = useAPI('/categories')
|
|
|
|
const [isOpen, setIsOpen] = useState(false)
|
|
|
|
const [isEditing, setIsEditing] = useState(false)
|
2020-08-03 14:37:05 +02:00
|
|
|
const [defaultInputState, setDefaultInputState] = useState(
|
|
|
|
defaultCategoryState
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
|
|
|
|
const toggleModal = () => setIsOpen(!isOpen)
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const handleRemoveCategory = async categoryId => {
|
2020-08-03 12:04:07 +02:00
|
|
|
try {
|
2020-08-03 14:37:05 +02:00
|
|
|
await api.delete(`/admin/categories/${categoryId}`, {
|
|
|
|
headers: { Authorization: props.user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
window.location.reload(true)
|
|
|
|
} catch {}
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:37:05 +02:00
|
|
|
const handleEditCategory = categoryInfo => {
|
2020-08-03 12:04:07 +02:00
|
|
|
setDefaultInputState(categoryInfo)
|
|
|
|
setIsEditing(true)
|
|
|
|
toggleModal()
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-08-03 14:37:05 +02:00
|
|
|
<HeadTag
|
|
|
|
title='Admin - FunctionProject'
|
|
|
|
description="Page d'administration de FunctionProject. Gérer les catégories."
|
|
|
|
/>
|
|
|
|
|
2020-10-30 16:58:27 +01:00
|
|
|
{isOpen
|
|
|
|
? (
|
|
|
|
<Modal>
|
|
|
|
<AddEditCategory
|
|
|
|
handleToggleModal={toggleModal}
|
|
|
|
defaultInputState={defaultInputState}
|
|
|
|
{...props}
|
|
|
|
isEditing={isEditing}
|
|
|
|
/>
|
|
|
|
</Modal>
|
|
|
|
)
|
|
|
|
: (
|
|
|
|
<div className='container-fluid text-center'>
|
|
|
|
<div className='row justify-content-center'>
|
|
|
|
<div className='col-24'>
|
|
|
|
<h1>Gérer les catégories</h1>
|
|
|
|
<button
|
|
|
|
onClick={() => {
|
|
|
|
setDefaultInputState(defaultCategoryState)
|
|
|
|
toggleModal()
|
|
|
|
setIsEditing(false)
|
|
|
|
}}
|
|
|
|
style={{ margin: '0 0 40px 0' }}
|
|
|
|
className='btn btn-dark'
|
|
|
|
>
|
|
|
|
Ajouter une catégorie
|
|
|
|
</button>
|
|
|
|
</div>
|
2020-08-03 14:37:05 +02:00
|
|
|
</div>
|
2020-10-30 16:58:27 +01:00
|
|
|
<div className='row justify-content-center'>
|
|
|
|
<div className='container-fluid'>
|
|
|
|
<div className='col-24 table-column'>
|
|
|
|
<table className='table'>
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
id
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
name
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
color
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
createdAt
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
updatedAt
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
Modifier
|
|
|
|
</th>
|
|
|
|
<th className='table-row' scope='col'>
|
|
|
|
Supprimer
|
|
|
|
</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{categories.map(category => {
|
|
|
|
return (
|
|
|
|
<tr
|
|
|
|
key={category.id}
|
|
|
|
style={{ backgroundColor: category.color }}
|
2020-08-03 14:37:05 +02:00
|
|
|
>
|
2020-10-30 16:58:27 +01:00
|
|
|
<td className='table-row'>{category.id}</td>
|
|
|
|
<td className='table-row'>{category.name}</td>
|
|
|
|
<td className='table-row'>{category.color}</td>
|
|
|
|
<td className='table-row'>
|
|
|
|
{date.format(
|
|
|
|
new Date(category.createdAt),
|
|
|
|
'DD/MM/YYYY à HH:mm',
|
|
|
|
false
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
<td className='table-row'>
|
|
|
|
{date.format(
|
|
|
|
new Date(category.updatedAt),
|
|
|
|
'DD/MM/YYYY à HH:mm',
|
|
|
|
false
|
|
|
|
)}
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
onClick={() =>
|
|
|
|
handleEditCategory({
|
|
|
|
name: category.name,
|
|
|
|
color: category.color,
|
|
|
|
id: category.id
|
|
|
|
})}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faPen}
|
|
|
|
style={{ width: '1.5rem' }}
|
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
<td
|
|
|
|
style={{ cursor: 'pointer' }}
|
|
|
|
onClick={() => handleRemoveCategory(category.id)}
|
|
|
|
>
|
|
|
|
<FontAwesomeIcon
|
|
|
|
icon={faTrash}
|
|
|
|
style={{ width: '1.5rem' }}
|
|
|
|
/>
|
|
|
|
</td>
|
|
|
|
</tr>
|
|
|
|
)
|
|
|
|
})}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
2020-08-03 14:37:05 +02:00
|
|
|
</div>
|
2020-10-30 16:58:27 +01:00
|
|
|
)}
|
2020-08-03 12:04:07 +02:00
|
|
|
</>
|
|
|
|
)
|
2020-04-12 12:40:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export async function getServerSideProps (context) {
|
|
|
|
const cookies = new Cookies(context.req.headers.cookie)
|
|
|
|
const user = { ...cookies.get('user') }
|
|
|
|
if (!user.isAdmin) {
|
|
|
|
return redirect(context, '/404')
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
props: { user }
|
|
|
|
}
|
2020-04-12 12:40:56 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default manageCategories
|