Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Autoría | Ultima modificación | Ver Log |

import React, { useState, useEffect } from "react";
import Option from "../behavior/Option";

const Competency = (props) => {

    // get props
    const { competency, backendVars, page, setPage, total, index } = props;

    //init states 
    const [errors, setErrors] = useState([]);
    const [input, setInput] = useState(competency.answer);


    /**
     * Check if there are behaviors to answer
     * @returns 
     */
    const validateCompetency = () => {
        setErrors([]);
        let formValid = true;
        let messages = [];
            competency.behaviors.map((behavior) => {
                //Validate if the answer is empty
                if (!behavior.answer || behavior.answer.length == 0) {
                    messages.push(backendVars.LBL_ERROR_FIELDS_EMPTY);
                    formValid = false;
                }
            });
        
        setErrors(messages);
        return formValid;
    }

    /**
     * Return to previous competency
     * @returns 
     */
    const handlePrevious = () => {
        setErrors([]);
        setPage(page - 1);
    };

    /**
     * Update competency answer
     * @param {*} value
     */
    const handleAnswer = (value) => {
        setInput(value);
        competency.answer = value;
    }


    /**
     * Continue to the next competency
     */
    const handleNext = () => validateCompetency() && setPage(page + 1);

    /**
     * componentDidMount
     */
    useEffect(() => {
        setInput(competency.answer);
    }, [competency]);


    return (
        <div className="panel-group" hidden={page == index ? false : true}>
            <div className="panel panel-default" id={`panel-${competency.competency_id}`}>
                <div className="panel-heading">
                    <h4 className="panel-title">{competency.type} {competency.name}</h4>
                </div>
                <div id={competency.competency_id} className="panel-collapse in collapse show">
                    <div className="panel-body">


                        <div
                            dangerouslySetInnerHTML={{ __html: competency.description }}
                            className="description"
                        />>


                        <div className="row">
                            
                                <div className="col-md-12 col-sm-12 col-xs-12 np-padding">
                                    {competency.behaviors.length > 0 &&
                                        <div className="col-md-12 col-sm-12 col-xs-12 np-padding">
                                            {competency.behaviors.map((behavior, i) => {
                                                return <Option
                                                    behavior={behavior}
                                                    key={i}
                                                    handleAnswer={handleAnswer}
                                                />

                                            })}
                                        </div>}
                                </div>

                        </div>

                        <div className="row">
                            {errors.length > 0 &&
                                <div className="col-md-12 np-padding">
                                    {errors.map((error, index) => {
                                        return (
                                            <div className="alert alert-danger alert-dismissible fade show" role="alert" key={index}>
                                                {error}
                                                <button type="button" className="close" data-dismiss="alert" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                            </div>);
                                    })}
                                </div>
                            }
                            <div className="col-md-12 np-padding">
                                <ul className="wizard">
                                    <li className="previous">
                                        {index != 0 &&
                                            <button
                                                type="button"
                                                className="btn btn-secondary"
                                                onClick={() => handlePrevious()}
                                            >
                                                {backendVars.LBL_EVALUATION_TEST_FORM_PREVIOUS}
                                            </button>
                                        }
                                    </li>
                                    <li className="next">
                                        {index != total - 1 &&
                                            <button
                                                type="button"
                                                onClick={() => handleNext()}
                                                className="btn btn-secondary">
                                                {backendVars.LBL_EVALUATION_TEST_FORM_NEXT}
                                            </button>
                                        }
                                    </li>
                                </ul>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    )
}

export default Competency;