Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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