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/refactor:removed cognitive complexity #38

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 @@ -107,7 +107,8 @@ const Unsupported = ({
) {
content = status_content;
}
const onClick = () => {

const onClickHandler = () => {
if (manual?.status === AUTH_STATUS_CODES.VERIFIED || manual?.status === AUTH_STATUS_CODES.PENDING) {
return onClickRedirectButton;
} else if (manual?.status === AUTH_STATUS_CODES.EXPIRED) {
Expand All @@ -121,7 +122,7 @@ const Unsupported = ({
status_description={content.description}
status_title={content.title}
>
{content.action_button?.(onClick(), from_platform.name)}
{content.action_button?.({ onClick: onClickHandler(), platform_name: from_platform.name })}
</VerificationStatus>
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ type TButtonLink = {
};

type TButton = {
onClick?: () => void;
onClick?: React.MouseEventHandler<HTMLElement>;
to?: never;
};

Expand Down
3 changes: 1 addition & 2 deletions packages/account/src/Helpers/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
import { ResidenceList, GetAccountStatus } from '@deriv/api-types';
import { FormikValues } from 'formik';
import { getIDVDocuments } from '../Constants/idv-document-config';
import { TServerError } from '../Types';
import { TIDVErrorStatus, TServerError } from '../Types';
import { LANGUAGE_CODES } from '../Constants/onfido';

export const documentAdditionalError = (
Expand Down Expand Up @@ -265,7 +265,6 @@ export const validate = <T,>(errors: Record<string, string>, values: T) => {
};
};

export type TIDVErrorStatus = typeof idv_error_statuses[keyof typeof idv_error_statuses];
export const verifyFields = (status: TIDVErrorStatus) => {
switch (status) {
case idv_error_statuses.poi_dob_mismatch:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,57 +30,22 @@ type TAuthStatus = {
is_submitted?: boolean;
};

export const getPOAStatusMessages = (
status: typeof AUTH_STATUS_CODES[keyof typeof AUTH_STATUS_CODES],
auth_status?: TAuthStatus,
should_show_redirect_btn?: boolean
) => {
const is_redirected_from_platform = isNavigationFromP2P() || isNavigationFromDerivGO();

const pendingButton = (onClick?: () => void, platform_name?: string) => {
if (!auth_status?.needs_poi) {
if (should_show_redirect_btn)
return (
<VerificationStatusActionButton
button_text={
<Localize i18n_default_text='Back to {{platform_name}}' values={{ platform_name }} />
}
onClick={onClick}
/>
);
if (!is_redirected_from_platform) {
return (
<VerificationStatusActionButton
to={routes.root}
button_text={<Localize i18n_default_text='Continue trading' />}
/>
);
}
}
if (auth_status?.needs_poi) {
return (
<VerificationStatusActionButton
to={routes.proof_of_identity}
button_text={<Localize i18n_default_text='Proof of identity' />}
/>
);
}
return null;
};

const resubmitButton = (onClick?: () => void) => (
<VerificationStatusActionButton button_text={<Localize i18n_default_text='Resubmit' />} onClick={onClick} />
);
type TActionButtonProps = {
onClick?: React.MouseEventHandler<HTMLElement>;
platform_name?: string;
auth_status?: TAuthStatus;
should_show_redirect_btn?: boolean;
is_redirected_from_platform: boolean;
};

const verifiedButton = (onClick?: () => void, platform_name?: string) => {
if (auth_status?.needs_poi) {
return (
<VerificationStatusActionButton
to={routes.proof_of_identity}
button_text={<Localize i18n_default_text='Proof of identity' />}
/>
);
}
const createPendingButton = ({
auth_status,
should_show_redirect_btn,
is_redirected_from_platform,
platform_name,
onClick,
}: TActionButtonProps) => {
if (!auth_status?.needs_poi) {
if (should_show_redirect_btn)
return (
<VerificationStatusActionButton
Expand All @@ -96,8 +61,72 @@ export const getPOAStatusMessages = (
/>
);
}
return null;
};
}
if (auth_status?.needs_poi) {
return (
<VerificationStatusActionButton
to={routes.proof_of_identity}
button_text={<Localize i18n_default_text='Proof of identity' />}
/>
);
}
return null;
};

const createVerifiedButton = ({
auth_status,
should_show_redirect_btn,
is_redirected_from_platform,
platform_name,
onClick,
}: TActionButtonProps) => {
if (auth_status?.needs_poi) {
return (
<VerificationStatusActionButton
to={routes.proof_of_identity}
button_text={<Localize i18n_default_text='Proof of identity' />}
/>
);
}
if (should_show_redirect_btn)
return (
<VerificationStatusActionButton
button_text={<Localize i18n_default_text='Back to {{platform_name}}' values={{ platform_name }} />}
onClick={onClick}
/>
);
if (!is_redirected_from_platform) {
return (
<VerificationStatusActionButton
to={routes.root}
button_text={<Localize i18n_default_text='Continue trading' />}
/>
);
}
return null;
};

export const getPOAStatusMessages = (
status: typeof AUTH_STATUS_CODES[keyof typeof AUTH_STATUS_CODES],
auth_status?: TAuthStatus,
should_show_redirect_btn?: boolean
) => {
const is_redirected_from_platform = isNavigationFromP2P() || isNavigationFromDerivGO();

const pendingButton = (props: Pick<TActionButtonProps, 'onClick' | 'platform_name'>) =>
createPendingButton({
...props,
auth_status,
should_show_redirect_btn,
is_redirected_from_platform,
});

const resubmitButton = ({ onClick }: Pick<TActionButtonProps, 'onClick'>) => (
<VerificationStatusActionButton button_text={<Localize i18n_default_text='Resubmit' />} onClick={onClick} />
);

const verifiedButton = (props: Pick<TActionButtonProps, 'onClick' | 'platform_name'>) =>
createVerifiedButton({ ...props, auth_status, should_show_redirect_btn, is_redirected_from_platform });

const titles: Record<typeof status, React.ReactElement> = {
expired: <Localize i18n_default_text='New proof of address is needed' />,
Expand Down Expand Up @@ -152,7 +181,7 @@ export const getPOAStatusMessages = (

const action_buttons: Record<
typeof status,
null | ((onClick?: () => void, platform_name?: string) => JSX.Element | null)
null | ((props: Pick<TActionButtonProps, 'onClick' | 'platform_name'>) => JSX.Element | null)
> = {
expired: resubmitButton,
none: null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ const ProofOfAddressContainer = observer(() => {
status_description={status_content.description}
status_title={status_content.title}
>
{status_content.action_button?.(buttonOnclick(), from_platform.name)}
{status_content.action_button?.({ onClick: buttonOnclick(), platform_name: from_platform.name })}
</VerificationStatus>
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Idv = ({
}
};

const onClick = () => (AUTH_STATUS_CODES.VERIFIED ? handleRequireSubmission : onClickRedirectButton);
const onClick = AUTH_STATUS_CODES.VERIFIED ? handleRequireSubmission : onClickRedirectButton;

if (
[AUTH_STATUS_CODES.REJECTED, AUTH_STATUS_CODES.SUSPECTED, AUTH_STATUS_CODES.EXPIRED].some(
Expand All @@ -64,7 +64,7 @@ const Idv = ({
status_description={status_content.description}
status_title={status_content.title}
>
{status_content.action_button?.(onClick(), from_platform.name)}
{status_content.action_button?.({ onClick, platform_name: from_platform.name })}
</VerificationStatus>
);
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const Onfido = ({
status_description={content.description}
status_title={content.title}
>
{content.action_button?.(onClick, from_platform.name)}
{content.action_button?.({ onClick, platform_name: from_platform.name })}
</VerificationStatus>
);
default:
Expand Down
Loading
Loading