Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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