2020-03-24 16:11:55 +01:00
|
|
|
import { useState } from 'react';
|
2020-03-25 18:22:03 +01:00
|
|
|
import Loader from './Loader';
|
2020-03-24 16:11:55 +01:00
|
|
|
import htmlParser from 'html-react-parser';
|
2020-03-25 18:22:03 +01:00
|
|
|
import api from '../utils/api';
|
2020-03-24 16:11:55 +01:00
|
|
|
|
|
|
|
const FunctionForm = (props) => {
|
|
|
|
// console.log(props);
|
|
|
|
|
|
|
|
const [inputState, setInputState] = useState({});
|
|
|
|
const [message, setMessage] = useState("");
|
|
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
|
|
|
|
|
|
const handleSubmit = (event) => {
|
2020-03-25 18:46:23 +01:00
|
|
|
setIsLoading(true);
|
2020-03-24 16:11:55 +01:00
|
|
|
event.preventDefault();
|
|
|
|
api.post(`/functions/${props.slug}`, inputState)
|
|
|
|
.then((response) => {
|
|
|
|
setMessage(response.data.resultHTML);
|
2020-03-25 18:46:23 +01:00
|
|
|
setIsLoading(false);
|
2020-03-24 16:11:55 +01:00
|
|
|
})
|
|
|
|
.catch((error) => {
|
|
|
|
setMessage(error.response.data.message);
|
2020-03-25 18:46:23 +01:00
|
|
|
setIsLoading(false);
|
2020-03-24 16:11:55 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
const handleChange = (event) => {
|
|
|
|
const inputStateNew = { ...inputState };
|
|
|
|
inputStateNew[event.target.name] = event.target.value;
|
|
|
|
setInputState(inputStateNew);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (props.inputArray.length <= 0) {
|
|
|
|
return (
|
|
|
|
<div className="FunctionComponent__slide text-center">
|
|
|
|
<p>La fonction n'est pas encore disponible.</p>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div className="FunctionComponent__slide">
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
{props.inputArray.map((input, index) => {
|
|
|
|
let inputResult;
|
|
|
|
switch(input.type) {
|
|
|
|
case "text" || "number":
|
2020-03-25 18:22:03 +01:00
|
|
|
inputResult = (<input onChange={handleChange} type={input.type} name={input.name} id={input.name} placeholder={input.placeholder} className="form-control" />);
|
2020-03-24 16:11:55 +01:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
inputResult = (<p>Erreur, l'input n'est pas valide...</p>);
|
|
|
|
}
|
|
|
|
return (
|
2020-03-25 18:22:03 +01:00
|
|
|
<div key={index} className="form-group">
|
|
|
|
<label className="form-label" htmlFor={input.name}>{input.label}</label>
|
2020-03-24 16:11:55 +01:00
|
|
|
{inputResult}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
|
2020-03-25 18:22:03 +01:00
|
|
|
<div className="form-group text-center">
|
2020-03-24 16:11:55 +01:00
|
|
|
<button type="submit" className="btn btn-dark">Envoyer</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2020-03-25 18:22:03 +01:00
|
|
|
<div className="form-result text-center">
|
2020-03-24 16:11:55 +01:00
|
|
|
{
|
|
|
|
(isLoading) ?
|
|
|
|
<Loader />
|
|
|
|
:
|
|
|
|
htmlParser(message)
|
|
|
|
}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export default FunctionForm;
|