Skip to content

Commit

Permalink
refactor: replace hardcoding with constant
Browse files Browse the repository at this point in the history
  • Loading branch information
shontzu-deriv committed Sep 12, 2024
1 parent dbf73d3 commit bb5d16a
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
getCFDPlatformLabel,
CFD_PLATFORMS,
validMT5Password,
hasBritishPound,
} from '@deriv/shared';
import { localize, Localize } from '@deriv/translations';
import { TPlatforms } from '../../Types';
Expand Down Expand Up @@ -65,11 +66,11 @@ const ResetTradingPassword = ({
max_number: max_length,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
errors.password = hasBritishPound(values.password)
? getErrorMessages().special_characters()
: getErrorMessages().password();
} else if (platform === CFD_PLATFORMS.MT5 && !validMT5Password(values.password)) {
errors.password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
errors.password = getErrorMessages().special_characters();
}

return errors;
Expand Down
4 changes: 1 addition & 3 deletions packages/cfd/src/Containers/cfd-password-change.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,7 @@ const CFDPasswordChange = observer(
actions.setFieldError(
'new_password',
// Localize is employed to convert the customized error message since the backend error lacks clarity.
localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
)
getErrorMessages().special_characters()
);
}

Expand Down
11 changes: 7 additions & 4 deletions packages/cfd/src/Containers/cfd-password-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
makeLazyLoader,
moduleLoader,
WS,
hasBritishPound,
} from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { Localize, localize } from '@deriv/translations';
Expand Down Expand Up @@ -739,15 +740,17 @@ const CFDPasswordModal = observer(({ form_error, platform }: TCFDPasswordModalPr
max_number: max_length,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
errors.password = hasBritishPound(values.password)
? getErrorMessages().special_characters()
: getErrorMessages().password();
} else if (
platform === CFD_PLATFORMS.MT5 &&
should_set_trading_password &&
!validMT5Password(values.password)
) {
errors.password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
errors.password = hasBritishPound(values.password)
? getErrorMessages().special_characters()
: getErrorMessages().password();
}
if (values.password?.toLowerCase() === email.toLowerCase()) {
errors.password = localize('Your password cannot be the same as your email address.');
Expand Down
18 changes: 13 additions & 5 deletions packages/cfd/src/Containers/cfd-reset-password-modal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { Formik, FormikHelpers } from 'formik';
import React from 'react';
import { Button, Icon, PasswordMeter, PasswordInput, FormSubmitButton, Loading, Modal, Text } from '@deriv/components';
import { validLength, validPassword, validMT5Password, getErrorMessages, WS, redirectToLogin } from '@deriv/shared';
import {
validLength,
validPassword,
validMT5Password,
getErrorMessages,
WS,
redirectToLogin,
hasBritishPound,
} from '@deriv/shared';
import { localize, Localize, getLanguage } from '@deriv/translations';
import { getMtCompanies, TMtCompanies } from '../Stores/Modules/CFD/Helpers/cfd-config';
import { TResetPasswordIntent, TCFDResetPasswordModal, TError } from './props.types';
Expand Down Expand Up @@ -102,11 +110,11 @@ const CFDResetPasswordModal = observer(({ platform }: TCFDResetPasswordModal) =>
max_number: max_length,
});
} else if (!validPassword(values.new_password)) {
errors.new_password = getErrorMessages().password();
errors.new_password = hasBritishPound
? getErrorMessages().special_characters()
: getErrorMessages().password();
} else if (platform !== CFD_PLATFORMS.DXTRADE && !validMT5Password(values.new_password)) {
errors.new_password = localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
errors.new_password = getErrorMessages().special_characters();
}
if (values.new_password.toLowerCase() === email.toLowerCase()) {
errors.new_password = localize('Your password cannot be the same as your email address.');
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { InlineMessage, Modal, Text, PasswordInput, FormSubmitButton } from '@deriv/components';
import { useMT5SVGEligibleToMigrate } from '@deriv/hooks';
import { CFD_PLATFORMS, WS, validLength, validPassword, getErrorMessages } from '@deriv/shared';
import { CFD_PLATFORMS, WS, validLength, validPassword, getErrorMessages, hasBritishPound } from '@deriv/shared';
import { observer, useStore } from '@deriv/stores';
import { Localize, localize } from '@deriv/translations';
import React from 'react';
Expand Down Expand Up @@ -59,7 +59,9 @@ const MT5MigrationBackSideContent = observer(() => {
max_number: 25,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
errors.password = hasBritishPound(values.password)
? getErrorMessages().special_characters()
: getErrorMessages().password();
}
if (values.password?.toLowerCase() === email.toLowerCase()) {
errors.password = localize('Your password cannot be the same as your email address.');
Expand Down
4 changes: 1 addition & 3 deletions packages/cfd/src/Helpers/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,7 @@ const validatePassword = (password: string): string | undefined => {
} else if (!validPassword(password)) {
return hasBritishPound(password) ? getErrorMessages().special_characters() : getErrorMessages().password();
} else if (!validMT5Password(password)) {
return localize(
'Password must have at least one of these special characters: !&‘’*-“%+.#&(),:;?=@<>\\[]^_{}|~'
);
return getErrorMessages().special_characters();
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@ import React from 'react';
import classNames from 'classnames';
import { Formik, Form, FormikHelpers, FormikErrors } from 'formik';
import { Button, Dialog, PasswordInput, PasswordMeter, Text } from '@deriv/components';
import { redirectToLogin, validPassword, validLength, getErrorMessages, WS, removeActionParam } from '@deriv/shared';
import {
redirectToLogin,
validPassword,
validLength,
getErrorMessages,
WS,
removeActionParam,
hasBritishPound,
} from '@deriv/shared';
import { getLanguage, localize, Localize } from '@deriv/translations';
import { observer, useStore } from '@deriv/stores';
import { TSocketError, TSocketRequest, TSocketResponse } from '@deriv/api/types';
Expand Down Expand Up @@ -85,7 +93,9 @@ const ResetPasswordModal = observer(() => {
max_number: 25,
});
} else if (!validPassword(values.password)) {
errors.password = getErrorMessages().password();
errors.password = hasBritishPound(values.password)
? getErrorMessages().special_characters()
: getErrorMessages().password();
}
return errors;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ export const hasInvalidCharacters = (target_string: string) => /[^\dX\s]/.test(t
export const isFormattedCardNumber = (target_string: string) =>
/(^\d{4})\s(\d{2}X{2})\s(X{4})\s(\d{4}$)/.test(target_string);
export const validFile = (file: File) => file?.type && /(image|application)\/(jpe?g|pdf|png)$/.test(file?.type);
export const validMT5Password = (value: string) => /^(?=.*[!@#$%^&*()+\-=[\]{};':"|,.<>/?_~])[ -~]{8,16}$/.test(value);
export const validMT5Password = (value: string) =>
/^(?=.*[!@#$%^&*()+\-=[\]{};':"|,.<>/?_~])[ -~]{8,16}$/.test(value) && !hasBritishPound(value);

let pre_build_dvrs: TInitPreBuildDVRs, form_error_messages: TFormErrorMessagesTypes;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export type TFormErrorMessagesTypes = Record<
| 'general'
| 'name'
| 'password'
| 'special_characters'
| 'po_box'
| 'phone'
| 'postcode'
Expand Down

0 comments on commit bb5d16a

Please sign in to comment.