Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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