Skip to content

Commit

Permalink
Merge pull request deriv-com#8 from oskar-binary/unsaved_changes
Browse files Browse the repository at this point in the history
Unsaved changes
  • Loading branch information
oskar-binary authored Sep 12, 2019
2 parents 4f3fc10 + c96ec6c commit a9c4540
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 150 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import React from 'react';

const IconMessageContent = ({ children, icon, message }) => (
const IconMessageContent = ({ children, icon, message, text }) => (
<div className='account-management__message-content'>
<div className='account-management__message-icon'>
{icon}
</div>
<div className='account-management__message'>
{message}
</div>
<div className='account-management__text'>
{text}
</div>
{children}
</div>
);
Expand Down
74 changes: 74 additions & 0 deletions packages/trader/src/Modules/Account/Components/leave-confirm.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import React from 'react';
import { withRouter } from 'react-router-dom';
import { FormikConsumer } from 'formik';
import { Button } from 'deriv-components';
import { localize } from 'App/i18n';
import IconUnsavedChanges from 'Assets/AccountManagement/icon-unsaved-changes.svg';
import IconMessageContent from '../Components/icon-message-content';

/**
* Blocks routing if Formik form is dirty
* Has to be a child of <Formik> for FormikConsumer to work
*/
class TransitionBlocker extends React.Component {
state = { show: false }

componentDidMount() {
this.unblock = this.props.history.block(next_location => {
if (this.props.dirty) {
this.props.onDirty(false);
this.setState({
show: true,
next_location,
});
}
return !this.props.dirty;
});

}

leave = () => {
const { pathname } = this.state.next_location;
this.unblock();
this.props.history.push(pathname);
}

back = () => {
this.setState({ nextLocation: null, show: false });
this.props.onDirty(true);
}

componentWillUnmount() {
this.unblock();
}

render() {
const { show } = this.state;

return (
<>
{show &&
<>
<IconMessageContent
message={localize('You have unsaved changes')}
text={localize('Are you sure you want to discard changes and leave this page?')}
icon={<IconUnsavedChanges />}
>
<div className='account-management-flex-wrapper'>
<Button type='button' onClick={this.back}>{localize('Cancel')}</Button>
<Button type='button' onClick={this.leave}>{localize('Leave')}</Button>
</div>
</IconMessageContent>
</>
}
</>
)
}
}
const TransitionBlockerWithRouter = withRouter(TransitionBlocker);

export const LeaveConfirm = ({ onDirty }) => (
<FormikConsumer>
{formik => <TransitionBlockerWithRouter onDirty={onDirty} dirty={formik.dirty && formik.submitCount === 0}/>}
</FormikConsumer>
);
Loading

0 comments on commit a9c4540

Please sign in to comment.