Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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