forked from deriv-com/deriv-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request deriv-com#8 from oskar-binary/unsaved_changes
Unsaved changes
- Loading branch information
Showing
5 changed files
with
252 additions
and
150 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/trader/src/Assets/AccountManagement/icon-unsaved-changes.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion
5
packages/trader/src/Modules/Account/Components/icon-message-content.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/trader/src/Modules/Account/Components/leave-confirm.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
); |
Oops, something went wrong.