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

Vinu/Ts cashier error dialog #5820

Merged
merged 8 commits into from
Jul 12, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ jest.mock('Stores/connect.js', () => ({
}));

describe('<ErrorDialog />', () => {
let modal_root_el;
beforeAll(() => {
const modal_root_el = document.createElement('div');
modal_root_el = document.createElement('div');
modal_root_el.setAttribute('id', 'modal_root');
document.body.appendChild(modal_root_el);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,45 @@
import PropTypes from 'prop-types';
import React from 'react';
import { Link, useHistory } from 'react-router-dom';
import { Dialog } from '@deriv/components';
import { localize, Localize } from '@deriv/translations';
import { routes } from '@deriv/shared';
import { connect } from 'Stores/connect';
import { RootStore, TReactElement } from 'Types';

const ErrorDialog = ({ disableApp, enableApp, error = {} }) => {
type TErrorDialogProps = {
disableApp: () => void;
enableApp: () => void;
error: {
message?: string;
code?: string;
setErrorMessage?: (message: string) => void;
};
};

type TSetDetails = {
title: string;
cancel_button_text: undefined | string;
confirm_button_text: undefined | string;
onConfirm: undefined | (() => void);
message: undefined | string | TReactElement;
has_close_icon?: boolean;
};

const ErrorDialog = ({ disableApp, enableApp, error = {} }: TErrorDialogProps) => {
const history = useHistory();
const [is_visible, setIsVisible] = React.useState(false);
const [details, setDetails] = React.useState({
const [details, setDetails] = React.useState<TSetDetails>({
title: '',
cancel_button_text: undefined,
confirm_button_text: '',
onConfirm: undefined,
message: '',
});

React.useEffect(() => {
// avoid resetting the text when dismissing the pop up
if (error.message) {
mapErrorToDetails(error.code, error.message);
}
}, [error.code, error.message, mapErrorToDetails]);

React.useEffect(() => {
setErrorVisibility(!!error.message);
}, [error.message]);

const mapErrorToDetails = React.useCallback(
(error_code, error_message) => {
(error_code?: string, error_message?: string) => {
if (
error_code &&
[
'Fiat2CryptoTransferOverLimit',
'Crypto2FiatTransferOverLimit',
Expand Down Expand Up @@ -94,12 +103,25 @@ const ErrorDialog = ({ disableApp, enableApp, error = {} }) => {
[history]
);

const setErrorVisibility = is_error_visible => {
React.useEffect(() => {
// avoid resetting the text when dismissing the pop up
if (error.message) {
mapErrorToDetails(error.code, error.message);
}
}, [error.code, error.message, mapErrorToDetails]);

React.useEffect(() => {
setErrorVisibility(!!error.message);
}, [error.message]);

const setErrorVisibility = (is_error_visible: boolean) => {
setIsVisible(is_error_visible);
};

const dismissError = () => {
error.setErrorMessage('');
if (error.setErrorMessage) {
error.setErrorMessage('');
}
setErrorVisibility(false);
};

Expand Down Expand Up @@ -130,13 +152,7 @@ const ErrorDialog = ({ disableApp, enableApp, error = {} }) => {
);
};

ErrorDialog.propTypes = {
error: PropTypes.object,
disableApp: PropTypes.func,
enableApp: PropTypes.func,
};

export default connect(({ ui }) => ({
export default connect(({ ui }: RootStore) => ({
disableApp: ui.disableApp,
enableApp: ui.enableApp,
}))(ErrorDialog);
3 changes: 0 additions & 3 deletions packages/cashier/src/components/error-dialog/index.js

This file was deleted.

3 changes: 3 additions & 0 deletions packages/cashier/src/components/error-dialog/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import ErrorDialog from './error-dialog';

export default ErrorDialog;
4 changes: 4 additions & 0 deletions packages/cashier/src/types/props.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ export type TReactChangeEvent = React.ChangeEvent<HTMLInputElement>;
export type TReactChildren = React.ReactNode;

export type TReactMouseEvent = React.MouseEvent<HTMLElement>;

export type TReactFormEvent = React.FormEvent<HTMLInputElement>;

export type TReactElement = React.ReactElement;