@wenn/onb
Version:
onb-core
90 lines (80 loc) • 2.03 kB
JavaScript
import React from 'react';
import { connect } from 'react-redux';
import FallbackForm from './FallbackForm';
import ErrorPage from './ErrorPage';
import { saveHasError } from './redux/actions/hasError.actions';
class HandleNotifications extends React.Component {
constructor(props) {
super(props);
this.state = {
values: {}
};
}
gatheredData() {
let search = (needle, haystack, found = {}) => {
Object.keys(haystack).forEach(key => {
if (key === needle) {
found = { ...found, ...haystack[key] };
return found;
}
if (typeof haystack[key] === 'object') {
found = search(needle, haystack[key], found);
}
});
return found;
};
return search('values', this.props.form);
}
componentDidCatch(error, info) {
this.props.saveHasError(true);
// You can also log the error to an error reporting service
// logErrorToMyService(error, info);
sendlog('error', {
error_type: 'critical',
error_name: error.name,
error_desc: error.message,
error_stack: error.stack,
info_desc: info
});
}
render() {
const {
currentStep,
form_data,
hasError,
Desktop,
wording,
children
} = this.props;
if (hasError) {
const gathered = this.gatheredData();
const reachedSf = Object.keys(gathered).includes('bank');
if (form_data && !reachedSf) {
return (
<FallbackForm
Desktop={Desktop}
FORM={form_data}
gathered={gathered}
/>
);
} else {
return <ErrorPage WORDING={wording} />;
}
}
return children;
}
}
const mapDispatchToProps = {
saveHasError
};
const mapState = ({ settings, hasError, currentStep, form }) => ({
wording: settings.feedback.error_page,
form_data: settings.feedback.form_data,
form,
currentStep,
hasError
});
export default connect(
mapState,
mapDispatchToProps
)(HandleNotifications);