Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 354 | Rev 356 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
306 geraldo 1
import React from "react";
352 geraldo 2
import { axios } from '../../../utils';
306 geraldo 3
import Section from "./section/Section";
4
import Spinner from "../../../shared/loading-spinner/Spinner";
5
 
6
const Test = (props) => {
7
 
8
    // get props
344 geraldo 9
    const { backendVars, test, loading, setTest, action } = props;
306 geraldo 10
 
340 geraldo 11
 
12
    /**
13
     * Send form data
14
     */
15
    const handleSubmit = async () => {
16
        if (validateForm()) {
355 geraldo 17
            const formData = new FormData();
18
            formData.append("content", JSON.stringify(test));
19
            await axios.post(action,  formData ).then((response) => {
340 geraldo 20
                if (response.data.success) {
21
                    console.info('Formulario almacenado');
22
                    setTest(null);
336 geraldo 23
                }
340 geraldo 24
            });
25
        }
26
    }
27
    /**
28
     * Update question answer
29
     * @param {*} slug_section
30
     * @param {*} slug_question
31
     * @param {*} answer
32
     */
33
    const handleAnswer = (slug_section, slug_question, answer) => {
34
        test.content.filter((section) => {
35
            if (section.slug_section == slug_section) {
36
                section.questions.map((question) => {
37
                    if (question.slug_question == slug_question) {
350 geraldo 38
                        question.type == 'multiple' ?
39
                            !question.answer ?
40
                                question.answer = [answer] :
41
                                question.answer.push(answer)
42
                            : question.answer = answer;
345 geraldo 43
 
340 geraldo 44
                    }
45
                })
46
            }
343 geraldo 47
        });
340 geraldo 48
        setTest(test);
49
    }
50
    /**
51
     * Check if there are questions to answer
52
     * @returns
53
     */
54
    const validateForm = () => {
55
        let formValid = true;
56
        test.content.map((section) => {
57
            section.questions.map((question) => {
58
                if (!question.answer) {
59
                    console.error(`Debe ingresar una respuesta en la pregunta ${question.position + 1} de la sección ${section.name}`);
60
                    formValid = false;
61
                }
336 geraldo 62
            })
63
        })
340 geraldo 64
 
65
        return formValid;
306 geraldo 66
    }
67
 
340 geraldo 68
 
306 geraldo 69
    return (
70
        <div>
71
            {loading ? (
72
                <div className="row">
73
                    <Spinner />
74
                </div>
75
            ) : (
311 geraldo 76
                <div className="row test-section">
340 geraldo 77
                    <div className="col-md-12 col-sm-12 col-xs-12">
78
                        <div className="company-title">
79
                            <div className="section_admin_title_buttons">
80
                                <h1 className="title">{test.name}</h1>
307 geraldo 81
                            </div>
306 geraldo 82
                        </div>
340 geraldo 83
                        <div
84
                            dangerouslySetInnerHTML={{ __html: test.text }}
85
                            className="description company-title"
86
                        ></div>
87
                    </div>
88
                    <div className="col-md-12 col-sm-12 col-xs-12">
89
                        <div className="company-title">
90
                            {test.content.map((section, key) => {
91
                                return <Section
92
                                    section={section}
93
                                    key={key}
94
                                    backendVars={backendVars}
95
                                    handleAnswer={handleAnswer}
96
                                />
97
                            })}
306 geraldo 98
                        </div>
340 geraldo 99
                    </div>
100
                    <div className="col-md-12 col-sm-12 col-xs-12">
101
                        <div className="company-title">
102
                            <button type="button" className="btn btn-danger" onClick={() => setTest(null)}>{backendVars.LBL_CANCEL}</button>
103
                            <button type="buttton" className="btn btn-success" onClick={() => handleSubmit()}>{backendVars.LBL_SAVE}</button>
307 geraldo 104
                        </div>
340 geraldo 105
                    </div>
306 geraldo 106
                </div>
107
            )}
108
        </div>
109
    )
110
}
111
 
112
export default Test;