Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 348 | Rev 350 | 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
344 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) {
349 geraldo 35
                        if(question.type == 'multiple'){
345 geraldo 36
 
349 geraldo 37
                            if(!question.answer){
38
                                question.answer = [answer];
39
                            }else{
40
                               question.answer.push(answer)
41
                            }
42
 
43
 
44
                        } else{
45
                            question.answer = answer;
46
                        }
47
 
48
 
49
 
340 geraldo 50
                    }
51
                })
52
            }
343 geraldo 53
        });
54
        console.log(test);
340 geraldo 55
        setTest(test);
56
    }
57
    /**
58
     * Check if there are questions to answer
59
     * @returns
60
     */
61
    const validateForm = () => {
62
        let formValid = true;
63
        test.content.map((section) => {
64
            section.questions.map((question) => {
65
                if (!question.answer) {
66
                    console.error(`Debe ingresar una respuesta en la pregunta ${question.position + 1} de la sección ${section.name}`);
67
                    formValid = false;
68
                }
336 geraldo 69
            })
70
        })
340 geraldo 71
 
72
        return formValid;
306 geraldo 73
    }
74
 
340 geraldo 75
 
306 geraldo 76
    return (
77
        <div>
78
            {loading ? (
79
                <div className="row">
80
                    <Spinner />
81
                </div>
82
            ) : (
311 geraldo 83
                <div className="row test-section">
340 geraldo 84
                    <div className="col-md-12 col-sm-12 col-xs-12">
85
                        <div className="company-title">
86
                            <div className="section_admin_title_buttons">
87
                                <h1 className="title">{test.name}</h1>
307 geraldo 88
                            </div>
306 geraldo 89
                        </div>
340 geraldo 90
                        <div
91
                            dangerouslySetInnerHTML={{ __html: test.text }}
92
                            className="description company-title"
93
                        ></div>
94
                    </div>
95
                    <div className="col-md-12 col-sm-12 col-xs-12">
96
                        <div className="company-title">
97
                            {test.content.map((section, key) => {
98
                                return <Section
99
                                    section={section}
100
                                    key={key}
101
                                    backendVars={backendVars}
102
                                    handleAnswer={handleAnswer}
103
                                />
104
                            })}
306 geraldo 105
                        </div>
340 geraldo 106
                    </div>
107
                    <div className="col-md-12 col-sm-12 col-xs-12">
108
                        <div className="company-title">
109
                            <button type="button" className="btn btn-danger" onClick={() => setTest(null)}>{backendVars.LBL_CANCEL}</button>
110
                            <button type="buttton" className="btn btn-success" onClick={() => handleSubmit()}>{backendVars.LBL_SAVE}</button>
307 geraldo 111
                        </div>
340 geraldo 112
                    </div>
306 geraldo 113
                </div>
114
            )}
115
        </div>
116
    )
117
}
118
 
119
export default Test;