Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 590 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
561 geraldo 1
import React, { useState, useEffect } from "react";
2
import { axios } from '../../../utils';
3
import Section from "./section/Section";
4
import Spinner from "../../../shared/loading-spinner/Spinner";
5
 
6
const Test = (props) => {
7
 
8
    //init states
9
    const [success, setSuccess] = useState(false);
10
    const [error, setError] = useState(false);
11
    const [draft, setDraft] = useState(false);
12
    const [page, setPage] = useState(0);
13
 
14
    // get props
15
    const { backendVars, test, loading, setTest, action } = props;
16
 
17
    /**
18
     * Send form data
19
     */
20
    const handleSubmit = async () => {
21
 
22
 
23
        //set states
24
        setDraft(false);
25
        setSuccess(false);
26
        setError(false);
27
 
28
        // set form data
29
        const formData = new FormData();
30
        formData.append("content", JSON.stringify(test.content));
31
        formData.append("status",
32
            formCompleted() ?
33
                formPending() ?
34
                    backendVars.STATUS_PENDING :
35
                    backendVars.STATUS_COMPLETED :
36
                backendVars.STATUS_DRAFT);
37
 
38
        // check if the form has at least one response
39
        if (leastOneAnswer()) {
40
            await axios.post(action, formData).then((response) =>
41
                response.data.success && formCompleted() ?
42
                    setSuccess(true) :
43
                    setDraft(true)
44
 
45
            )
46
        } else {
47
            setError(true);
48
        }
49
 
50
 
51
    }
52
 
53
    /**
54
     * Check if there are questions to answer
55
     * @returns
56
     */
57
    const formCompleted = () => {
58
        let completed = true;
59
        test.content.map((section) => {
60
            section.questions.map((question) => {
61
                //Validate if the answer is empty
62
                if (!question.answer || question.answer.length == 0) {
63
                    completed = false;
64
                }
65
            });
66
        })
67
        return completed;
68
    }
69
 
70
 
71
    /**
72
     * Check if there is at least one answer
73
     * @returns
74
     */
75
    const leastOneAnswer = () => {
76
        let answer = false;
77
        test.content.map((section) => {
78
            section.questions.map((question) => {
79
                //Validate if the answer is not empty
80
                if (question.answer != "" || question.answer.length != 0) {
81
                    answer = true;
82
                }
83
            });
84
        })
85
        return answer;
86
    }
87
 
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
    }
103
 
104
    /**
105
     * Cancel test and send to the list of forms
106
     * @returns
107
     */
108
    const handleGoBack = () => {
109
        setTest(null);
110
        setSuccess(false)
111
    }
112
 
113
 
114
    /**
115
     * componentDidMount
116
     */
117
    useEffect(() => {
118
        setPage(0);
119
    }, [action]);
120
 
121
    return (
122
        <div>
123
            {loading ? (
124
                <div className="row">
125
                    <Spinner />
126
                </div>
127
            ) : (
128
                <div>
129
                    {!success ? (
130
                        <div className="row test-section">
131
 
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>
142
                            </div>
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) => {
151
                                            return (
152
                                                <Section
153
                                                    section={section}
154
                                                    index={key}
155
                                                    page={page}
156
                                                    setPage={setPage}
157
                                                    total={test.content.length}
158
                                                    backendVars={backendVars}
159
                                                />)
160
                                        })}
161
                                    </div>
162
                                </div>
163
                            )}
164
 
165
                            {draft &&
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" >
169
                                            {backendVars.LBL_TEXT_SAVE_CONTINUE_FORM}
170
                                            <button
171
                                                type="button"
172
                                                className="close"
173
                                                data-dismiss="alert" aria-label="Close"
174
                                                onClick={() => setDraft(false)}
175
 
176
                                            >
177
                                                <span aria-hidden="true">&times;</span>
178
                                            </button>
179
                                        </div>
180
                                    </div>
181
                                </div>
182
                            }
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
 
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"
210
                                        onClick={() => handleGoBack()}>
211
                                        {backendVars.LBL_GO_BACK}
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>
221
                        </div>
222
                    ) : (
223
                        <div className="row">
224
                            <div className="company-title text-center">
225
                                <div className="section_admin_title_buttons">
226
                                    <h1 className="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
227
                                </div>
228
                            </div>
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"
233
                                    onClick={() => handleGoBack()}>
234
                                    {backendVars.LBL_GO_BACK}
235
                                </button>
236
                            </div>
237
                        </div>
238
                    )}
239
                </div>
240
            )}
241
        </div>
242
    )
243
}
244
 
245
export default Test;