Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 469 | Rev 471 | 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 &&
470 geraldo 115
                                <div className="col-md-12 col-sm-12 col-xs-12">
116
                                    <div className="company-title">
117
                                        <div className="alert alert-success alert-dismissible fade show" role="alert" >
118
                                            Sus cambios se han guardado con éxito
119
                                            <button
120
                                                type="button"
121
                                                className="close"
122
                                                data-dismiss="alert" aria-label="Close"
123
                                                onClick={() => setDraft(false)}
469 geraldo 124
 
470 geraldo 125
                                            >
126
                                                <span aria-hidden="true">&times;</span>
127
                                            </button>
128
                                        </div>
469 geraldo 129
                                    </div>
130
                                </div>
131
                            }
382 geraldo 132
                            <div className="col-md-12 col-sm-12 col-xs-12 text-right">
133
                                <div className="company-title">
134
                                    <button
135
                                        type="button"
136
                                        className="btn btn-secondary"
441 geraldo 137
                                        onClick={() => handleGoBack()}>
138
                                        {backendVars.LBL_GO_BACK}
382 geraldo 139
                                    </button>
140
                                    <button
141
                                        type="buttton"
142
                                        className="btn btn-primary"
143
                                        onClick={() => handleSubmit()}>
144
                                        {backendVars.LBL_SAVE}
145
                                    </button>
146
                                </div>
147
                            </div>
307 geraldo 148
                        </div>
382 geraldo 149
                    ) : (
150
                        <div className="row">
469 geraldo 151
                            <div className="company-title text-center">
152
                                <div className="section_admin_title_buttons">
153
                                    <h1 className="title">{backendVars.LBL_SUCCESS_SELF_EVALUATION}</h1>
384 geraldo 154
                                </div>
382 geraldo 155
                            </div>
384 geraldo 156
                            <div className="col-md-12 col-sm-12 col-xs-12 text-center">
157
                                <br />
158
                                <button
159
                                    className="btn btn-sm btn-primary"
441 geraldo 160
                                    onClick={() => handleGoBack()}>
469 geraldo 161
                                    {backendVars.LBL_GO_BACK}
162
                                </button>
384 geraldo 163
                            </div>
382 geraldo 164
                        </div>
165
                    )}
306 geraldo 166
                </div>
167
            )}
168
        </div>
169
    )
170
}
171
 
172
export default Test;