Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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