Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
561 geraldo 1
import React, { useState, useEffect } from "react";
2
import { axios } from '../../../utils';
3
import Section from "./section/Section";
4
import Spinner from "../../../shared/loading-spinner/Spinner";
625 geraldo 5
import ConfirmModal from "../../../shared/confirm-modal/ConfirmModal";
561 geraldo 6
 
7
const Test = (props) => {
8
 
9
    //init states
626 geraldo 10
    const [confirmModalShow, setConfirmModalShow] = useState(false);
561 geraldo 11
    const [success, setSuccess] = useState(false);
12
    const [error, setError] = useState(false);
13
    const [draft, setDraft] = useState(false);
14
    const [page, setPage] = useState(0);
15
 
16
    // get props
17
    const { backendVars, test, loading, setTest, action } = props;
18
 
19
    /**
590 geraldo 20
 * Send form data
21
 */
561 geraldo 22
    const handleSubmit = async () => {
23
        //set states
24
        setDraft(false);
25
        setSuccess(false);
26
        setError(false);
625 geraldo 27
        // check if the form has at least one response
28
        if (leastOneAnswer()) {
626 geraldo 29
            formCompleted() ?
30
                setConfirmModalShow(true) :
31
                sendData();
625 geraldo 32
        } else {
33
            setError(true);
34
        }
35
    }
36
 
626 geraldo 37
 
625 geraldo 38
    const sendData = async () => {
561 geraldo 39
        // set form data
40
        const formData = new FormData();
41
        formData.append("content", JSON.stringify(test.content));
42
        formData.append("status",
43
            formCompleted() ?
590 geraldo 44
                backendVars.STATUS_PENDING :
561 geraldo 45
                backendVars.STATUS_DRAFT);
625 geraldo 46
        await axios.post(action, formData).then((response) => {
47
            if (response.data.success) {
48
                formCompleted() ?
49
                    setSuccess(true) :
50
                    setDraft(true)
51
            }
52
        })
561 geraldo 53
    }
625 geraldo 54
 
55
 
56
 
57
 
561 geraldo 58
    /**
590 geraldo 59
     * Check if there are options to answer
561 geraldo 60
     * @returns
61
     */
62
    const formCompleted = () => {
63
        let completed = true;
64
        test.content.map((section) => {
590 geraldo 65
            if (section.type == 'multiple') {
66
                section.options.map((option) => {
67
                    //Validate if the answer is empty
68
                    if (!option.answer || option.answer.length == 0) {
69
                        completed = false;
70
                    }
71
                });
72
            } else {
73
                if (!section.answer || section.answer.length == 0) {
561 geraldo 74
                    completed = false;
75
                }
590 geraldo 76
            }
561 geraldo 77
        })
78
        return completed;
79
    }
80
    /**
81
     * Check if there is at least one answer
82
     * @returns
83
     */
84
    const leastOneAnswer = () => {
85
        let answer = false;
86
        test.content.map((section) => {
590 geraldo 87
            if (section.type == 'multiple') {
88
                section.options.map((option) => {
89
                    //Validate if the answer is not empty
90
                    if (option.answer != "" || option.answer.length != 0) {
91
                        answer = true;
92
                    }
93
                });
94
            } else {
95
                if (section.answer || section.answer.length != 0) {
561 geraldo 96
                    answer = true;
97
                }
590 geraldo 98
            }
561 geraldo 99
        })
100
        return answer;
101
    }
102
    /**
103
     * Cancel test and send to the list of forms
104
     * @returns
105
     */
106
    const handleGoBack = () => {
107
        setTest(null);
108
        setSuccess(false)
109
    }
110
    /**
111
     * componentDidMount
112
     */
113
    useEffect(() => {
114
        setPage(0);
115
    }, [action]);
116
 
625 geraldo 117
    /**
118
     * Click cancel modal
119
     * @returns
120
     */
121
    const handleConfirmModalShow = () => setConfirmModalShow(!confirmModalShow);
122
 
123
 
124
    /**
125
     * Click accept modal
126
     * @returns
127
     */
626 geraldo 128
    const handleConfirmModalAccept = async () => sendData();
625 geraldo 129
 
130
 
626 geraldo 131
 
561 geraldo 132
    return (
133
        <div>
134
            {loading ? (
135
                <div className="row">
136
                    <Spinner />
137
                </div>
138
            ) : (
139
                <div>
140
 
627 geraldo 141
<ConfirmModal
625 geraldo 142
                                show={confirmModalShow}
143
                                onClose={handleConfirmModalShow}
144
                                onAccept={handleConfirmModalAccept}
145
                            />
146
 
627 geraldo 147
                    {!success ? (
148
                        <div className="row test-section">
149
 
150
 
561 geraldo 151
                            <div className="col-md-12 col-sm-12 col-xs-12">
152
                                <div className="company-title">
153
                                    <div className="section_admin_title_buttons">
154
                                        <h1 className="title">{test.name}</h1>
155
                                    </div>
156
                                </div>
157
                                <div
158
                                    dangerouslySetInnerHTML={{ __html: test.text }}
159
                                    className="description company-title"
160
                                ></div>
161
                            </div>
162
                            {test.content.length <= 0 ? (
163
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
164
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
165
                                </div>
166
                            ) : (
167
                                <div className="col-md-12 col-sm-12 col-xs-12">
168
                                    <div className="company-title">
169
                                        {test.content.map((section, key) => {
170
                                            return (
171
                                                <Section
172
                                                    section={section}
173
                                                    index={key}
174
                                                    page={page}
175
                                                    setPage={setPage}
176
                                                    total={test.content.length}
177
                                                    backendVars={backendVars}
178
                                                />)
179
                                        })}
627 geraldo 180
 
181
 
561 geraldo 182
                                    </div>
183
                                </div>
184
                            )}
185
 
186
                            {draft &&
187
                                <div className="col-md-12 col-sm-12 col-xs-12">
188
                                    <div className="company-title">
189
                                        <div className="alert alert-success alert-dismissible fade show" role="alert" >
190
                                            {backendVars.LBL_TEXT_SAVE_CONTINUE_FORM}
191
                                            <button
192
                                                type="button"
193
                                                className="close"
194
                                                data-dismiss="alert" aria-label="Close"
195
                                                onClick={() => setDraft(false)}
196
 
197
                                            >
198
                                                <span aria-hidden="true">&times;</span>
199
                                            </button>
200
                                        </div>
201
                                    </div>
202
                                </div>
203
                            }
204
 
205
                            {error &&
206
 
207
                                <div className="col-md-12 col-sm-12 col-xs-12">
208
                                    <div className="company-title">
209
                                        <div className="alert alert-danger alert-dismissible fade show" role="alert" >
210
                                            {backendVars.LBL_ERROR_ANSWER_FORM}
211
                                            <button
212
                                                type="button"
213
                                                className="close"
214
                                                data-dismiss="alert" aria-label="Close"
215
                                                onClick={() => setError(false)}
216
 
217
                                            >
218
                                                <span aria-hidden="true">&times;</span>
219
                                            </button>
220
                                        </div>
221
                                    </div>
222
                                </div>
223
                            }
224
 
225
 
226
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
227
                                <div className="company-title">
228
                                    <button
229
                                        type="button"
230
                                        className="btn btn-secondary"
231
                                        onClick={() => handleGoBack()}>
232
                                        {backendVars.LBL_GO_BACK}
233
                                    </button>
234
                                    <button
235
                                        type="buttton"
236
                                        className="btn btn-primary"
237
                                        onClick={() => handleSubmit()}>
238
                                        {backendVars.LBL_SAVE}
239
                                    </button>
240
                                </div>
241
                            </div>
242
                        </div>
243
                    ) : (
244
                        <div className="row">
245
                            <div className="company-title text-center">
246
                                <div className="section_admin_title_buttons">
627 geraldo 247
                                    <h1 className="title">{backendVars.LBL_SUCCESS_EVALUATION}</h1>
561 geraldo 248
                                </div>
249
                            </div>
250
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
251
                                <br />
252
                                <button
253
                                    className="btn btn-sm btn-primary"
254
                                    onClick={() => handleGoBack()}>
255
                                    {backendVars.LBL_GO_BACK}
256
                                </button>
257
                            </div>
258
                        </div>
259
                    )}
260
                </div>
261
            )}
262
        </div>
263
    )
264
}
265
 
266
export default Test;