Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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