UNPKG

@wenn/onb

Version:

onb-core

104 lines (85 loc) 2.54 kB
import React from 'react'; import PropTypes from 'prop-types'; import { connect } from 'react-redux'; import { reduxForm } from 'redux-form'; import { saveCamunda, sendToCamunda } from '../../redux/actions/camunda.actions'; const ValidateIdentityHOC = Component => { class ValidateIdentity extends React.Component { componentDidMount() { sendlog("view_page"); } handleOnSend = e => { e.preventDefault(); const { formData: { values }, questions: { query_id }, viewName, saveCamunda, sendToCamunda } = this.props; const oldAnswers = Object.keys(values).map(item => ({ question_id: item.replace('a', ''), selected_answer_id: values[item].replace('a', '') })); const QUESTS = this.props.questions.validation_questionnaire.questions; const questsMap = {}; QUESTS.forEach(element => { questsMap[element.id] = {}; element.options.forEach(option => { questsMap[element.id][option.id] = option.text; }); }); const answers = oldAnswers.map(ans => ({ ...ans, text: questsMap[ans.question_id][ans.selected_answer_id] })); const data = { answers: JSON.stringify({ answers, query_id }) }; sendlog('event', { event_action: 'click', ...data }); saveCamunda(data); sendToCamunda(viewName); }; render() { return <Component onHandleOnSend={this.handleOnSend} {...this.props} />; } } ValidateIdentity.proptTypes = { formData: PropTypes.object.isRequired, wording: PropTypes.object.isRequired, saveCamunda: PropTypes.func.isRequired, sendToCamunda: PropTypes.func.isRequired, viewName: PropTypes.string.isRequired, questions: PropTypes.object.isRequired }; ValidateIdentity = reduxForm({ form: 'ValidateIdentity', destroyOnUnmount: false, forceUnregisterOnUnmount: true })(ValidateIdentity); const mapState = ( { settings: { wording, config, theme }, form, camundaData }, { viewName } ) => ({ formData: form.ValidateIdentity ? form.ValidateIdentity : {}, wording: wording[viewName], config, viewName, theme: theme, questions: camundaData.validation_questions_retrieval ? camundaData.validation_questions_retrieval : {} }); const mapDispatch = { saveCamunda, sendToCamunda }; return connect( mapState, mapDispatch )(ValidateIdentity); }; export default ValidateIdentityHOC;