Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 561 | | 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
565 geraldo 7
    const { section, backendVars, page, setPage, total, index  } = props;
357 geraldo 8
 
364 geraldo 9
    //init states
365 geraldo 10
    const [errors, setErrors] = useState([]);
451 geraldo 11
 
357 geraldo 12
 
360 geraldo 13
    /**
14
     * Check if there are questions to answer
15
     * @returns
16
     */
17
    const validateSection = () => {
370 geraldo 18
        setErrors([]);
360 geraldo 19
        let formValid = true;
366 geraldo 20
        let messages = [];
360 geraldo 21
        section.questions.map((question) => {
22
            //Validate if the answer is empty
381 geraldo 23
            if (!question.answer || question.answer.length == 0) {
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
     */
391 geraldo 37
    const handlePrevious = () => {
38
        setErrors([]);
39
        setPage(page - 1);
40
    };
364 geraldo 41
 
391 geraldo 42
 
364 geraldo 43
    /**
44
     * Continue to the next section
45
     */
451 geraldo 46
    const handleNext = () => validateSection() && setPage(page + 1);
47
 
364 geraldo 48
 
306 geraldo 49
    return (
312 geraldo 50
 
400 geraldo 51
        <div className="panel-group" hidden={page == index ? false : true}>
312 geraldo 52
            <div className="panel panel-default" id={`panel-${section.slug_section}`}>
53
                <div className="panel-heading">
392 geraldo 54
                    <h4 className="panel-title">{section.name}</h4>
312 geraldo 55
                </div>
56
                <div id={section.slug_section} className="panel-collapse in collapse show">
57
                    <div className="panel-body">
58
                        <div
59
                            dangerouslySetInnerHTML={{ __html: section.text }}
60
                            className="description"
61
                        />
62
                        <div className="row">
357 geraldo 63
                            {section.questions.map((question, i) => {
332 geraldo 64
                                return <Question
65
                                    question={question}
357 geraldo 66
                                    key={i}
332 geraldo 67
                                    backendVars={backendVars}
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 (
469 geraldo 76
                                            <div className="alert alert-danger alert-dismissible fade show" role="alert" key={index}>
392 geraldo 77
                                                {error}
469 geraldo 78
                                                <button type="button" className="close" data-dismiss="alert" aria-label="Close">
392 geraldo 79
                                                    <span aria-hidden="true">&times;</span>
80
                                                </button>
81
                                            </div>);
365 geraldo 82
                                    })}
364 geraldo 83
                                </div>
365 geraldo 84
                            }
367 geraldo 85
                            <div className="col-md-12 np-padding">
469 geraldo 86
                                <ul className="wizard">
87
                                    <li className="previous">
400 geraldo 88
                                        {index != 0 &&
365 geraldo 89
                                            <button
90
                                                type="button"
91
                                                className="btn btn-secondary"
92
                                                onClick={() => handlePrevious()}
93
                                            >
561 geraldo 94
                                                {backendVars.LBL_EVALUATION_TEST_FORM_PREVIOUS}
365 geraldo 95
                                            </button>
368 geraldo 96
                                        }
97
                                    </li>
469 geraldo 98
                                    <li className="next">
400 geraldo 99
                                        {index != total - 1 &&
365 geraldo 100
                                            <button
101
                                                type="button"
102
                                                onClick={() => handleNext()}
103
                                                className="btn btn-secondary">
561 geraldo 104
                                                {backendVars.LBL_EVALUATION_TEST_FORM_NEXT}
365 geraldo 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;