This repository has been archived on 2024-10-29. You can view files and clone it, but cannot push or open issues or pull requests.
FunctionProject/website/components/FunctionAdmin/EditFormFunction.jsx

256 lines
8.4 KiB
React
Raw Normal View History

2020-08-03 12:04:07 +02:00
import { useState } from 'react'
import api from '../../utils/api'
2020-08-03 14:14:45 +02:00
const EditFormFunction = props => {
const [inputsArray, setInputsArray] = useState(
props.functionInfo.utilizationForm || []
)
2020-08-03 12:04:07 +02:00
const addInput = () => {
const newInputsArray = [...inputsArray]
newInputsArray.push({ name: '', label: '', placeholder: '', type: 'text' })
setInputsArray(newInputsArray)
}
2020-08-03 14:14:45 +02:00
const addOption = event => {
2020-08-03 12:04:07 +02:00
const newInputsArray = [...inputsArray]
const index = event.target.id.split('-')[1]
const inputObject = newInputsArray[index]
inputObject.options.push({ name: '', value: '' })
setInputsArray(newInputsArray)
}
const handleChangeOption = (inputIndex, optionIndex, event) => {
const newInputsArray = [...inputsArray]
const inputObject = newInputsArray[inputIndex]
const optionObject = inputObject.options[optionIndex]
optionObject[event.target.name] = event.target.value
setInputsArray(newInputsArray)
}
2020-08-03 14:14:45 +02:00
const handleChangeInput = event => {
2020-08-03 12:04:07 +02:00
const newInputsArray = [...inputsArray]
const index = event.target.id.split('-')[1]
const inputObject = newInputsArray[index]
inputObject[event.target.name] = event.target.value
if (event.target.value === 'select') {
inputObject.options = []
2020-04-21 18:03:16 +02:00
}
2020-08-03 12:04:07 +02:00
setInputsArray(newInputsArray)
}
2020-08-03 14:14:45 +02:00
const handleSubmit = async event => {
2020-08-03 12:04:07 +02:00
event.preventDefault()
let Notyf
if (typeof window !== 'undefined') {
Notyf = require('notyf')
}
2020-08-03 12:04:07 +02:00
const notyf = new Notyf.Notyf({
duration: 5000
})
try {
2020-08-03 14:14:45 +02:00
await api.put(
`/admin/functions/form/${props.functionInfo.id}`,
{ form: inputsArray },
{ headers: { Authorization: props.user.token } }
)
2020-08-03 12:04:07 +02:00
notyf.success('Sauvegardé!')
} catch (error) {
notyf.error('Erreur!')
}
2020-08-03 12:04:07 +02:00
}
2020-08-03 14:14:45 +02:00
const handleRemoveInput = event => {
2020-08-03 12:04:07 +02:00
const newInputsArray = [...inputsArray]
const index = event.target.id.split('-')[1]
newInputsArray.splice(index, 1)
setInputsArray(newInputsArray)
}
const handleRemoveOption = (inputIndex, optionIndex) => {
const newInputsArray = [...inputsArray]
const inputObject = newInputsArray[inputIndex]
const optionsArray = inputObject.options
optionsArray.splice(optionIndex, 1)
setInputsArray(newInputsArray)
}
return (
<div className='container-fluid'>
<form onSubmit={handleSubmit}>
2020-08-03 14:14:45 +02:00
{inputsArray.length > 0 && (
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'>
Sauvegarder
</button>
</div>
)}
2020-08-03 12:04:07 +02:00
{inputsArray.map((input, index) => {
return (
<div key={index} className='form-group Admin__Input-group'>
<div className='text-center'>
2020-08-03 14:14:45 +02:00
<button
type='button'
onClick={handleRemoveInput}
id={`remove-${index}`}
className='btn btn-dark'
>
Supprimer l'input
</button>
2020-08-03 12:04:07 +02:00
</div>
2020-08-03 14:14:45 +02:00
<label className='form-label' htmlFor={`name-${index}`}>
Nom de l'input :
</label>
<input
value={input.name}
onChange={handleChangeInput}
type='text'
name='name'
id={`name-${index}`}
className='form-control'
placeholder='(e.g : cityName)'
/>
2020-08-03 12:04:07 +02:00
<br />
2020-08-03 14:14:45 +02:00
<label className='form-label' htmlFor={`label-${index}`}>
Label :
</label>
<input
value={input.label}
onChange={handleChangeInput}
type='text'
name='label'
id={`label-${index}`}
className='form-control'
placeholder="(e.g : Entrez le nom d'une ville :)"
/>
2020-08-03 12:04:07 +02:00
<br />
2020-08-03 14:14:45 +02:00
{input.type !== 'select' && (
2020-08-03 12:04:07 +02:00
<>
2020-08-03 14:14:45 +02:00
<label
className='form-label'
htmlFor={`placeholder-${index}`}
>
Placeholder :
</label>
<input
value={input.placeholder}
onChange={handleChangeInput}
type='text'
name='placeholder'
id={`placeholder-${index}`}
className='form-control'
placeholder='(e.g : Paris, FR)'
/>
2020-08-03 12:04:07 +02:00
<br />
2020-08-03 14:14:45 +02:00
</>
)}
2020-08-03 12:04:07 +02:00
2020-08-03 14:14:45 +02:00
<label className='form-label' htmlFor={`type-${index}`}>
Type :
</label>
<select
value={input.type}
onChange={handleChangeInput}
name='type'
id={`type-${index}`}
className='form-control'
>
2020-08-03 12:04:07 +02:00
<option value='text'>text</option>
<option value='integer'>Number integer</option>
<option value='float'>Number float</option>
<option value='calendar'>calendar</option>
<option value='select'>select</option>
</select>
2020-08-03 14:14:45 +02:00
{input.type === 'select' && (
2020-08-03 12:04:07 +02:00
<div style={{ marginTop: '50px' }}>
<label className='form-label'>Options :</label>
{input.options.map((option, optionIndex) => {
return (
2020-08-03 14:14:45 +02:00
<div
key={optionIndex}
style={{ margin: '0 0 30px 0' }}
className='form-group Admin__Input-group'
>
2020-08-03 12:04:07 +02:00
<div className='text-center'>
2020-08-03 14:14:45 +02:00
<button
type='button'
onClick={() =>
handleRemoveOption(index, optionIndex)}
className='btn btn-dark'
>
Supprimer l'option
</button>
</div>
2020-08-03 14:14:45 +02:00
<label
className='form-label'
htmlFor={`optionName-${optionIndex}-${index}`}
>
Nom de l'option
</label>
<input
onChange={event =>
handleChangeOption(index, optionIndex, event)}
value={option.name}
id={`optionName-${optionIndex}-${index}`}
name='name'
type='text'
className='form-control'
placeholder="Nom de l'option"
/>
2020-08-03 12:04:07 +02:00
<br />
2020-08-03 14:14:45 +02:00
<label
className='form-label'
htmlFor={`optionValue-${optionIndex}-${index}`}
>
Valeur de l'option
</label>
<input
onChange={event =>
handleChangeOption(index, optionIndex, event)}
value={option.value}
id={`optionValue-${optionIndex}-${index}`}
name='value'
type='text'
className='form-control'
placeholder="Valeur de l'option"
/>
2020-08-03 12:04:07 +02:00
</div>
)
})}
2020-08-03 12:04:07 +02:00
<div className='form-group text-center'>
2020-08-03 14:14:45 +02:00
<button
id={`optionAdd-${index}`}
onClick={addOption}
type='button'
className='btn btn-dark'
>
Ajouter une option
</button>
2020-08-03 12:04:07 +02:00
</div>
2020-08-03 14:14:45 +02:00
</div>
)}
</div>
2020-08-03 12:04:07 +02:00
)
})}
</form>
<div style={{ marginBottom: '30px' }} className='form-group text-center'>
2020-08-03 14:14:45 +02:00
<button type='button' onClick={addInput} className='btn btn-dark'>
Ajouter un input
</button>
2020-08-03 12:04:07 +02:00
</div>
</div>
)
}
2020-08-03 12:04:07 +02:00
export default EditFormFunction