2020-08-03 12:04:07 +02:00
|
|
|
import { useState, useEffect } from 'react'
|
|
|
|
import Loader from '../Loader'
|
|
|
|
import htmlParser from 'html-react-parser'
|
|
|
|
import dynamic from 'next/dynamic'
|
|
|
|
import api from '../../utils/api'
|
|
|
|
import fr from 'date-fns/locale/fr'
|
|
|
|
import { registerLocale } from 'react-datepicker'
|
|
|
|
import date from 'date-and-time'
|
2020-04-20 17:53:44 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
registerLocale('fr', fr)
|
2020-03-24 16:11:55 +01:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const FunctionForm = props => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const [inputState, setInputState] = useState({})
|
|
|
|
const [message, setMessage] = useState('')
|
|
|
|
const [isLoading, setIsLoading] = useState(false)
|
2020-03-24 16:11:55 +01:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
// inputState par défaut
|
|
|
|
useEffect(() => {
|
|
|
|
const inputStateNew = { ...inputState }
|
2020-08-03 14:14:45 +02:00
|
|
|
props.inputsArray.forEach(input => {
|
2020-08-03 12:04:07 +02:00
|
|
|
if (input.type === 'select' && input.options.length > 0) {
|
|
|
|
inputStateNew[input.name] = input.options[0].value
|
|
|
|
}
|
|
|
|
})
|
|
|
|
setInputState(inputStateNew)
|
|
|
|
}, [])
|
2020-03-24 16:11:55 +01:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleSubmit = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
setIsLoading(true)
|
|
|
|
event.preventDefault()
|
2020-08-03 14:14:45 +02:00
|
|
|
api
|
|
|
|
.post(`/functions/${props.slug}`, inputState)
|
|
|
|
.then(response => {
|
2020-08-03 12:04:07 +02:00
|
|
|
setMessage(response.data.resultHTML)
|
|
|
|
setIsLoading(false)
|
|
|
|
})
|
2020-08-03 14:14:45 +02:00
|
|
|
.catch(error => {
|
2020-08-03 12:04:07 +02:00
|
|
|
setMessage(error.response.data.message)
|
|
|
|
setIsLoading(false)
|
|
|
|
})
|
|
|
|
}
|
2020-04-23 01:08:54 +02:00
|
|
|
|
2020-08-03 14:14:45 +02:00
|
|
|
const handleChange = event => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const inputStateNew = { ...inputState }
|
|
|
|
inputStateNew[event.target.name] = event.target.value
|
|
|
|
setInputState(inputStateNew)
|
|
|
|
}
|
2020-03-24 16:11:55 +01:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
if (props.inputsArray.length <= 0) {
|
2020-03-24 16:11:55 +01:00
|
|
|
return (
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='FunctionComponent__slide text-center'>
|
|
|
|
<p>La fonction n'est pas encore disponible.</p>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
{props.inputsArray.map((input, index) => {
|
|
|
|
switch (input.type) {
|
|
|
|
case 'text':
|
|
|
|
return (
|
|
|
|
<div key={index} className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor={input.name}>
|
|
|
|
{input.label}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
onChange={handleChange}
|
|
|
|
type='text'
|
|
|
|
name={input.name}
|
|
|
|
id={input.name}
|
|
|
|
placeholder={input.placeholder}
|
|
|
|
className='form-control'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
case 'integer':
|
|
|
|
case 'float':
|
|
|
|
return (
|
|
|
|
<div key={index} className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor={input.name}>
|
|
|
|
{input.label}
|
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
onChange={handleChange}
|
|
|
|
type='number'
|
|
|
|
step={input.type === 'integer' ? '1' : '0.01'}
|
|
|
|
name={input.name}
|
|
|
|
id={input.name}
|
|
|
|
placeholder={input.placeholder}
|
|
|
|
className='form-control'
|
|
|
|
/>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
)
|
|
|
|
case 'calendar':
|
|
|
|
// Permet au datepicker de prendre la hauteur
|
|
|
|
if (typeof window !== 'undefined') {
|
|
|
|
const newScript = document.createElement('script')
|
|
|
|
newScript.src = '/js/extraHeightCSS.js'
|
|
|
|
document.body.appendChild(newScript)
|
|
|
|
}
|
|
|
|
// eslint-disable-next-line
|
2020-08-03 14:14:45 +02:00
|
|
|
const DatePicker = dynamic(() => import('react-datepicker'), {
|
|
|
|
ssr: false
|
|
|
|
})
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
|
|
|
<div key={index} className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor={input.name}>
|
|
|
|
{input.label}
|
|
|
|
</label>
|
2020-08-03 12:04:07 +02:00
|
|
|
<br />
|
|
|
|
<DatePicker
|
|
|
|
selected={(() => {
|
|
|
|
try {
|
|
|
|
if (inputState[input.name] != null) {
|
|
|
|
const dateArray = inputState[input.name].split('/')
|
|
|
|
const year = dateArray[2]
|
|
|
|
const month = dateArray[1]
|
|
|
|
const day = dateArray[0]
|
2020-08-03 14:14:45 +02:00
|
|
|
return new Date(
|
|
|
|
year,
|
|
|
|
parseInt(month) - 1,
|
|
|
|
parseInt(day) + 1
|
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
|
|
|
throw new Error('Not a valid date')
|
|
|
|
} catch {
|
|
|
|
return new Date()
|
|
|
|
}
|
|
|
|
})()}
|
|
|
|
locale='fr'
|
|
|
|
dateFormat='dd/MM/yyyy'
|
|
|
|
fixedHeight
|
|
|
|
placeholderText={input.placeholder}
|
2020-08-03 14:14:45 +02:00
|
|
|
onChange={dateObject => {
|
2020-08-03 12:04:07 +02:00
|
|
|
try {
|
2020-08-03 14:14:45 +02:00
|
|
|
const formattedDate = date.format(
|
|
|
|
dateObject,
|
|
|
|
'DD/MM/YYYY',
|
2020-08-04 11:42:21 +02:00
|
|
|
false
|
2020-08-03 14:14:45 +02:00
|
|
|
)
|
2020-08-03 12:04:07 +02:00
|
|
|
handleChange({
|
|
|
|
target: {
|
|
|
|
name: input.name,
|
|
|
|
value: formattedDate
|
|
|
|
}
|
|
|
|
})
|
|
|
|
} catch {}
|
|
|
|
}}
|
|
|
|
/>
|
2020-03-24 16:11:55 +01:00
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
)
|
|
|
|
case 'select':
|
|
|
|
return (
|
|
|
|
<div key={index} className='form-group'>
|
2020-08-03 14:14:45 +02:00
|
|
|
<label className='form-label' htmlFor={input.name}>
|
|
|
|
{input.label}
|
|
|
|
</label>
|
|
|
|
<select
|
|
|
|
onChange={handleChange}
|
|
|
|
name={input.name}
|
|
|
|
id={input.name}
|
|
|
|
value={inputState[input.name] || input.options[0]}
|
|
|
|
className='form-control'
|
|
|
|
>
|
2020-08-03 12:04:07 +02:00
|
|
|
{input.options.map((option, optionIndex) => {
|
|
|
|
return (
|
2020-08-03 14:14:45 +02:00
|
|
|
<option key={optionIndex} value={option.value}>
|
|
|
|
{option.name}
|
|
|
|
</option>
|
2020-08-03 12:04:07 +02:00
|
|
|
)
|
|
|
|
})}
|
|
|
|
</select>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
default:
|
2020-08-03 14:14:45 +02:00
|
|
|
return <p>Erreur, l'input n'est pas valide...</p>
|
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-03-24 16:11:55 +01:00
|
|
|
}
|
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default FunctionForm
|