Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

Rev Autor Línea Nro. Línea
561 geraldo 1
import React, { useState } from "react";
564 geraldo 2
import Option from "../option/Option";
561 geraldo 3
 
4
const Section = (props) => {
5
 
6
    // get props
564 geraldo 7
    const { section, backendVars, page, setPage, total, index } = props;
561 geraldo 8
 
9
    //init states
10
    const [errors, setErrors] = useState([]);
564 geraldo 11
    const [input, setInput] = useState(section.answer);
561 geraldo 12
 
564 geraldo 13
 
561 geraldo 14
    /**
15
     * Check if there are questions to answer
16
     * @returns
17
     */
18
    const validateSection = () => {
19
        setErrors([]);
20
        let formValid = true;
21
        let messages = [];
22
        section.questions.map((question) => {
23
            //Validate if the answer is empty
24
            if (!question.answer || question.answer.length == 0) {
25
                // set error message
26
                messages.push(backendVars.LBL_EMPTY_ANSWER.replace('%n', question.position + 1));
27
                formValid = false;
28
            }
29
        });
30
        setErrors(messages);
31
        return formValid;
32
    }
33
 
34
    /**
35
     * Return to previous section
36
     * @returns
37
     */
38
    const handlePrevious = () => {
39
        setErrors([]);
40
        setPage(page - 1);
41
    };
42
 
564 geraldo 43
    /**
44
     * Update section answer
45
     * @param {*} value
46
     */
47
     const handleAnswer = (value) => {
48
        setInput(value);
49
        section.answer = value;
50
    }
561 geraldo 51
 
564 geraldo 52
 
561 geraldo 53
    /**
54
     * Continue to the next section
55
     */
56
    const handleNext = () => validateSection() && setPage(page + 1);
57
 
564 geraldo 58
    /**
59
     * componentDidMount
60
     */
61
     useEffect(() => {
62
        setInput(section.answer);
63
    }, [section]);
64
 
65
 
561 geraldo 66
    return (
67
        <div className="panel-group" hidden={page == index ? false : true}>
564 geraldo 68
            <div className="panel panel-default" id={`panel-${section.id_section}`}>
561 geraldo 69
                <div className="panel-heading">
564 geraldo 70
                    <h4 className="panel-title">{section.title}</h4>
561 geraldo 71
                </div>
564 geraldo 72
                <div id={section.id_section} className="panel-collapse in collapse show">
561 geraldo 73
                    <div className="panel-body">
74
                        <div
75
                            dangerouslySetInnerHTML={{ __html: section.text }}
76
                            className="description"
77
                        />
78
                        <div className="row">
564 geraldo 79
                            {section.type == 'simple' ? (
80
                                <div className="form-group">
81
 
82
                                    <textarea
83
                                        className="form-control"
84
                                        rows="5"
85
                                        value={input}
86
                                        maxLength='200'
87
                                        name={section.id_section}
88
                                        onChange={e =>
89
                                            handleAnswer(e.target.value)}
90
                                    />
91
                                </div>
92
                            ) : (
93
                                <div>
94
                                    {section.options.map((option, i) => {
95
                                        return <Option
96
                                            option={option}
97
                                            key={i}
98
                                            handleAnswer={handleAnswer}
99
                                        />
100
 
101
                                    })}
102
                                </div>)}
103
 
561 geraldo 104
                        </div>
564 geraldo 105
 
561 geraldo 106
                        <div className="row">
107
                            {errors.length > 0 &&
108
                                <div className="col-md-12 np-padding">
109
                                    {errors.map((error, index) => {
110
                                        return (
111
                                            <div className="alert alert-danger alert-dismissible fade show" role="alert" key={index}>
112
                                                {error}
113
                                                <button type="button" className="close" data-dismiss="alert" aria-label="Close">
114
                                                    <span aria-hidden="true">&times;</span>
115
                                                </button>
116
                                            </div>);
117
                                    })}
118
                                </div>
119
                            }
120
                            <div className="col-md-12 np-padding">
121
                                <ul className="wizard">
122
                                    <li className="previous">
123
                                        {index != 0 &&
124
                                            <button
125
                                                type="button"
126
                                                className="btn btn-secondary"
127
                                                onClick={() => handlePrevious()}
128
                                            >
564 geraldo 129
                                                {backendVars.LBL_EVALUATION_TEST_FORM_PREVIOUS}
561 geraldo 130
                                            </button>
131
                                        }
132
                                    </li>
133
                                    <li className="next">
134
                                        {index != total - 1 &&
135
                                            <button
136
                                                type="button"
137
                                                onClick={() => handleNext()}
138
                                                className="btn btn-secondary">
564 geraldo 139
                                                {backendVars.LBL_EVALUATION_TEST_FORM_NEXT}
561 geraldo 140
                                            </button>
141
                                        }
142
                                    </li>
143
                                </ul>
144
                            </div>
145
                        </div>
146
                    </div>
147
                </div>
148
            </div>
149
        </div>
150
    )
151
}
152
 
153
export default Section;