2020-08-03 12:04:07 +02:00
import { useState } from 'react'
import api from '../../utils/api'
import 'notyf/notyf.min.css'
2020-04-16 19:44:19 +02:00
const EditFormFunction = ( props ) => {
2020-08-03 12:04:07 +02:00
const [ inputsArray , setInputsArray ] = useState ( props . functionInfo . utilizationForm || [ ] )
const addInput = ( ) => {
const newInputsArray = [ ... inputsArray ]
newInputsArray . push ( { name : '' , label : '' , placeholder : '' , type : 'text' } )
setInputsArray ( newInputsArray )
}
const addOption = ( event ) => {
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 )
}
const handleChangeInput = ( event ) => {
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 )
}
const handleSubmit = async ( event ) => {
event . preventDefault ( )
let Notyf
if ( typeof window !== 'undefined' ) {
Notyf = require ( 'notyf' )
2020-04-16 19:44:19 +02:00
}
2020-08-03 12:04:07 +02:00
const notyf = new Notyf . Notyf ( {
duration : 5000
} )
try {
await api . put ( ` /admin/functions/form/ ${ props . functionInfo . id } ` , { form : inputsArray } , { headers : { Authorization : props . user . token } } )
notyf . success ( 'Sauvegardé!' )
} catch ( error ) {
notyf . error ( 'Erreur!' )
2020-04-16 19:44:19 +02:00
}
2020-08-03 12:04:07 +02:00
}
const handleRemoveInput = ( event ) => {
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 } >
{ ( inputsArray . length > 0 ) &&
< div className = 'form-group text-center' >
< button type = 'submit' className = 'btn btn-dark' > Sauvegarder < / b u t t o n >
< / d i v > }
{ inputsArray . map ( ( input , index ) => {
return (
< div key = { index } className = 'form-group Admin__Input-group' >
< div className = 'text-center' >
< button type = 'button' onClick = { handleRemoveInput } id = { ` remove- ${ index } ` } className = 'btn btn-dark' > Supprimer l ' input < / b u t t o n >
< / d i v >
< label className = 'form-label' htmlFor = { ` name- ${ index } ` } > Nom de l ' input : < / l a b e l >
< input value = { input . name } onChange = { handleChangeInput } type = 'text' name = 'name' id = { ` name- ${ index } ` } className = 'form-control' placeholder = '(e.g : cityName)' / >
< br / >
< label className = 'form-label' htmlFor = { ` label- ${ index } ` } > Label : < / l a b e l >
< 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 :)" / >
< br / >
{ ( input . type !== 'select' ) &&
< >
< label className = 'form-label' htmlFor = { ` placeholder- ${ index } ` } > Placeholder : < / l a b e l >
< input value = { input . placeholder } onChange = { handleChangeInput } type = 'text' name = 'placeholder' id = { ` placeholder- ${ index } ` } className = 'form-control' placeholder = '(e.g : Paris, FR)' / >
< br / >
< / > }
< label className = 'form-label' htmlFor = { ` type- ${ index } ` } > Type : < / l a b e l >
< select value = { input . type } onChange = { handleChangeInput } name = 'type' id = { ` type- ${ index } ` } className = 'form-control' >
< option value = 'text' > text < / o p t i o n >
< option value = 'integer' > Number integer < / o p t i o n >
< option value = 'float' > Number float < / o p t i o n >
< option value = 'calendar' > calendar < / o p t i o n >
< option value = 'select' > select < / o p t i o n >
< / s e l e c t >
{ ( input . type === 'select' ) &&
< div style = { { marginTop : '50px' } } >
< label className = 'form-label' > Options : < / l a b e l >
{ input . options . map ( ( option , optionIndex ) => {
2020-04-16 19:44:19 +02:00
return (
2020-08-03 12:04:07 +02:00
< div key = { optionIndex } style = { { margin : '0 0 30px 0' } } className = 'form-group Admin__Input-group' >
< div className = 'text-center' >
< button type = 'button' onClick = { ( ) => handleRemoveOption ( index , optionIndex ) } className = 'btn btn-dark' > Supprimer l ' option < / b u t t o n >
2020-04-16 19:44:19 +02:00
< / d i v >
2020-08-03 12:04:07 +02:00
< label className = 'form-label' htmlFor = { ` optionName- ${ optionIndex } - ${ index } ` } > Nom de l ' option < / l a b e l >
< 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" / >
< br / >
< label className = 'form-label' htmlFor = { ` optionValue- ${ optionIndex } - ${ index } ` } > Valeur de l ' option < / l a b e l >
< 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" / >
< / d i v >
)
} ) }
2020-04-16 19:44:19 +02:00
2020-08-03 12:04:07 +02:00
< div className = 'form-group text-center' >
< button id = { ` optionAdd- ${ index } ` } onClick = { addOption } type = 'button' className = 'btn btn-dark' > Ajouter une option < / b u t t o n >
< / d i v >
< / d i v > }
2020-04-16 19:44:19 +02:00
< / d i v >
2020-08-03 12:04:07 +02:00
)
} ) }
< / f o r m >
< div style = { { marginBottom : '30px' } } className = 'form-group text-center' >
< button type = 'button' onClick = { addInput } className = 'btn btn-dark' > Ajouter un input < / b u t t o n >
< / d i v >
< / d i v >
)
2020-04-16 19:44:19 +02:00
}
2020-08-03 12:04:07 +02:00
export default EditFormFunction