Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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