Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 378 | Rev 380 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
361 geraldo 1
import React, { useState } 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";
361 geraldo 5
import ConfirmationBox from "../../../shared/confirmation-box/ConfirmationBox";
306 geraldo 6
 
7
const Test = (props) => {
8
 
360 geraldo 9
    //init states
361 geraldo 10
    const [confirmationBoxShow, setConfirmationBoxShow] = useState(false);
375 geraldo 11
    const [valid, setValid] = useState(false);
364 geraldo 12
    const [page, setPage] = useState(0);
360 geraldo 13
 
306 geraldo 14
    // get props
344 geraldo 15
    const { backendVars, test, loading, setTest, action } = props;
306 geraldo 16
 
340 geraldo 17
    /**
18
     * Send form data
19
     */
20
    const handleSubmit = async () => {
21
        if (validateForm()) {
356 geraldo 22
            //init form data
355 geraldo 23
            const formData = new FormData();
24
            formData.append("content", JSON.stringify(test));
360 geraldo 25
            await axios.post(action, formData).then((response) => {
340 geraldo 26
                if (response.data.success) {
27
                    setTest(null);
336 geraldo 28
                }
340 geraldo 29
            });
30
        }
31
    }
32
    /**
33
     * Update question answer
34
     * @param {*} slug_section
35
     * @param {*} slug_question
36
     * @param {*} answer
37
     */
38
    const handleAnswer = (slug_section, slug_question, answer) => {
39
        test.content.filter((section) => {
40
            if (section.slug_section == slug_section) {
41
                section.questions.map((question) => {
42
                    if (question.slug_question == slug_question) {
356 geraldo 43
                        //valid if the question has more than one answer
350 geraldo 44
                        question.type == 'multiple' ?
45
                            !question.answer ?
46
                                question.answer = [answer] :
379 geraldo 47
                                question.answer.includes(answer) ?
48
                                    question.answer = removeOptionMultiple(question.answer, answer) :
49
                                    question.answer.push(answer)
350 geraldo 50
                            : question.answer = answer;
345 geraldo 51
 
340 geraldo 52
                    }
53
                })
54
            }
343 geraldo 55
        });
378 geraldo 56
        validateForm();
340 geraldo 57
        setTest(test);
58
    }
378 geraldo 59
 
379 geraldo 60
    const removeOptionMultiple = (arr, item) => {
61
        var i = arr.indexOf(item);
62
        return arr.splice(i, 1);
378 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
340 geraldo 73
                if (!question.answer) {
74
                    formValid = false;
75
                }
336 geraldo 76
            })
77
        })
375 geraldo 78
        setValid(formValid);
340 geraldo 79
        return formValid;
306 geraldo 80
    }
81
 
361 geraldo 82
    /**
83
     * Update component status
84
     * @returns
85
     */
86
    const handleConfirmationBoxShow = () => setConfirmationBoxShow(!confirmationBoxShow);
340 geraldo 87
 
361 geraldo 88
    /**
89
     * Cancel test and send to the list of forms
90
     * @returns
365 geraldo 91
     */
361 geraldo 92
    const handleCancel = () => setTest('');
357 geraldo 93
 
306 geraldo 94
    return (
95
        <div>
96
            {loading ? (
97
                <div className="row">
98
                    <Spinner />
99
                </div>
100
            ) : (
311 geraldo 101
                <div className="row test-section">
340 geraldo 102
                    <div className="col-md-12 col-sm-12 col-xs-12">
103
                        <div className="company-title">
104
                            <div className="section_admin_title_buttons">
105
                                <h1 className="title">{test.name}</h1>
307 geraldo 106
                            </div>
306 geraldo 107
                        </div>
340 geraldo 108
                        <div
109
                            dangerouslySetInnerHTML={{ __html: test.text }}
110
                            className="description company-title"
111
                        ></div>
112
                    </div>
365 geraldo 113
                    {test.content.length <= 0 ? (
114
                        <div className="col-md-12 col-sm-12 col-xs-12 text-center">
115
                            {backendVars.LBL_DATATABLE_SZERORECORDS}
306 geraldo 116
                        </div>
365 geraldo 117
                    ) : (
118
                        <div className="col-md-12 col-sm-12 col-xs-12">
119
                            <div className="company-title">
120
                                {test.content.map((section, key) => {
121
                                    return <Section
122
                                        section={section}
123
                                        key={key}
124
                                        page={page}
125
                                        setPage={setPage}
126
                                        total={test.content.length}
127
                                        backendVars={backendVars}
128
                                        handleAnswer={handleAnswer}
129
                                    />
130
                                })}
131
                            </div>
132
                        </div>)}
360 geraldo 133
                    <div className="col-md-12 col-sm-12 col-xs-12 text-right">
340 geraldo 134
                        <div className="company-title">
361 geraldo 135
                            <button
136
                                type="button"
378 geraldo 137
                                className="btn btn-secondary"
361 geraldo 138
                                onClick={handleConfirmationBoxShow}>
360 geraldo 139
                                {backendVars.LBL_CANCEL}
140
                            </button>
361 geraldo 141
 
142
                            <ConfirmationBox
143
                                show={confirmationBoxShow}
144
                                onClose={handleConfirmationBoxShow}
145
                                onAccept={handleCancel}
146
                            />
147
 
148
 
149
                            <button
150
                                type="buttton"
378 geraldo 151
                                className="btn btn-primary"
379 geraldo 152
                                disabled={!valid}
361 geraldo 153
                                onClick={() => handleSubmit()}>
360 geraldo 154
                                {backendVars.LBL_SAVE}
155
                            </button>
307 geraldo 156
                        </div>
340 geraldo 157
                    </div>
306 geraldo 158
                </div>
159
            )}
361 geraldo 160
 
161
 
306 geraldo 162
        </div>
163
    )
164
}
165
 
166
export default Test;