Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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