Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Likhith/74122/migrate leaveconfirm to ts #12

Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/account/build/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ const ALIASES = {
Services: path.resolve(__dirname, '../src/Services'),
Stores: path.resolve(__dirname, '../src/Stores'),
Styles: path.resolve(__dirname, '../src/Styles'),
Types: path.resolve(__dirname, '../src/Types'),
};

const rules = (is_test_env = false, is_mocha_only = false) => [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,28 +28,28 @@ const LeaveConfirmComponent = () => {
return (
<Router history={history}>
<Formik>
<LeaveConfirm />
<LeaveConfirm onDirty={jest.fn()} />
</Formik>
</Router>
);
};

const withRouter = Component => {
const history = createBrowserHistory();
return props => {
return (
<Router history={history}>
<Component {...props} />
</Router>
);
};
const WrapperComponent = props => (
<Router history={history}>
<Component {...props} />
</Router>
);

return WrapperComponent;
};

const TransitionBlockerComponent = withRouter(TransitionBlockerWithRouter);

describe('LeaveConfirm', () => {
it('should render LeaveConfirm component in desktop mode', () => {
jest.spyOn(React, 'useState').mockReturnValueOnce([true, () => {}]);
jest.spyOn(React, 'useState').mockReturnValueOnce([true, () => null]);
render(<LeaveConfirmComponent />);
expect(
screen.getByText('You have unsaved changes. Are you sure you want to discard changes and leave this page?')
Expand Down Expand Up @@ -91,8 +91,8 @@ describe('LeaveConfirm', () => {
});
it('should change pathname when user leaves form', () => {
jest.spyOn(React, 'useState')
.mockReturnValueOnce([true, () => {}])
.mockReturnValueOnce([{ pathname: '/' }, () => {}]);
.mockReturnValueOnce([true, () => null])
.mockReturnValueOnce([{ pathname: '/' }, () => null]);
render(<TransitionBlockerComponent />);
const el_leave_settings_btn = screen.getByRole('button', { name: 'Leave Settings' });
fireEvent.click(el_leave_settings_btn);
Expand Down
2 changes: 1 addition & 1 deletion packages/account/src/Components/leave-confirm/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
import LeaveConfirm from './leave-confirm.jsx';
import LeaveConfirm from './leave-confirm';

export default LeaveConfirm;
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
import React from 'react';
import { PropTypes } from 'prop-types';
import { useHistory, withRouter } from 'react-router-dom';
import { FormikConsumer } from 'formik';
import { Button, Icon, Modal } from '@deriv/components';
import { isMobile, PlatformContext } from '@deriv/shared';
import { localize } from '@deriv/translations';
import IconMessageContent from 'Components/icon-message-content';
import { TPlatformContext } from 'Types';

const LeaveConfirmMessage = ({ back, leave }) => {
const { is_appstore } = React.useContext(PlatformContext);
type TLeaveConfirmMessage = {
back: () => void;
leave: () => void;
};

type TTransitionBlocker = {
dirty: boolean;
onDirty: (prop: boolean) => void;
};

const LeaveConfirmMessage = ({ back, leave }: TLeaveConfirmMessage) => {
const { is_appstore } = React.useContext<TPlatformContext>(PlatformContext);
return (
<IconMessageContent
className='leave-confirm'
Expand Down Expand Up @@ -47,17 +57,17 @@ const LeaveConfirmMessage = ({ back, leave }) => {
* Blocks routing if Formik form is dirty
* Has to be a child of <Formik> for FormikConsumer to work
*/
export const TransitionBlocker = ({ dirty, onDirty }) => {
export const TransitionBlocker = ({ dirty, onDirty }: TTransitionBlocker) => {
const [show, setShow] = React.useState(false);
const [next_location, setNextLocation] = React.useState(null);
const [next_location, setNextLocation] = React.useState<{ pathname: string } | null>(null);
const history = useHistory();

React.useEffect(() => {
return () => unblock();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const unblock = history.block(location => {
const unblock = history.block((location: { pathname: string }) => {
if (dirty) {
if (onDirty) onDirty(false);

Expand All @@ -70,10 +80,11 @@ export const TransitionBlocker = ({ dirty, onDirty }) => {
});

const leave = () => {
const { pathname } = next_location;

unblock();
history.push(pathname);
if (next_location && 'pathname' in next_location) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will if (next_location?. pathname) work here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it will

const { pathname } = next_location;
unblock();
history.push(pathname);
} else history.push(null);
};

const back = () => {
Expand All @@ -98,14 +109,10 @@ export const TransitionBlocker = ({ dirty, onDirty }) => {
};
export const TransitionBlockerWithRouter = withRouter(TransitionBlocker);

const LeaveConfirm = ({ onDirty }) => (
const LeaveConfirm = ({ onDirty }: { onDirty: (prop: boolean) => void }) => (
<FormikConsumer>
{formik => <TransitionBlockerWithRouter onDirty={onDirty} dirty={formik.dirty && formik.submitCount === 0} />}
</FormikConsumer>
);

LeaveConfirm.propTypes = {
onDirty: PropTypes.func,
};

export default LeaveConfirm;
4 changes: 4 additions & 0 deletions packages/account/src/Types/common-prop.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export type TPlatformContext = {
is_appstore: boolean;
displayName: string;
};
1 change: 1 addition & 0 deletions packages/account/src/Types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './common-prop.types';
8 changes: 3 additions & 5 deletions packages/account/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,13 @@
"Containers/*": ["src/Containers/*"],
"Constants/*": ["src/Constants/*"],
"Configs/*": ["src/Configs/*"],
"Duplicated/*": ["src/Duplicated/*"],
"Helpers/*": ["src/Helpers/*"],
"Layout/*": ["src/Layout/*"],
"Modules/*": ["src/Modules/*"],
"Sections/*": ["src/Sections/*"],
"Stores/*": ["src/Stores/*"],
"Styles/*": ["src/Styles/*"],
"@deriv/*": ["../*/src"]
"Types": ["src/Types"],
"@deriv/*": ["../*/src"],
}
},
"include": ["src"]
}
}