Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 364 | Rev 375 | 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
365 geraldo 83
     */
361 geraldo 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>
365 geraldo 105
                    {test.content.length <= 0 ? (
106
                        <div className="col-md-12 col-sm-12 col-xs-12 text-center">
107
                            {backendVars.LBL_DATATABLE_SZERORECORDS}
306 geraldo 108
                        </div>
365 geraldo 109
                    ) : (
110
                        <div className="col-md-12 col-sm-12 col-xs-12">
111
                            <div className="company-title">
112
                                {test.content.map((section, key) => {
113
                                    return <Section
114
                                        section={section}
115
                                        key={key}
116
                                        page={page}
117
                                        setPage={setPage}
118
                                        total={test.content.length}
119
                                        backendVars={backendVars}
120
                                        handleAnswer={handleAnswer}
121
                                    />
122
                                })}
123
                            </div>
124
                        </div>)}
360 geraldo 125
                    <div className="col-md-12 col-sm-12 col-xs-12 text-right">
340 geraldo 126
                        <div className="company-title">
361 geraldo 127
                            <button
128
                                type="button"
129
                                className="btn btn-danger"
130
                                onClick={handleConfirmationBoxShow}>
360 geraldo 131
                                {backendVars.LBL_CANCEL}
132
                            </button>
361 geraldo 133
 
134
                            <ConfirmationBox
135
                                show={confirmationBoxShow}
136
                                onClose={handleConfirmationBoxShow}
137
                                onAccept={handleCancel}
138
                            />
139
 
140
 
141
                            <button
142
                                type="buttton"
143
                                className="btn btn-primary"
144
                                onClick={() => handleSubmit()}>
360 geraldo 145
                                {backendVars.LBL_SAVE}
146
                            </button>
307 geraldo 147
                        </div>
340 geraldo 148
                    </div>
306 geraldo 149
                </div>
150
            )}
361 geraldo 151
 
152
 
306 geraldo 153
        </div>
154
    )
155
}
156
 
157
export default Test;