Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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