Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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