Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 375 | Rev 377 | 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);
340 geraldo 72
        return formValid;
306 geraldo 73
    }
74
 
361 geraldo 75
    /**
76
     * Update component status
77
     * @returns
78
     */
79
    const handleConfirmationBoxShow = () => setConfirmationBoxShow(!confirmationBoxShow);
340 geraldo 80
 
361 geraldo 81
    /**
82
     * Cancel test and send to the list of forms
83
     * @returns
365 geraldo 84
     */
361 geraldo 85
    const handleCancel = () => setTest('');
357 geraldo 86
 
306 geraldo 87
    return (
88
        <div>
89
            {loading ? (
90
                <div className="row">
91
                    <Spinner />
92
                </div>
93
            ) : (
311 geraldo 94
                <div className="row test-section">
340 geraldo 95
                    <div className="col-md-12 col-sm-12 col-xs-12">
96
                        <div className="company-title">
97
                            <div className="section_admin_title_buttons">
98
                                <h1 className="title">{test.name}</h1>
307 geraldo 99
                            </div>
306 geraldo 100
                        </div>
340 geraldo 101
                        <div
102
                            dangerouslySetInnerHTML={{ __html: test.text }}
103
                            className="description company-title"
104
                        ></div>
105
                    </div>
365 geraldo 106
                    {test.content.length <= 0 ? (
107
                        <div className="col-md-12 col-sm-12 col-xs-12 text-center">
108
                            {backendVars.LBL_DATATABLE_SZERORECORDS}
306 geraldo 109
                        </div>
365 geraldo 110
                    ) : (
111
                        <div className="col-md-12 col-sm-12 col-xs-12">
112
                            <div className="company-title">
113
                                {test.content.map((section, key) => {
114
                                    return <Section
115
                                        section={section}
116
                                        key={key}
117
                                        page={page}
118
                                        setPage={setPage}
119
                                        total={test.content.length}
120
                                        backendVars={backendVars}
121
                                        handleAnswer={handleAnswer}
122
                                    />
123
                                })}
124
                            </div>
125
                        </div>)}
360 geraldo 126
                    <div className="col-md-12 col-sm-12 col-xs-12 text-right">
340 geraldo 127
                        <div className="company-title">
361 geraldo 128
                            <button
129
                                type="button"
130
                                className="btn btn-danger"
131
                                onClick={handleConfirmationBoxShow}>
360 geraldo 132
                                {backendVars.LBL_CANCEL}
133
                            </button>
361 geraldo 134
 
135
                            <ConfirmationBox
136
                                show={confirmationBoxShow}
137
                                onClose={handleConfirmationBoxShow}
138
                                onAccept={handleCancel}
139
                            />
140
 
141
 
142
                            <button
143
                                type="buttton"
376 geraldo 144
                                className="btn btn-success"
145
                                disabled ={!this.state.valid}
361 geraldo 146
                                onClick={() => handleSubmit()}>
360 geraldo 147
                                {backendVars.LBL_SAVE}
148
                            </button>
307 geraldo 149
                        </div>
340 geraldo 150
                    </div>
306 geraldo 151
                </div>
152
            )}
361 geraldo 153
 
154
 
306 geraldo 155
        </div>
156
    )
157
}
158
 
159
export default Test;