Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 478 | Rev 480 | 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) => {
478 geraldo 74
                console.log(question.answer);
479 geraldo 75
                console.log(question.answer.length);
476 geraldo 76
                //Validate if the answer is not empty
479 geraldo 77
                if (question.answer!=""  || question.answer.length != 0) {
478 geraldo 78
                    console.log(1);
476 geraldo 79
                    answer = true;
80
                }
81
            });
336 geraldo 82
        })
476 geraldo 83
        return answer;
306 geraldo 84
    }
85
 
340 geraldo 86
 
361 geraldo 87
    /**
88
     * Cancel test and send to the list of forms
89
     * @returns
365 geraldo 90
     */
441 geraldo 91
    const handleGoBack = () => {
92
        setTest(null);
93
        setSuccess(false)
94
    }
357 geraldo 95
 
395 geraldo 96
 
97
    /**
98
     * componentDidMount
99
     */
100
    useEffect(() => {
399 geraldo 101
        setPage(0);
396 geraldo 102
    }, [action]);
395 geraldo 103
 
306 geraldo 104
    return (
105
        <div>
106
            {loading ? (
107
                <div className="row">
108
                    <Spinner />
109
                </div>
110
            ) : (
382 geraldo 111
                <div>
112
                    {!success ? (
113
                        <div className="row test-section">
384 geraldo 114
 
382 geraldo 115
                            <div className="col-md-12 col-sm-12 col-xs-12">
116
                                <div className="company-title">
117
                                    <div className="section_admin_title_buttons">
118
                                        <h1 className="title">{test.name}</h1>
119
                                    </div>
120
                                </div>
121
                                <div
122
                                    dangerouslySetInnerHTML={{ __html: test.text }}
123
                                    className="description company-title"
124
                                ></div>
307 geraldo 125
                            </div>
382 geraldo 126
                            {test.content.length <= 0 ? (
127
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
128
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
129
                                </div>
130
                            ) : (
131
                                <div className="col-md-12 col-sm-12 col-xs-12">
132
                                    <div className="company-title">
133
                                        {test.content.map((section, key) => {
384 geraldo 134
                                            return (
135
                                                <Section
136
                                                    section={section}
399 geraldo 137
                                                    index={key}
384 geraldo 138
                                                    page={page}
139
                                                    setPage={setPage}
140
                                                    total={test.content.length}
141
                                                    backendVars={backendVars}
142
                                                />)
382 geraldo 143
                                        })}
144
                                    </div>
384 geraldo 145
                                </div>
146
                            )}
469 geraldo 147
 
148
                            {draft &&
470 geraldo 149
                                <div className="col-md-12 col-sm-12 col-xs-12">
150
                                    <div className="company-title">
151
                                        <div className="alert alert-success alert-dismissible fade show" role="alert" >
471 geraldo 152
                                            {backendVars.LBL_TEXT_SAVE_CONTINUE_FORM}
470 geraldo 153
                                            <button
154
                                                type="button"
155
                                                className="close"
156
                                                data-dismiss="alert" aria-label="Close"
157
                                                onClick={() => setDraft(false)}
469 geraldo 158
 
470 geraldo 159
                                            >
160
                                                <span aria-hidden="true">&times;</span>
161
                                            </button>
162
                                        </div>
469 geraldo 163
                                    </div>
164
                                </div>
165
                            }
473 geraldo 166
 
167
                            {error &&
168
 
169
                                <div className="col-md-12 col-sm-12 col-xs-12">
170
                                    <div className="company-title">
171
                                        <div className="alert alert-danger alert-dismissible fade show" role="alert" >
172
                                            {backendVars.LBL_ERROR_ANSWER_FORM}
173
                                            <button
174
                                                type="button"
175
                                                className="close"
176
                                                data-dismiss="alert" aria-label="Close"
177
                                                onClick={() => setError(false)}
178
 
179
                                            >
180
                                                <span aria-hidden="true">&times;</span>
181
                                            </button>
182
                                        </div>
183
                                    </div>
184
                                </div>
185
                            }
186
 
187
 
382 geraldo 188
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
189
                                <div className="company-title">
190
                                    <button
191
                                        type="button"
192
                                        className="btn btn-secondary"
441 geraldo 193
                                        onClick={() => handleGoBack()}>
194
                                        {backendVars.LBL_GO_BACK}
382 geraldo 195
                                    </button>
196
                                    <button
197
                                        type="buttton"
198
                                        className="btn btn-primary"
199
                                        onClick={() => handleSubmit()}>
200
                                        {backendVars.LBL_SAVE}
201
                                    </button>
202
                                </div>
203
                            </div>
307 geraldo 204
                        </div>
382 geraldo 205
                    ) : (
206
                        <div className="row">
469 geraldo 207
                            <div className="company-title text-center">
208
                                <div className="section_admin_title_buttons">
209
                                    <h1 className="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 210
                                </div>
382 geraldo 211
                            </div>
384 geraldo 212
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
213
                                <br />
214
                                <button
215
                                    className="btn btn-sm btn-primary"
441 geraldo 216
                                    onClick={() => handleGoBack()}>
469 geraldo 217
                                    {backendVars.LBL_GO_BACK}
218
                                </button>
384 geraldo 219
                            </div>
382 geraldo 220
                        </div>
221
                    )}
306 geraldo 222
                </div>
223
            )}
224
        </div>
225
    )
226
}
227
 
228
export default Test;