Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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