Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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