Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 449 | Rev 469 | 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 );
449 geraldo 24
        console.log(test.content);
431 geraldo 25
        await axios.post(action, formData).then((response) => {
26
            if (response.data.success && validateForm()) {
27
                setSuccess(true);
28
            }
29
        });
340 geraldo 30
    }
345 geraldo 31
 
382 geraldo 32
    /**
340 geraldo 33
     * Check if there are questions to answer
34
     * @returns
35
     */
36
    const validateForm = () => {
37
        let formValid = true;
38
        test.content.map((section) => {
39
            section.questions.map((question) => {
356 geraldo 40
                //Validate if the answer is empty
382 geraldo 41
                if (!question.answer || question.answer.length == 0) {
340 geraldo 42
                    formValid = false;
43
                }
336 geraldo 44
            })
45
        })
340 geraldo 46
        return formValid;
306 geraldo 47
    }
48
 
340 geraldo 49
 
361 geraldo 50
    /**
51
     * Cancel test and send to the list of forms
52
     * @returns
365 geraldo 53
     */
441 geraldo 54
    const handleGoBack = () => {
55
        setTest(null);
56
        setSuccess(false)
57
    }
357 geraldo 58
 
395 geraldo 59
 
60
    /**
61
     * componentDidMount
62
     */
63
    useEffect(() => {
399 geraldo 64
        setPage(0);
396 geraldo 65
    }, [action]);
395 geraldo 66
 
306 geraldo 67
    return (
68
        <div>
69
            {loading ? (
70
                <div className="row">
71
                    <Spinner />
72
                </div>
73
            ) : (
382 geraldo 74
                <div>
75
                    {!success ? (
76
                        <div className="row test-section">
384 geraldo 77
 
382 geraldo 78
                            <div className="col-md-12 col-sm-12 col-xs-12">
79
                                <div className="company-title">
80
                                    <div className="section_admin_title_buttons">
81
                                        <h1 className="title">{test.name}</h1>
82
                                    </div>
83
                                </div>
84
                                <div
85
                                    dangerouslySetInnerHTML={{ __html: test.text }}
86
                                    className="description company-title"
87
                                ></div>
307 geraldo 88
                            </div>
382 geraldo 89
                            {test.content.length <= 0 ? (
90
                                <div className="col-md-12 col-sm-12 col-xs-12 text-center">
91
                                    {backendVars.LBL_DATATABLE_SZERORECORDS}
92
                                </div>
93
                            ) : (
94
                                <div className="col-md-12 col-sm-12 col-xs-12">
95
                                    <div className="company-title">
96
                                        {test.content.map((section, key) => {
384 geraldo 97
                                            return (
98
                                                <Section
99
                                                    section={section}
399 geraldo 100
                                                    index={key}
384 geraldo 101
                                                    page={page}
102
                                                    setPage={setPage}
103
                                                    total={test.content.length}
104
                                                    backendVars={backendVars}
105
                                                />)
382 geraldo 106
                                        })}
107
                                    </div>
384 geraldo 108
                                </div>
109
                            )}
382 geraldo 110
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
111
                                <div className="company-title">
112
                                    <button
113
                                        type="button"
114
                                        className="btn btn-secondary"
441 geraldo 115
                                        onClick={() => handleGoBack()}>
116
                                        {backendVars.LBL_GO_BACK}
382 geraldo 117
                                    </button>
118
                                    <button
119
                                        type="buttton"
120
                                        className="btn btn-primary"
121
                                        onClick={() => handleSubmit()}>
122
                                        {backendVars.LBL_SAVE}
123
                                    </button>
124
                                </div>
125
                            </div>
307 geraldo 126
                        </div>
382 geraldo 127
                    ) : (
128
                        <div className="row">
384 geraldo 129
                            <div class="company-title text-center">
130
                                <div class="section_admin_title_buttons">
387 geraldo 131
                                    <h1 class="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 132
                                </div>
382 geraldo 133
                            </div>
384 geraldo 134
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
135
                                <br />
136
                                <button
137
                                    className="btn btn-sm btn-primary"
441 geraldo 138
                                    onClick={() => handleGoBack()}>
139
                                        {backendVars.LBL_GO_BACK}
140
                                    </button>
384 geraldo 141
                            </div>
382 geraldo 142
                        </div>
143
                    )}
306 geraldo 144
                </div>
145
            )}
146
        </div>
147
    )
148
}
149
 
150
export default Test;