2020-04-21 18:03:16 +02:00
import { useState , Fragment } from 'react' ;
2020-04-23 17:31:36 +02:00
import api from '../../utils/api' ;
2020-04-16 19:44:19 +02:00
import 'notyf/notyf.min.css' ;
const EditFormFunction = ( props ) => {
const [ inputsArray , setInputsArray ] = useState ( props . functionInfo . utilizationForm || [ ] ) ;
const addInput = ( ) => {
const newInputsArray = [ ... inputsArray ] ;
newInputsArray . push ( { name : "" , label : "" , placeholder : "" , type : "text" } ) ;
setInputsArray ( newInputsArray ) ;
}
2020-04-21 18:03:16 +02:00
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 ) => {
2020-04-16 19:44:19 +02:00
const newInputsArray = [ ... inputsArray ] ;
2020-04-21 18:03:16 +02:00
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 ] ;
2020-04-16 19:44:19 +02:00
inputObject [ event . target . name ] = event . target . value ;
2020-04-21 18:03:16 +02:00
if ( event . target . value === "select" ) {
inputObject . options = [ ] ;
}
2020-04-16 19:44:19 +02:00
setInputsArray ( newInputsArray ) ;
}
const handleSubmit = async ( event ) => {
event . preventDefault ( ) ;
let Notyf ;
if ( typeof window != 'undefined' ) {
Notyf = require ( 'notyf' ) ;
}
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!' ) ;
}
}
const handleRemoveInput = ( event ) => {
const newInputsArray = [ ... inputsArray ] ;
2020-04-21 18:03:16 +02:00
const index = event . target . id . split ( '-' ) [ 1 ] ;
2020-04-16 19:44:19 +02:00
newInputsArray . splice ( index , 1 ) ;
setInputsArray ( newInputsArray ) ;
}
2020-04-21 18:03:16 +02:00
const handleRemoveOption = ( inputIndex , optionIndex ) => {
const newInputsArray = [ ... inputsArray ] ;
const inputObject = newInputsArray [ inputIndex ] ;
const optionsArray = inputObject . options ;
optionsArray . splice ( optionIndex , 1 ) ;
setInputsArray ( newInputsArray ) ;
}
2020-04-16 19:44:19 +02:00
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" >
2020-04-21 18:03:16 +02:00
< 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 >
2020-04-16 19:44:19 +02:00
< input value = { input . name } onChange = { handleChangeInput } type = "text" name = "name" id = { ` name- ${ index } ` } className = "form-control" placeholder = "(e.g : cityName)" / >
< br / >
2020-04-21 18:03:16 +02:00
< label className = "form-label" htmlFor = { ` label- ${ index } ` } > Label : < / l a b e l >
2020-04-16 19:44:19 +02:00
< 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 / >
2020-04-21 18:03:16 +02:00
{ ( input . type !== "select" ) &&
< Fragment >
< 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 / >
< / F r a g m e n t >
}
< label className = "form-label" htmlFor = { ` type- ${ index } ` } > Type : < / l a b e l >
2020-04-16 19:44:19 +02:00
< select value = { input . type } onChange = { handleChangeInput } name = "type" id = { ` type- ${ index } ` } className = "form-control" >
< option value = "text" > text < / o p t i o n >
2020-04-21 18:03:16 +02:00
< option value = "integer" > Number integer < / o p t i o n >
< option value = "float" > Number float < / o p t i o n >
2020-04-20 17:53:44 +02:00
< option value = "calendar" > calendar < / o p t i o n >
2020-04-21 18:03:16 +02:00
< option value = "select" > select < / o p t i o n >
2020-04-16 19:44:19 +02:00
< / s e l e c t >
2020-04-21 18:03:16 +02:00
{ ( input . type === "select" ) &&
< div style = { { marginTop : '50px' } } >
< label className = "form-label" > Options : < / l a b e l >
{ input . options . map ( ( option , optionIndex ) => {
return (
< 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 >
< / d i v >
< 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 >
) ;
} ) }
< 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 >
) ;
} ) }
< / 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 >
) ;
}
export default EditFormFunction ;