Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 482 | 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";
5
 
6
const Test = (props) => {
7
 
360 geraldo 8
    //init states
382 geraldo 9
    const [success, setSuccess] = useState(false);
473 geraldo 10
    const [error, setError] = useState(false);
469 geraldo 11
    const [draft, setDraft] = 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 () => {
473 geraldo 21
 
22
 
23
        //set states
469 geraldo 24
        setDraft(false);
25
        setSuccess(false);
473 geraldo 26
        setError(false);
27
 
28
        // set form data
431 geraldo 29
        const formData = new FormData();
441 geraldo 30
        formData.append("content", JSON.stringify(test.content));
482 geraldo 31
        formData.append("status",
32
            formCompleted() ?
33
                formPending() ?
34
                    backendVars.STATUS_PENDING :
35
                    backendVars.STATUS_COMPLETED :
36
                backendVars.STATUS_DRAFT);
469 geraldo 37
 
473 geraldo 38
        // check if the form has at least one response
478 geraldo 39
        if (leastOneAnswer()) {
473 geraldo 40
            await axios.post(action, formData).then((response) =>
476 geraldo 41
                response.data.success && formCompleted() ?
473 geraldo 42
                    setSuccess(true) :
43
                    setDraft(true)
44
 
45
            )
478 geraldo 46
        } else {
47
            setError(true);
48
        }
473 geraldo 49
 
478 geraldo 50
 
340 geraldo 51
    }
345 geraldo 52
 
382 geraldo 53
    /**
340 geraldo 54
     * Check if there are questions to answer
55
     * @returns
56
     */
476 geraldo 57
    const formCompleted = () => {
58
        let completed = true;
340 geraldo 59
        test.content.map((section) => {
60
            section.questions.map((question) => {
356 geraldo 61
                //Validate if the answer is empty
477 geraldo 62
                if (!question.answer || question.answer.length == 0) {
476 geraldo 63
                    completed = false;
64
                }
65
            });
66
        })
67
        return completed;
68
    }
473 geraldo 69
 
476 geraldo 70
 
71
    /**
72
     * Check if there is at least one answer
73
     * @returns
74
     */
478 geraldo 75
    const leastOneAnswer = () => {
476 geraldo 76
        let answer = false;
77
        test.content.map((section) => {
78
            section.questions.map((question) => {
79
                //Validate if the answer is not empty
482 geraldo 80
                if (question.answer != "" || question.answer.length != 0) {
476 geraldo 81
                    answer = true;
82
                }
83
            });
336 geraldo 84
        })
476 geraldo 85
        return answer;
306 geraldo 86
    }
87
 
482 geraldo 88
    /**
89
     * Validate if there are "Open" type questions
90
     * @returns
91
     */
92
    const formPending = () => {
93
        let pending = false;
94
        test.content.map((section) => {
95
            section.questions.map((question) => {
96
                if (question.type == "open" || question.type == "rating-open"  || question.type == "rating-range" ) {
97
                    pending = true;
98
                }
99
            });
100
        })
101
        return pending;
102
    }
340 geraldo 103
 
361 geraldo 104
    /**
105
     * Cancel test and send to the list of forms
106
     * @returns
365 geraldo 107
     */
441 geraldo 108
    const handleGoBack = () => {
109
        setTest(null);
110
        setSuccess(false)
111
    }
357 geraldo 112
 
395 geraldo 113
 
114
    /**
115
     * componentDidMount
116
     */
117
    useEffect(() => {
399 geraldo 118
        setPage(0);
396 geraldo 119
    }, [action]);
395 geraldo 120
 
306 geraldo 121
    return (
122
        <div>
123
            {loading ? (
124
                <div className="row">
125
                    <Spinner />
126
                </div>
127
            ) : (
382 geraldo 128
                <div>
129
                    {!success ? (
130
                        <div className="row test-section">
384 geraldo 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}
399 geraldo 154
                                                    index={key}
384 geraldo 155
                                                    page={page}
156
                                                    setPage={setPage}
157
                                                    total={test.content.length}
158
                                                    backendVars={backendVars}
159
                                                />)
382 geraldo 160
                                        })}
161
                                    </div>
384 geraldo 162
                                </div>
163
                            )}
469 geraldo 164
 
165
                            {draft &&
470 geraldo 166
                                <div className="col-md-12 col-sm-12 col-xs-12">
167
                                    <div className="company-title">
168
                                        <div className="alert alert-success alert-dismissible fade show" role="alert" >
471 geraldo 169
                                            {backendVars.LBL_TEXT_SAVE_CONTINUE_FORM}
470 geraldo 170
                                            <button
171
                                                type="button"
172
                                                className="close"
173
                                                data-dismiss="alert" aria-label="Close"
174
                                                onClick={() => setDraft(false)}
469 geraldo 175
 
470 geraldo 176
                                            >
177
                                                <span aria-hidden="true">&times;</span>
178
                                            </button>
179
                                        </div>
469 geraldo 180
                                    </div>
181
                                </div>
182
                            }
473 geraldo 183
 
184
                            {error &&
185
 
186
                                <div className="col-md-12 col-sm-12 col-xs-12">
187
                                    <div className="company-title">
188
                                        <div className="alert alert-danger alert-dismissible fade show" role="alert" >
189
                                            {backendVars.LBL_ERROR_ANSWER_FORM}
190
                                            <button
191
                                                type="button"
192
                                                className="close"
193
                                                data-dismiss="alert" aria-label="Close"
194
                                                onClick={() => setError(false)}
195
 
196
                                            >
197
                                                <span aria-hidden="true">&times;</span>
198
                                            </button>
199
                                        </div>
200
                                    </div>
201
                                </div>
202
                            }
203
 
204
 
382 geraldo 205
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
206
                                <div className="company-title">
207
                                    <button
208
                                        type="button"
209
                                        className="btn btn-secondary"
441 geraldo 210
                                        onClick={() => handleGoBack()}>
211
                                        {backendVars.LBL_GO_BACK}
382 geraldo 212
                                    </button>
213
                                    <button
214
                                        type="buttton"
215
                                        className="btn btn-primary"
216
                                        onClick={() => handleSubmit()}>
217
                                        {backendVars.LBL_SAVE}
218
                                    </button>
219
                                </div>
220
                            </div>
307 geraldo 221
                        </div>
382 geraldo 222
                    ) : (
223
                        <div className="row">
469 geraldo 224
                            <div className="company-title text-center">
225
                                <div className="section_admin_title_buttons">
561 geraldo 226
                                    <h1 className="title">{backendVars.LBL_SUCCESS_EVALUATION}</h1>
384 geraldo 227
                                </div>
382 geraldo 228
                            </div>
384 geraldo 229
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
230
                                <br />
231
                                <button
232
                                    className="btn btn-sm btn-primary"
441 geraldo 233
                                    onClick={() => handleGoBack()}>
469 geraldo 234
                                    {backendVars.LBL_GO_BACK}
235
                                </button>
384 geraldo 236
                            </div>
382 geraldo 237
                        </div>
238
                    )}
306 geraldo 239
                </div>
240
            )}
241
        </div>
242
    )
243
}
244
 
245
export default Test;