Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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