Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 376 | Rev 381 | 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
     */
378 geraldo 43
    const handleNext = () => validateSection() && setPage(page + 1);
44
 
364 geraldo 45
 
306 geraldo 46
    return (
312 geraldo 47
 
364 geraldo 48
        <div className="panel-group" hidden={page == section.position ? false : true}>
312 geraldo 49
            <div className="panel panel-default" id={`panel-${section.slug_section}`}>
50
                <div className="panel-heading">
51
                    <h4 className="panel-title">
363 geraldo 52
                        <a>{section.name}</a>
312 geraldo 53
                    </h4>
54
                </div>
55
                <div id={section.slug_section} className="panel-collapse in collapse show">
56
                    <div className="panel-body">
57
                        <div
58
                            dangerouslySetInnerHTML={{ __html: section.text }}
59
                            className="description"
60
                        />
61
                        <div className="row">
357 geraldo 62
                            {section.questions.map((question, i) => {
332 geraldo 63
                                return <Question
64
                                    question={question}
357 geraldo 65
                                    key={i}
332 geraldo 66
                                    backendVars={backendVars}
342 geraldo 67
                                    handleAnswer={handleAnswer}
332 geraldo 68
                                />;
312 geraldo 69
                            })}
70
                        </div>
357 geraldo 71
                        <div className="row">
365 geraldo 72
                            {errors.length > 0 &&
73
                                <div className="col-md-12 np-padding">
374 geraldo 74
                                    {errors.map((error, index) => {
372 geraldo 75
                                        return (
365 geraldo 76
                                        <div class="alert alert-danger" role="alert" key={index}>
77
                                            {error}
372 geraldo 78
                                        </div>);
365 geraldo 79
                                    })}
364 geraldo 80
                                </div>
365 geraldo 81
                            }
367 geraldo 82
                            <div className="col-md-12 np-padding">
363 geraldo 83
                                <ul class="wizard">
368 geraldo 84
                                    <li class="previous">
85
                                        {section.position != 0 &&
365 geraldo 86
                                            <button
87
                                                type="button"
88
                                                className="btn btn-secondary"
89
                                                onClick={() => handlePrevious()}
90
                                            >
91
                                                {backendVars.LBL_SELF_EVALUATION_TEST_FORM_PREVIOUS}
92
                                            </button>
368 geraldo 93
                                        }
94
                                    </li>
95
                                    <li class="next">
96
                                        {section.position != total - 1 &&
365 geraldo 97
                                            <button
98
                                                type="button"
99
                                                onClick={() => handleNext()}
100
                                                className="btn btn-secondary">
101
                                                {backendVars.LBL_SELF_EVALUATION_TEST_FORM_NEXT}
102
                                            </button>
368 geraldo 103
                                        }
104
                                    </li>
359 geraldo 105
                                </ul>
357 geraldo 106
                            </div>
107
                        </div>
312 geraldo 108
                    </div>
109
                </div>
306 geraldo 110
            </div>
111
        </div>
112
    )
113
}
114
 
115
export default Section;