Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
364 geraldo 1
import React, { useState } from "react";
306 geraldo 2
import Question from "../question/Question";
3
 
4
const Section = (props) => {
307 geraldo 5
 
364 geraldo 6
    // get props
365 geraldo 7
    const { section, backendVars, handleAnswer, page, setPage, total } = props;
357 geraldo 8
 
364 geraldo 9
    //init states
365 geraldo 10
    const [errors, setErrors] = useState([]);
357 geraldo 11
 
360 geraldo 12
    /**
13
     * Check if there are questions to answer
14
     * @returns
15
     */
16
    const validateSection = () => {
370 geraldo 17
        setErrors([]);
360 geraldo 18
        let formValid = true;
366 geraldo 19
        let messages = [];
360 geraldo 20
        section.questions.map((question) => {
376 geraldo 21
            console.log(question);
360 geraldo 22
            //Validate if the answer is empty
23
            if (!question.answer) {
366 geraldo 24
                // set error message
365 geraldo 25
                messages.push(backendVars.LBL_EMPTY_ANSWER.replace('%n', question.position + 1));
360 geraldo 26
                formValid = false;
27
            }
365 geraldo 28
        });
372 geraldo 29
        setErrors(messages);
360 geraldo 30
        return formValid;
31
    }
357 geraldo 32
 
364 geraldo 33
    /**
34
     * Return to previous section
35
     * @returns
36
     */
37
    const handlePrevious = () => setPage(page - 1);
38
 
39
 
40
    /**
41
     * Continue to the next section
42
     */
43
    const handleNext = () => {
44
        if (validateSection()) {
45
            setPage(page + 1);
46
        }
47
    }
48
 
306 geraldo 49
    return (
312 geraldo 50
 
364 geraldo 51
        <div className="panel-group" hidden={page == section.position ? false : true}>
312 geraldo 52
            <div className="panel panel-default" id={`panel-${section.slug_section}`}>
53
                <div className="panel-heading">
54
                    <h4 className="panel-title">
363 geraldo 55
                        <a>{section.name}</a>
312 geraldo 56
                    </h4>
57
                </div>
58
                <div id={section.slug_section} className="panel-collapse in collapse show">
59
                    <div className="panel-body">
60
                        <div
61
                            dangerouslySetInnerHTML={{ __html: section.text }}
62
                            className="description"
63
                        />
64
                        <div className="row">
357 geraldo 65
                            {section.questions.map((question, i) => {
332 geraldo 66
                                return <Question
67
                                    question={question}
357 geraldo 68
                                    key={i}
332 geraldo 69
                                    backendVars={backendVars}
342 geraldo 70
                                    handleAnswer={handleAnswer}
332 geraldo 71
                                />;
312 geraldo 72
                            })}
73
                        </div>
357 geraldo 74
                        <div className="row">
365 geraldo 75
                            {errors.length > 0 &&
76
                                <div className="col-md-12 np-padding">
374 geraldo 77
                                    {errors.map((error, index) => {
372 geraldo 78
                                        return (
365 geraldo 79
                                        <div class="alert alert-danger" role="alert" key={index}>
80
                                            {error}
372 geraldo 81
                                        </div>);
365 geraldo 82
                                    })}
364 geraldo 83
                                </div>
365 geraldo 84
                            }
367 geraldo 85
                            <div className="col-md-12 np-padding">
363 geraldo 86
                                <ul class="wizard">
368 geraldo 87
                                    <li class="previous">
88
                                        {section.position != 0 &&
365 geraldo 89
                                            <button
90
                                                type="button"
91
                                                className="btn btn-secondary"
92
                                                onClick={() => handlePrevious()}
93
                                            >
94
                                                {backendVars.LBL_SELF_EVALUATION_TEST_FORM_PREVIOUS}
95
                                            </button>
368 geraldo 96
                                        }
97
                                    </li>
98
                                    <li class="next">
99
                                        {section.position != total - 1 &&
365 geraldo 100
                                            <button
101
                                                type="button"
102
                                                onClick={() => handleNext()}
103
                                                className="btn btn-secondary">
104
                                                {backendVars.LBL_SELF_EVALUATION_TEST_FORM_NEXT}
105
                                            </button>
368 geraldo 106
                                        }
107
                                    </li>
359 geraldo 108
                                </ul>
357 geraldo 109
                            </div>
110
                        </div>
312 geraldo 111
                    </div>
112
                </div>
306 geraldo 113
            </div>
114
        </div>
115
    )
116
}
117
 
118
export default Section;