Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 384 | Rev 387 | 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";
384 geraldo 5
import ConfirmModal from "../../../shared/confirm-modal/ConfirmModal";
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">
384 geraldo 109
 
110
                            <ConfirmModal
385 geraldo 111
                                show={confirmationBoxShow}
112
                                title={backendVars.LBL_TITLE_CONFIRM_SELF_EVALUATION}
113
                                message={backendVars.LBL_TEXT_CONFIRM_SELF_EVALUATION}
114
                                acceptLabel={backendVars.LBL_BTN_CONFIRM_SELF_EVALUATION}
115
                                onClose = {handleConfirmationBoxShow}
384 geraldo 116
                                onAccept={handleCancel}
117
                            />
118
 
382 geraldo 119
                            <div className="col-md-12 col-sm-12 col-xs-12">
120
                                <div className="company-title">
121
                                    <div className="section_admin_title_buttons">
122
                                        <h1 className="title">{test.name}</h1>
123
                                    </div>
124
                                </div>
125
                                <div
126
                                    dangerouslySetInnerHTML={{ __html: test.text }}
127
                                    className="description company-title"
128
                                ></div>
307 geraldo 129
                            </div>
382 geraldo 130
                            {test.content.length <= 0 ? (
131
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
132
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
133
                                </div>
134
                            ) : (
135
                                <div className="col-md-12 col-sm-12 col-xs-12">
136
                                    <div className="company-title">
137
                                        {test.content.map((section, key) => {
384 geraldo 138
                                            return (
139
                                                <Section
140
                                                    section={section}
141
                                                    key={key}
142
                                                    page={page}
143
                                                    setPage={setPage}
144
                                                    total={test.content.length}
145
                                                    backendVars={backendVars}
146
                                                    handleAnswer={handleAnswer}
147
                                                />)
382 geraldo 148
                                        })}
149
                                    </div>
384 geraldo 150
                                </div>
151
                            )}
382 geraldo 152
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
153
                                <div className="company-title">
154
                                    <button
155
                                        type="button"
156
                                        className="btn btn-secondary"
157
                                        onClick={handleConfirmationBoxShow}>
158
                                        {backendVars.LBL_CANCEL}
159
                                    </button>
160
                                    <button
161
                                        type="buttton"
162
                                        className="btn btn-primary"
163
                                        disabled={!valid}
164
                                        onClick={() => handleSubmit()}>
165
                                        {backendVars.LBL_SAVE}
166
                                    </button>
167
                                </div>
168
                            </div>
307 geraldo 169
                        </div>
382 geraldo 170
                    ) : (
171
                        <div className="row">
384 geraldo 172
                            <div class="company-title text-center">
173
                                <div class="section_admin_title_buttons">
174
                                    <h1 class="title">{backendVars.LABEL_SUCCESS_SELF_EVALUATION}  </h1>
175
                                </div>
382 geraldo 176
                            </div>
384 geraldo 177
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
178
                                <br />
179
                                <button
180
                                    className="btn btn-sm btn-primary"
181
                                    onClick={() => {
182
                                        setTest(null);
183
                                        setSuccess(false)
184
                                    }}>{backendVars.LBL_GO_BACK}</button>
185
                            </div>
382 geraldo 186
                        </div>
187
                    )}
306 geraldo 188
                </div>
189
            )}
190
        </div>
191
    )
192
}
193
 
194
export default Test;