@wenn/onb
Version:
onb-core
185 lines (168 loc) • 4.63 kB
JavaScript
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import { reduxForm, change } from 'redux-form';
import {
saveCamunda,
sendToCamunda
} from '../../redux/actions/camunda.actions';
import {
SAVE_RETRY_SMS,
RESET_FAILED_CODE_ATTEMPTS,
SET_SMS_FLAG,
UNSET_SMS_FLAG
} from '../../redux/constants';
import { globalNotification } from './../../redux/actions/notifications.actions';
import { saveFlowError } from './../../redux/actions/run.actions';
const BasicInfoHOC = Component => {
class BasicInfo extends React.Component {
componentDidMount() {
sendlog('view_page');
}
componentWillUpdate() {
const { failedCodeAttempts, dispatch, retrySms } = this.props;
if (failedCodeAttempts.count >= 5 && retrySms >= 2) {
dispatch(
globalNotification({
bpmStep: {
code: 'sms_retry_limit',
step: 'end'
}
})
);
dispatch(saveFlowError(true));
} else if (failedCodeAttempts.count >= 5 && retrySms < 3) {
this.sendSms();
} else {
dispatch({ type: UNSET_SMS_FLAG });
}
}
parseBank(v) {
return v && v.replace(/( |[0-9])/g, '');
}
limitLength(v, max) {
return v && v.substring(0, max);
}
sendSms = e => {
e && e.preventDefault();
const {
formData: { values },
viewName,
saveCamunda,
sendToCamunda,
dispatch
} = this.props;
const { validationcellcode, validationcellphone } = values;
let data = { resendSMS: 'true' };
if (validationcellcode && validationcellphone) {
dispatch(
change('InitialInfo', 'validationcellcode', values.validationcellcode)
);
dispatch(
change(
'InitialInfo',
'validationcellphone',
values.validationcellphone
)
);
data = {
...data,
validationcellcode,
validationcellphone
};
}
dispatch({ type: SAVE_RETRY_SMS });
dispatch({ type: SET_SMS_FLAG });
dispatch({ type: RESET_FAILED_CODE_ATTEMPTS });
saveCamunda(data);
sendToCamunda(`${viewName}:resendSMS`, {
goto: { type: 'String', value: 'step2' },
flowerror: { type: 'String', value: 'false' }
});
};
handleOnSend = e => {
e.preventDefault();
const {
formData: { values },
viewName,
saveCamunda,
sendToCamunda,
camunda
} = this.props;
const data = {
...values,
bank: values.bank || '150',
resendSMS: 'false',
sms_not_received: 'false'
};
if (window.dataLayer) {
const {
email,
validationcellcode,
validationcellphone,
firstname,
lastname
} = camunda;
window.dataLayer.push({
event: 'info_fb_amm',
email: email.value,
phone: `${validationcellcode ? validationcellcode.value : ''}${validationcellphone.value}`,
firstname: firstname.value,
lastname: lastname.value
});
}
sendlog('event', { event_action: 'click', ...data });
saveCamunda(data);
sendToCamunda(viewName);
};
render() {
return (
<div>
<Component
{...this.props}
onHandleOnSend={this.handleOnSend}
parseBank={this.parseBank}
limitLength={this.limitLength}
sendSms={this.sendSms}
/>
</div>
);
}
}
BasicInfo.propTypes = {
formData: PropTypes.object.isRequired,
wording: PropTypes.object.isRequired,
saveCamunda: PropTypes.func.isRequired,
sendToCamunda: PropTypes.func.isRequired,
viewName: PropTypes.string.isRequired
};
BasicInfo = reduxForm({
form: 'BasicInfo',
destroyOnUnmount: false,
forceUnregisterOnUnmount: true
})(BasicInfo);
const mapState = (
{ settings, form, camunda, bpmStep, retrySms, failedCodeAttempts },
{ viewName }
) => ({
formData: form.BasicInfo ? form.BasicInfo : {},
wording: settings.wording[viewName],
config: settings.config,
retrySms,
failedCodeAttempts,
camunda,
firstname: camunda && camunda.firstname ? camunda.firstname.value : '',
BANKS: settings.utils.BANKS.AR,
viewName,
bpmStep
});
const mapDispatch = {
saveCamunda,
sendToCamunda
};
return connect(
mapState,
mapDispatch
)(BasicInfo);
};
export default BasicInfoHOC;