Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 380 | Rev 384 | 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);
382 geraldo 12
    const [success, setSuccess] = useState(false);
364 geraldo 13
    const [page, setPage] = useState(0);
360 geraldo 14
 
306 geraldo 15
    // get props
344 geraldo 16
    const { backendVars, test, loading, setTest, action } = props;
306 geraldo 17
 
340 geraldo 18
    /**
19
     * Send form data
20
     */
21
    const handleSubmit = async () => {
22
        if (validateForm()) {
356 geraldo 23
            //init form data
355 geraldo 24
            const formData = new FormData();
25
            formData.append("content", JSON.stringify(test));
360 geraldo 26
            await axios.post(action, formData).then((response) => {
340 geraldo 27
                if (response.data.success) {
382 geraldo 28
                    setSuccess(true);
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] :
379 geraldo 48
                                question.answer.includes(answer) ?
49
                                    question.answer = removeOptionMultiple(question.answer, answer) :
50
                                    question.answer.push(answer)
350 geraldo 51
                            : question.answer = answer;
345 geraldo 52
 
340 geraldo 53
                    }
54
                })
55
            }
343 geraldo 56
        });
378 geraldo 57
        validateForm();
340 geraldo 58
        setTest(test);
59
    }
378 geraldo 60
 
382 geraldo 61
    /**
62
     * Delete existing option
63
     * @param {*} arr
64
     * @param {*} item
65
     * @returns
66
     */
67
    const removeOptionMultiple = (arr, item) => arr.splice(arr.indexOf(item), 1);
380 geraldo 68
 
340 geraldo 69
    /**
70
     * Check if there are questions to answer
71
     * @returns
72
     */
73
    const validateForm = () => {
74
        let formValid = true;
75
        test.content.map((section) => {
76
            section.questions.map((question) => {
356 geraldo 77
                //Validate if the answer is empty
382 geraldo 78
                if (!question.answer || question.answer.length == 0) {
340 geraldo 79
                    formValid = false;
80
                }
336 geraldo 81
            })
82
        })
375 geraldo 83
        setValid(formValid);
340 geraldo 84
        return formValid;
306 geraldo 85
    }
86
 
361 geraldo 87
    /**
88
     * Update component status
89
     * @returns
90
     */
91
    const handleConfirmationBoxShow = () => setConfirmationBoxShow(!confirmationBoxShow);
340 geraldo 92
 
361 geraldo 93
    /**
94
     * Cancel test and send to the list of forms
95
     * @returns
365 geraldo 96
     */
361 geraldo 97
    const handleCancel = () => setTest('');
357 geraldo 98
 
306 geraldo 99
    return (
100
        <div>
101
            {loading ? (
102
                <div className="row">
103
                    <Spinner />
104
                </div>
105
            ) : (
382 geraldo 106
                <div>
107
                    {!success ? (
108
                        <div className="row test-section">
109
                            <div className="col-md-12 col-sm-12 col-xs-12">
110
                                <div className="company-title">
111
                                    <div className="section_admin_title_buttons">
112
                                        <h1 className="title">{test.name}</h1>
113
                                    </div>
114
                                </div>
115
                                <div
116
                                    dangerouslySetInnerHTML={{ __html: test.text }}
117
                                    className="description company-title"
118
                                ></div>
307 geraldo 119
                            </div>
382 geraldo 120
                            {test.content.length <= 0 ? (
121
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
122
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
123
                                </div>
124
                            ) : (
125
                                <div className="col-md-12 col-sm-12 col-xs-12">
126
                                    <div className="company-title">
127
                                        {test.content.map((section, key) => {
128
                                            return <Section
129
                                                section={section}
130
                                                key={key}
131
                                                page={page}
132
                                                setPage={setPage}
133
                                                total={test.content.length}
134
                                                backendVars={backendVars}
135
                                                handleAnswer={handleAnswer}
136
                                            />
137
                                        })}
138
                                    </div>
139
                                </div>)}
140
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
141
                                <div className="company-title">
142
                                    <button
143
                                        type="button"
144
                                        className="btn btn-secondary"
145
                                        onClick={handleConfirmationBoxShow}>
146
                                        {backendVars.LBL_CANCEL}
147
                                    </button>
148
 
149
                                    <ConfirmationBox
150
                                        show={confirmationBoxShow}
151
                                        onClose={handleConfirmationBoxShow}
152
                                        onAccept={handleCancel}
365 geraldo 153
                                    />
361 geraldo 154
 
155
 
382 geraldo 156
                                    <button
157
                                        type="buttton"
158
                                        className="btn btn-primary"
159
                                        disabled={!valid}
160
                                        onClick={() => handleSubmit()}>
161
                                        {backendVars.LBL_SAVE}
162
                                    </button>
163
                                </div>
164
                            </div>
307 geraldo 165
                        </div>
382 geraldo 166
                    ) : (
167
                        <div className="row">
168
                            <div className="col-md-12 col-sm-12 col-xs-12">
169
                                <h2>Su prueba se ha enviado con éxito</h2>
170
                                <a onClick={() => {
171
                                    setTest(null);
172
                                    setSuccess(false)
173
                                }}>{backendVars.LBL_GO_BACK}</a>
174
                            </div>
175
                        </div>
176
                    )}
306 geraldo 177
                </div>
178
            )}
361 geraldo 179
 
180
 
306 geraldo 181
        </div>
182
    )
183
}
184
 
185
export default Test;