Rev 456 | Rev 470 | Ir a la última revisión | Autoría | Comparar con el anterior | Ultima modificación | Ver Log |
import React, { useState, useEffect } from "react";
import { axios } from '../../../utils';
import Section from "./section/Section";
import Spinner from "../../../shared/loading-spinner/Spinner";
const Test = (props) => {
//init states
const [success, setSuccess] = useState(false);
const [draft, setDraft] = useState(false);
const [page, setPage] = useState(0);
// get props
const { backendVars, test, loading, setTest, action } = props;
/**
* Send form data
*/
const handleSubmit = async () => {
//init form data
setDraft(false);
setSuccess(false);
const formData = new FormData();
let status = validateForm() ? 'p' : 'd';
formData.append("content", JSON.stringify(test.content));
formData.append("status", status);
await axios.post(action, formData).then((response) =>
response.data.success && validateForm() ?
setSuccess(true) :
setDraft(true)
);
}
/**
* Check if there are questions to answer
* @returns
*/
const validateForm = () => {
let formValid = true;
test.content.map((section) => {
section.questions.map((question) => {
//Validate if the answer is empty
if (!question.answer || question.answer.length == 0) {
formValid = false;
}
})
})
return formValid;
}
/**
* Cancel test and send to the list of forms
* @returns
*/
const handleGoBack = () => {
setTest(null);
setSuccess(false)
}
/**
* componentDidMount
*/
useEffect(() => {
setPage(0);
}, [action]);
return (
<div>
{loading ? (
<div className="row">
<Spinner />
</div>
) : (
<div>
{!success ? (
<div className="row test-section">
<div className="col-md-12 col-sm-12 col-xs-12">
<div className="company-title">
<div className="section_admin_title_buttons">
<h1 className="title">{test.name}</h1>
</div>
</div>
<div
dangerouslySetInnerHTML={{ __html: test.text }}
className="description company-title"
></div>
</div>
{test.content.length <= 0 ? (
<div className="col-md-12 col-sm-12 col-xs-12 text-center">
{backendVars.LBL_DATATABLE_SZERORECORDS}
</div>
) : (
<div className="col-md-12 col-sm-12 col-xs-12">
<div className="company-title">
{test.content.map((section, key) => {
return (
<Section
section={section}
index={key}
page={page}
setPage={setPage}
total={test.content.length}
backendVars={backendVars}
/>)
})}
</div>
</div>
)}
{draft &&
<div className="col-md-12 col-sm-12 col-xs-12 text-right">
<div className="alert alert-success alert-dismissible fade show" role="alert" >
Sus cambios se han guardado con éxito
<button
type="button"
className="close"
data-dismiss="alert" aria-label="Close"
onClick={() => setDraft(false)}
>
<span aria-hidden="true">×</span>
</button>
</div>
</div>
}
<div className="col-md-12 col-sm-12 col-xs-12 text-right">
<div className="company-title">
<button
type="button"
className="btn btn-secondary"
onClick={() => handleGoBack()}>
{backendVars.LBL_GO_BACK}
</button>
<button
type="buttton"
className="btn btn-primary"
onClick={() => handleSubmit()}>
{backendVars.LBL_SAVE}
</button>
</div>
</div>
</div>
) : (
<div className="row">
<div className="company-title text-center">
<div className="section_admin_title_buttons">
<h1 className="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
</div>
</div>
<div className="col-md-12 col-sm-12 col-xs-12 text-center">
<br />
<button
className="btn btn-sm btn-primary"
onClick={() => handleGoBack()}>
{backendVars.LBL_GO_BACK}
</button>
</div>
</div>
)}
</div>
)}
</div>
)
}
export default Test;