Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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