Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 394 | Rev 396 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
395 geraldo 1
import React, { useState, useEffect } 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);
395 geraldo 13
    const [page, setPage] = useState(0);
360 geraldo 14
 
306 geraldo 15
    // get props
395 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
 
395 geraldo 99
 
100
    /**
101
     * componentDidMount
102
     */
103
    useEffect(() => {
104
 
105
        if (test.content.length <= 0) {
106
            setPage(test.content[0].position);
107
        }
108
 
109
    }, []);
110
 
306 geraldo 111
    return (
112
        <div>
113
            {loading ? (
114
                <div className="row">
115
                    <Spinner />
116
                </div>
117
            ) : (
382 geraldo 118
                <div>
119
                    {!success ? (
120
                        <div className="row test-section">
384 geraldo 121
 
122
                            <ConfirmModal
385 geraldo 123
                                show={confirmationBoxShow}
124
                                title={backendVars.LBL_TITLE_CONFIRM_SELF_EVALUATION}
125
                                message={backendVars.LBL_TEXT_CONFIRM_SELF_EVALUATION}
126
                                acceptLabel={backendVars.LBL_BTN_CONFIRM_SELF_EVALUATION}
395 geraldo 127
                                onClose={handleConfirmationBoxShow}
384 geraldo 128
                                onAccept={handleCancel}
129
                            />
130
 
382 geraldo 131
                            <div className="col-md-12 col-sm-12 col-xs-12">
132
                                <div className="company-title">
133
                                    <div className="section_admin_title_buttons">
134
                                        <h1 className="title">{test.name}</h1>
135
                                    </div>
136
                                </div>
137
                                <div
138
                                    dangerouslySetInnerHTML={{ __html: test.text }}
139
                                    className="description company-title"
140
                                ></div>
307 geraldo 141
                            </div>
382 geraldo 142
                            {test.content.length <= 0 ? (
143
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
144
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
145
                                </div>
146
                            ) : (
147
                                <div className="col-md-12 col-sm-12 col-xs-12">
148
                                    <div className="company-title">
149
                                        {test.content.map((section, key) => {
384 geraldo 150
                                            return (
151
                                                <Section
152
                                                    section={section}
153
                                                    key={key}
154
                                                    page={page}
155
                                                    setPage={setPage}
156
                                                    total={test.content.length}
157
                                                    backendVars={backendVars}
158
                                                    handleAnswer={handleAnswer}
159
                                                />)
382 geraldo 160
                                        })}
161
                                    </div>
384 geraldo 162
                                </div>
163
                            )}
382 geraldo 164
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
165
                                <div className="company-title">
166
                                    <button
167
                                        type="button"
168
                                        className="btn btn-secondary"
169
                                        onClick={handleConfirmationBoxShow}>
170
                                        {backendVars.LBL_CANCEL}
171
                                    </button>
172
                                    <button
173
                                        type="buttton"
174
                                        className="btn btn-primary"
175
                                        disabled={!valid}
176
                                        onClick={() => handleSubmit()}>
177
                                        {backendVars.LBL_SAVE}
178
                                    </button>
179
                                </div>
180
                            </div>
307 geraldo 181
                        </div>
382 geraldo 182
                    ) : (
183
                        <div className="row">
384 geraldo 184
                            <div class="company-title text-center">
185
                                <div class="section_admin_title_buttons">
387 geraldo 186
                                    <h1 class="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 187
                                </div>
382 geraldo 188
                            </div>
384 geraldo 189
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
190
                                <br />
191
                                <button
192
                                    className="btn btn-sm btn-primary"
193
                                    onClick={() => {
194
                                        setTest(null);
195
                                        setSuccess(false)
196
                                    }}>{backendVars.LBL_GO_BACK}</button>
197
                            </div>
382 geraldo 198
                        </div>
199
                    )}
306 geraldo 200
                </div>
201
            )}
202
        </div>
203
    )
204
}
205
 
206
export default Test;