2020-08-03 12:04:07 +02:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import htmlParser from 'html-react-parser'
|
2020-08-03 12:11:08 +02:00
|
|
|
import Loader from '../Loader'
|
2020-08-03 12:04:07 +02:00
|
|
|
import useAPI from '../../hooks/useAPI'
|
|
|
|
import api from '../../utils/api'
|
2020-04-15 22:50:40 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const AddEditFunction = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const [, categories] = useAPI('/categories')
|
|
|
|
const [inputState, setInputState] = useState(props.defaultInputState)
|
|
|
|
const [message, setMessage] = useState('')
|
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (categories.length > 0 && !props.isEditing) {
|
|
|
|
handleChange({
|
|
|
|
target: {
|
|
|
|
name: 'categorieId',
|
|
|
|
value: categories[0].id
|
2020-04-15 22:50:40 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
})
|
2020-04-15 22:50:40 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}, [categories])
|
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const apiCallFunction = formData => {
|
|
|
|
if (props.isEditing) {
|
|
|
|
return api.put(`/admin/functions/${inputState.id}`, formData, {
|
|
|
|
headers: { Authorization: props.user.token }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return api.post('/admin/functions', formData, {
|
|
|
|
headers: { Authorization: props.user.token }
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const handleChange = (event, isTypeCheck = false) => {
|
|
|
|
const inputStateNew = { ...inputState }
|
2020-08-03 14:14:45 +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)
|
|
|
|
}
|
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleSubmit = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
event.preventDefault()
|
|
|
|
setIsLoading(true)
|
|
|
|
const formData = new window.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)
|
|
|
|
|
|
|
|
if (props.isEditing) {
|
|
|
|
formData.append('isOnline', inputState.isOnline)
|
2020-04-15 22:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
apiCallFunction(formData)
|
|
|
|
.then(() => {
|
|
|
|
setIsLoading(false)
|
|
|
|
window.location.reload(true)
|
|
|
|
})
|
2020-08-03 14:14:45 +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 (
|
|
|
|
<>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='title'>
|
|
|
|
Titre :
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
value={inputState.title}
|
|
|
|
onChange={handleChange}
|
|
|
|
type='text'
|
|
|
|
name='title'
|
|
|
|
id='title'
|
|
|
|
className='form-control'
|
|
|
|
placeholder='(e.g : Nombre aléatoire)'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='slug'>
|
|
|
|
Slug :
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
value={inputState.slug}
|
|
|
|
onChange={handleChange}
|
|
|
|
type='text'
|
|
|
|
name='slug'
|
|
|
|
id='slug'
|
|
|
|
className='form-control'
|
|
|
|
placeholder='(e.g : randomNumber)'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='description'>
|
|
|
|
Description :
|
|
|
|
</label>
|
|
|
|
<textarea
|
|
|
|
style={{ height: 'auto' }}
|
|
|
|
value={inputState.description}
|
|
|
|
onChange={handleChange}
|
|
|
|
name='description'
|
|
|
|
id='description'
|
|
|
|
className='form-control'
|
|
|
|
rows='5'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='type'>
|
|
|
|
Type :
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
onChange={handleChange}
|
|
|
|
name='type'
|
|
|
|
id='type'
|
|
|
|
className='form-control'
|
|
|
|
{...(props.isEditing && { value: inputState.type })}
|
|
|
|
>
|
2020-08-03 12:04:07 +02:00
|
|
|
<option value='form'>Formulaire</option>
|
|
|
|
<option value='article'>Article</option>
|
|
|
|
<option value='page'>Page</option>
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='categorieId'>
|
|
|
|
Catégorie :
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
onChange={handleChange}
|
|
|
|
name='categorieId'
|
|
|
|
id='categorieId'
|
|
|
|
className='form-control'
|
|
|
|
{...(props.isEditing && { value: inputState.categorieId })}
|
|
|
|
>
|
|
|
|
{categories.map(category => (
|
|
|
|
<option
|
|
|
|
key={category.id}
|
|
|
|
value={category.id}
|
|
|
|
className='Admin__Modal-select-option'
|
|
|
|
style={{ backgroundColor: category.color }}
|
|
|
|
>
|
|
|
|
{category.name}
|
|
|
|
</option>
|
2020-08-03 12:04:07 +02:00
|
|
|
))}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor='image'>
|
|
|
|
Image <em>(150x150 recommandé)</em> :
|
|
|
|
</label>
|
2020-08-03 12:04:07 +02:00
|
|
|
<br />
|
2020-08-03 14:14:45 +02:00
|
|
|
<input
|
|
|
|
onChange={handleChange}
|
|
|
|
accept='image/jpeg,image/jpg,image/png'
|
|
|
|
type='file'
|
|
|
|
name='image'
|
|
|
|
id='image'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
{props.isEditing && (
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group custom-control custom-switch'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<input
|
|
|
|
onChange={event => handleChange(event, true)}
|
|
|
|
type='checkbox'
|
|
|
|
name='isOnline'
|
|
|
|
checked={inputState.isOnline}
|
|
|
|
className='custom-control-input'
|
|
|
|
id='isOnline'
|
|
|
|
/>
|
|
|
|
<label className='custom-control-label' htmlFor='isOnline'>
|
|
|
|
isOnline
|
|
|
|
</label>
|
|
|
|
</div>
|
|
|
|
)}
|
2020-08-03 12:04:07 +02:00
|
|
|
|
|
|
|
<div className='form-group text-center'>
|
2020-08-03 14:14:45 +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:14:45 +02:00
|
|
|
{isLoading ? <Loader /> : htmlParser(message)}
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
2020-04-15 22:50:40 +02:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default AddEditFunction
|