Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
361 geraldo 1
import React, { useState } 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";
361 geraldo 5
import ConfirmationBox from "../../../shared/confirmation-box/ConfirmationBox";
306 geraldo 6
 
7
const Test = (props) => {
8
 
360 geraldo 9
    //init states
361 geraldo 10
    const [confirmationBoxShow, setConfirmationBoxShow] = useState(false);
364 geraldo 11
    const [page, setPage] = useState(0);
360 geraldo 12
 
306 geraldo 13
    // get props
344 geraldo 14
    const { backendVars, test, loading, setTest, action } = props;
306 geraldo 15
 
340 geraldo 16
    /**
17
     * Send form data
18
     */
19
    const handleSubmit = async () => {
20
        if (validateForm()) {
356 geraldo 21
            //init form data
355 geraldo 22
            const formData = new FormData();
23
            formData.append("content", JSON.stringify(test));
360 geraldo 24
            await axios.post(action, formData).then((response) => {
340 geraldo 25
                if (response.data.success) {
26
                    console.info('Formulario almacenado');
27
                    setTest(null);
336 geraldo 28
                }
340 geraldo 29
            });
30
        }
31
    }
32
    /**
33
     * Update question answer
34
     * @param {*} slug_section
35
     * @param {*} slug_question
36
     * @param {*} answer
37
     */
38
    const handleAnswer = (slug_section, slug_question, answer) => {
39
        test.content.filter((section) => {
40
            if (section.slug_section == slug_section) {
41
                section.questions.map((question) => {
42
                    if (question.slug_question == slug_question) {
356 geraldo 43
                        //valid if the question has more than one answer
350 geraldo 44
                        question.type == 'multiple' ?
45
                            !question.answer ?
46
                                question.answer = [answer] :
47
                                question.answer.push(answer)
48
                            : question.answer = answer;
345 geraldo 49
 
340 geraldo 50
                    }
51
                })
52
            }
343 geraldo 53
        });
340 geraldo 54
        setTest(test);
55
    }
56
    /**
57
     * Check if there are questions to answer
58
     * @returns
59
     */
60
    const validateForm = () => {
61
        let formValid = true;
62
        test.content.map((section) => {
63
            section.questions.map((question) => {
356 geraldo 64
                //Validate if the answer is empty
340 geraldo 65
                if (!question.answer) {
66
                    formValid = false;
67
                }
336 geraldo 68
            })
69
        })
340 geraldo 70
 
71
        return formValid;
306 geraldo 72
    }
73
 
361 geraldo 74
    /**
75
     * Update component status
76
     * @returns
77
     */
78
    const handleConfirmationBoxShow = () => setConfirmationBoxShow(!confirmationBoxShow);
340 geraldo 79
 
361 geraldo 80
    /**
81
     * Cancel test and send to the list of forms
82
     * @returns
83
     */
84
    const handleCancel = () => setTest('');
357 geraldo 85
 
306 geraldo 86
    return (
87
        <div>
88
            {loading ? (
89
                <div className="row">
90
                    <Spinner />
91
                </div>
92
            ) : (
311 geraldo 93
                <div className="row test-section">
340 geraldo 94
                    <div className="col-md-12 col-sm-12 col-xs-12">
95
                        <div className="company-title">
96
                            <div className="section_admin_title_buttons">
97
                                <h1 className="title">{test.name}</h1>
307 geraldo 98
                            </div>
306 geraldo 99
                        </div>
340 geraldo 100
                        <div
101
                            dangerouslySetInnerHTML={{ __html: test.text }}
102
                            className="description company-title"
103
                        ></div>
104
                    </div>
105
                    <div className="col-md-12 col-sm-12 col-xs-12">
106
                        <div className="company-title">
107
                            {test.content.map((section, key) => {
108
                                return <Section
109
                                    section={section}
110
                                    key={key}
364 geraldo 111
                                    page={page}
112
                                    setPage ={setPage}
340 geraldo 113
                                    backendVars={backendVars}
114
                                    handleAnswer={handleAnswer}
115
                                />
116
                            })}
306 geraldo 117
                        </div>
340 geraldo 118
                    </div>
360 geraldo 119
                    <div className="col-md-12 col-sm-12 col-xs-12 text-right">
340 geraldo 120
                        <div className="company-title">
361 geraldo 121
                            <button
122
                                type="button"
123
                                className="btn btn-danger"
124
                                onClick={handleConfirmationBoxShow}>
360 geraldo 125
                                {backendVars.LBL_CANCEL}
126
                            </button>
361 geraldo 127
 
128
                            <ConfirmationBox
129
                                show={confirmationBoxShow}
130
                                onClose={handleConfirmationBoxShow}
131
                                onAccept={handleCancel}
132
                            />
133
 
134
 
135
                            <button
136
                                type="buttton"
137
                                className="btn btn-primary"
138
                                onClick={() => handleSubmit()}>
360 geraldo 139
                                {backendVars.LBL_SAVE}
140
                            </button>
307 geraldo 141
                        </div>
340 geraldo 142
                    </div>
306 geraldo 143
                </div>
144
            )}
361 geraldo 145
 
146
 
306 geraldo 147
        </div>
148
    )
149
}
150
 
151
export default Test;