Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 479 | 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));
476 geraldo 31
        formData.append("status", formCompleted() ? backendVars.STATUS_PENDING : backendVars.STATUS_DRAFT);
469 geraldo 32
 
473 geraldo 33
        // check if the form has at least one response
478 geraldo 34
        if (leastOneAnswer()) {
473 geraldo 35
            await axios.post(action, formData).then((response) =>
476 geraldo 36
                response.data.success && formCompleted() ?
473 geraldo 37
                    setSuccess(true) :
38
                    setDraft(true)
39
 
40
            )
478 geraldo 41
        } else {
42
            setError(true);
43
        }
473 geraldo 44
 
478 geraldo 45
 
340 geraldo 46
    }
345 geraldo 47
 
382 geraldo 48
    /**
340 geraldo 49
     * Check if there are questions to answer
50
     * @returns
51
     */
476 geraldo 52
    const formCompleted = () => {
53
        let completed = true;
340 geraldo 54
        test.content.map((section) => {
55
            section.questions.map((question) => {
356 geraldo 56
                //Validate if the answer is empty
477 geraldo 57
                if (!question.answer || question.answer.length == 0) {
476 geraldo 58
                    completed = false;
59
                }
60
            });
61
        })
62
        return completed;
63
    }
473 geraldo 64
 
476 geraldo 65
 
66
    /**
67
     * Check if there is at least one answer
68
     * @returns
69
     */
478 geraldo 70
    const leastOneAnswer = () => {
476 geraldo 71
        let answer = false;
72
        test.content.map((section) => {
73
            section.questions.map((question) => {
74
                //Validate if the answer is not empty
479 geraldo 75
                if (question.answer!=""  || question.answer.length != 0) {
476 geraldo 76
                    answer = true;
77
                }
78
            });
336 geraldo 79
        })
476 geraldo 80
        return answer;
306 geraldo 81
    }
82
 
340 geraldo 83
 
361 geraldo 84
    /**
85
     * Cancel test and send to the list of forms
86
     * @returns
365 geraldo 87
     */
441 geraldo 88
    const handleGoBack = () => {
89
        setTest(null);
90
        setSuccess(false)
91
    }
357 geraldo 92
 
395 geraldo 93
 
94
    /**
95
     * componentDidMount
96
     */
97
    useEffect(() => {
399 geraldo 98
        setPage(0);
396 geraldo 99
    }, [action]);
395 geraldo 100
 
306 geraldo 101
    return (
102
        <div>
103
            {loading ? (
104
                <div className="row">
105
                    <Spinner />
106
                </div>
107
            ) : (
382 geraldo 108
                <div>
109
                    {!success ? (
110
                        <div className="row test-section">
384 geraldo 111
 
382 geraldo 112
                            <div className="col-md-12 col-sm-12 col-xs-12">
113
                                <div className="company-title">
114
                                    <div className="section_admin_title_buttons">
115
                                        <h1 className="title">{test.name}</h1>
116
                                    </div>
117
                                </div>
118
                                <div
119
                                    dangerouslySetInnerHTML={{ __html: test.text }}
120
                                    className="description company-title"
121
                                ></div>
307 geraldo 122
                            </div>
382 geraldo 123
                            {test.content.length <= 0 ? (
124
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
125
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
126
                                </div>
127
                            ) : (
128
                                <div className="col-md-12 col-sm-12 col-xs-12">
129
                                    <div className="company-title">
130
                                        {test.content.map((section, key) => {
384 geraldo 131
                                            return (
132
                                                <Section
133
                                                    section={section}
399 geraldo 134
                                                    index={key}
384 geraldo 135
                                                    page={page}
136
                                                    setPage={setPage}
137
                                                    total={test.content.length}
138
                                                    backendVars={backendVars}
139
                                                />)
382 geraldo 140
                                        })}
141
                                    </div>
384 geraldo 142
                                </div>
143
                            )}
469 geraldo 144
 
145
                            {draft &&
470 geraldo 146
                                <div className="col-md-12 col-sm-12 col-xs-12">
147
                                    <div className="company-title">
148
                                        <div className="alert alert-success alert-dismissible fade show" role="alert" >
471 geraldo 149
                                            {backendVars.LBL_TEXT_SAVE_CONTINUE_FORM}
470 geraldo 150
                                            <button
151
                                                type="button"
152
                                                className="close"
153
                                                data-dismiss="alert" aria-label="Close"
154
                                                onClick={() => setDraft(false)}
469 geraldo 155
 
470 geraldo 156
                                            >
157
                                                <span aria-hidden="true">&times;</span>
158
                                            </button>
159
                                        </div>
469 geraldo 160
                                    </div>
161
                                </div>
162
                            }
473 geraldo 163
 
164
                            {error &&
165
 
166
                                <div className="col-md-12 col-sm-12 col-xs-12">
167
                                    <div className="company-title">
168
                                        <div className="alert alert-danger alert-dismissible fade show" role="alert" >
169
                                            {backendVars.LBL_ERROR_ANSWER_FORM}
170
                                            <button
171
                                                type="button"
172
                                                className="close"
173
                                                data-dismiss="alert" aria-label="Close"
174
                                                onClick={() => setError(false)}
175
 
176
                                            >
177
                                                <span aria-hidden="true">&times;</span>
178
                                            </button>
179
                                        </div>
180
                                    </div>
181
                                </div>
182
                            }
183
 
184
 
382 geraldo 185
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
186
                                <div className="company-title">
187
                                    <button
188
                                        type="button"
189
                                        className="btn btn-secondary"
441 geraldo 190
                                        onClick={() => handleGoBack()}>
191
                                        {backendVars.LBL_GO_BACK}
382 geraldo 192
                                    </button>
193
                                    <button
194
                                        type="buttton"
195
                                        className="btn btn-primary"
196
                                        onClick={() => handleSubmit()}>
197
                                        {backendVars.LBL_SAVE}
198
                                    </button>
199
                                </div>
200
                            </div>
307 geraldo 201
                        </div>
382 geraldo 202
                    ) : (
203
                        <div className="row">
469 geraldo 204
                            <div className="company-title text-center">
205
                                <div className="section_admin_title_buttons">
206
                                    <h1 className="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 207
                                </div>
382 geraldo 208
                            </div>
384 geraldo 209
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
210
                                <br />
211
                                <button
212
                                    className="btn btn-sm btn-primary"
441 geraldo 213
                                    onClick={() => handleGoBack()}>
469 geraldo 214
                                    {backendVars.LBL_GO_BACK}
215
                                </button>
384 geraldo 216
                            </div>
382 geraldo 217
                        </div>
218
                    )}
306 geraldo 219
                </div>
220
            )}
221
        </div>
222
    )
223
}
224
 
225
export default Test;