Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 444 | Rev 446 | 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);
395 geraldo 10
    const [page, setPage] = useState(0);
360 geraldo 11
 
306 geraldo 12
    // get props
395 geraldo 13
    const { backendVars, test, loading, setTest, action } = props;
306 geraldo 14
 
340 geraldo 15
    /**
16
     * Send form data
17
     */
18
    const handleSubmit = async () => {
431 geraldo 19
        //init form data
20
        const formData = new FormData();
441 geraldo 21
        formData.append("content", JSON.stringify(test.content));
444 geraldo 22
        formData.append("status", status);
431 geraldo 23
        await axios.post(action, formData).then((response) => {
24
            if (response.data.success && validateForm()) {
25
                setSuccess(true);
26
            }
27
        });
340 geraldo 28
    }
29
    /**
30
     * Update question answer
31
     * @param {*} slug_section
32
     * @param {*} slug_question
33
     * @param {*} answer
34
     */
35
    const handleAnswer = (slug_section, slug_question, answer) => {
36
        test.content.filter((section) => {
37
            if (section.slug_section == slug_section) {
38
                section.questions.map((question) => {
39
                    if (question.slug_question == slug_question) {
356 geraldo 40
                        //valid if the question has more than one answer
350 geraldo 41
                        question.type == 'multiple' ?
42
                            !question.answer ?
43
                                question.answer = [answer] :
379 geraldo 44
                                question.answer.includes(answer) ?
45
                                    question.answer = removeOptionMultiple(question.answer, answer) :
46
                                    question.answer.push(answer)
350 geraldo 47
                            : question.answer = answer;
345 geraldo 48
 
340 geraldo 49
                    }
50
                })
51
            }
343 geraldo 52
        });
340 geraldo 53
        setTest(test);
54
    }
378 geraldo 55
 
382 geraldo 56
    /**
57
     * Delete existing option
58
     * @param {*} arr
59
     * @param {*} item
60
     * @returns
61
     */
62
    const removeOptionMultiple = (arr, item) => arr.splice(arr.indexOf(item), 1);
380 geraldo 63
 
340 geraldo 64
    /**
65
     * Check if there are questions to answer
66
     * @returns
67
     */
68
    const validateForm = () => {
69
        let formValid = true;
70
        test.content.map((section) => {
71
            section.questions.map((question) => {
356 geraldo 72
                //Validate if the answer is empty
382 geraldo 73
                if (!question.answer || question.answer.length == 0) {
340 geraldo 74
                    formValid = false;
75
                }
336 geraldo 76
            })
77
        })
340 geraldo 78
        return formValid;
306 geraldo 79
    }
80
 
340 geraldo 81
 
361 geraldo 82
    /**
83
     * Cancel test and send to the list of forms
84
     * @returns
365 geraldo 85
     */
441 geraldo 86
    const handleGoBack = () => {
87
        setTest(null);
88
        setSuccess(false)
89
    }
357 geraldo 90
 
395 geraldo 91
 
92
    /**
93
     * componentDidMount
94
     */
95
    useEffect(() => {
399 geraldo 96
        setPage(0);
396 geraldo 97
    }, [action]);
395 geraldo 98
 
306 geraldo 99
    return (
100
        <div>
101
            {loading ? (
102
                <div className="row">
103
                    <Spinner />
104
                </div>
105
            ) : (
382 geraldo 106
                <div>
107
                    {!success ? (
108
                        <div className="row test-section">
384 geraldo 109
 
382 geraldo 110
                            <div className="col-md-12 col-sm-12 col-xs-12">
111
                                <div className="company-title">
112
                                    <div className="section_admin_title_buttons">
113
                                        <h1 className="title">{test.name}</h1>
114
                                    </div>
115
                                </div>
116
                                <div
117
                                    dangerouslySetInnerHTML={{ __html: test.text }}
118
                                    className="description company-title"
119
                                ></div>
307 geraldo 120
                            </div>
382 geraldo 121
                            {test.content.length <= 0 ? (
122
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
123
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
124
                                </div>
125
                            ) : (
126
                                <div className="col-md-12 col-sm-12 col-xs-12">
127
                                    <div className="company-title">
128
                                        {test.content.map((section, key) => {
384 geraldo 129
                                            return (
130
                                                <Section
131
                                                    section={section}
399 geraldo 132
                                                    index={key}
384 geraldo 133
                                                    page={page}
134
                                                    setPage={setPage}
135
                                                    total={test.content.length}
136
                                                    backendVars={backendVars}
137
                                                    handleAnswer={handleAnswer}
138
                                                />)
382 geraldo 139
                                        })}
140
                                    </div>
384 geraldo 141
                                </div>
142
                            )}
382 geraldo 143
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
144
                                <div className="company-title">
145
                                    <button
146
                                        type="button"
147
                                        className="btn btn-secondary"
441 geraldo 148
                                        onClick={() => handleGoBack()}>
149
                                        {backendVars.LBL_GO_BACK}
382 geraldo 150
                                    </button>
151
                                    <button
152
                                        type="buttton"
153
                                        className="btn btn-primary"
154
                                        onClick={() => handleSubmit()}>
155
                                        {backendVars.LBL_SAVE}
156
                                    </button>
157
                                </div>
158
                            </div>
307 geraldo 159
                        </div>
382 geraldo 160
                    ) : (
161
                        <div className="row">
384 geraldo 162
                            <div class="company-title text-center">
163
                                <div class="section_admin_title_buttons">
387 geraldo 164
                                    <h1 class="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 165
                                </div>
382 geraldo 166
                            </div>
384 geraldo 167
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
168
                                <br />
169
                                <button
170
                                    className="btn btn-sm btn-primary"
441 geraldo 171
                                    onClick={() => handleGoBack()}>
172
                                        {backendVars.LBL_GO_BACK}
173
                                    </button>
384 geraldo 174
                            </div>
382 geraldo 175
                        </div>
176
                    )}
306 geraldo 177
                </div>
178
            )}
179
        </div>
180
    )
181
}
182
 
183
export default Test;