Skip to content

Commit

Permalink
Merge branch 'master' into aizad/91073/account-signup-modal
Browse files Browse the repository at this point in the history
  • Loading branch information
aizad-deriv authored Mar 27, 2023
2 parents ca2bd77 + 6073091 commit 20d5e7b
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ import React from 'react';
import { render, screen } from '@testing-library/react';
import EmailVerificationEmptyState from '../email-verification-empty-state';
import { TRootStore } from 'Types';
import { useVerifyEmail } from '@deriv/hooks';
import { VerifyEmail } from '@deriv/api-types';
import CashierProviders from '../../../cashier-providers';

const mock_store: DeepPartial<TRootStore> = {
Expand All @@ -13,18 +11,8 @@ const mock_store: DeepPartial<TRootStore> = {
};

describe('EmailVerificationEmptyState', () => {
const verify: ReturnType<typeof useVerifyEmail> = {
is_loading: false,
error: '',
data: {} as VerifyEmail,
counter: 58,
is_counter_running: true,
sent_count: 2,
has_been_sent: true,
send: jest.fn(),
};
test('should disable resend button after sending the request', () => {
render(<EmailVerificationEmptyState verify={verify} />, {
render(<EmailVerificationEmptyState type='reset_password' />, {
wrapper: ({ children }) => <CashierProviders store={mock_store as TRootStore}>{children}</CashierProviders>,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import React from 'react';
import { useVerifyEmail } from '@deriv/hooks';
import { useVerifyEmail, TEmailVerificationType } from '@deriv/hooks';
import { localize } from '@deriv/translations';
import EmptyState from 'Components/empty-state';
import EmailVerificationResendEmptyState from './email-verification-resend-empty-state';
import './email-verification-empty-state.scss';

type TEmailVerificationEmptyStateProps = {
verify: ReturnType<typeof useVerifyEmail>;
type: TEmailVerificationType;
};

const EmailVerificationEmptyState = ({ verify }: TEmailVerificationEmptyStateProps) => {
const EmailVerificationEmptyState = ({ type }: TEmailVerificationEmptyStateProps) => {
const verify = useVerifyEmail(type);

const action = {
label: localize("Didn't receive the email?"),
onClick: verify.send,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const WithdrawalTab = observer(() => {
// match the behavior of the `Withdrawal` page and first inform the user.

if (verify.error && 'code' in verify.error) return <PaymentAgentWithdrawalLocked error={verify.error} />;
if (!verify.is_loading && verify.has_been_sent) return <EmailVerificationEmptyState verify={verify} />;
if (!verify.is_loading && verify.has_been_sent)
return <EmailVerificationEmptyState type={'paymentagent_withdraw'} />;
if (verification_code || payment_agent.is_withdraw)
return <PaymentAgentContainer verification_code={verification_code} />;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const WithdrawalVerificationEmail = observer(() => {

if (verify.error) return <Error error={verify.error} />;

if (verify.has_been_sent) return <EmailVerificationEmptyState verify={verify} />;
if (verify.has_been_sent) return <EmailVerificationEmptyState type={'payment_withdraw'} />;

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/Services/socket-general.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,10 +244,10 @@ const BinarySocketGeneral = (() => {
WS.subscribeBalanceActiveAccount(ResponseHandlers.balanceActiveAccount, client_store.loginid);
};

const authorizeAccount = async response => {
const authorizeAccount = response => {
client_store.responseAuthorize(response);
subscribeBalances();
client_store.setAccountSettings((await WS.storage.getSettings()).get_settings);
WS.storage.getSettings();
WS.getAccountStatus();
WS.storage.payoutCurrencies();
if (!client_store.is_virtual) {
Expand Down
21 changes: 5 additions & 16 deletions packages/core/src/Stores/client-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,6 @@ export default class ClientStore extends BaseStore {

account_limits = {};
self_exclusion = {};
sent_verify_emails_data = {};

local_currency_config = {
currency: '',
Expand Down Expand Up @@ -205,7 +204,6 @@ export default class ClientStore extends BaseStore {
new_email: observable,
account_limits: observable,
self_exclusion: observable,
sent_verify_emails_data: observable,
local_currency_config: observable,
has_cookie_account: observable,
financial_assessment: observable,
Expand Down Expand Up @@ -309,7 +307,6 @@ export default class ClientStore extends BaseStore {
setPreferredLanguage: action.bound,
setCookieAccount: action.bound,
setCFDScore: action.bound,
setSentVerifyEmailsData: action.bound,
updateSelfExclusion: action.bound,
responsePayoutCurrencies: action.bound,
responseAuthorize: action.bound,
Expand Down Expand Up @@ -1180,11 +1177,6 @@ export default class ClientStore extends BaseStore {
LocalStore.setObject(LANGUAGE_KEY, lang);
};

setSentVerifyEmailsData(sent_verify_emails_data) {
this.sent_verify_emails_data = sent_verify_emails_data;
LocalStore.setObject('sent_verify_emails_data', sent_verify_emails_data);
}

setCookieAccount() {
const domain = /deriv\.(com|me)/.test(window.location.hostname) ? deriv_urls.DERIV_HOST_NAME : 'binary.sx';

Expand Down Expand Up @@ -1319,7 +1311,7 @@ export default class ClientStore extends BaseStore {
new_data.landing_company_shortcode = authorize_response.authorize.landing_company_name;
runInAction(() => (client_accounts[client_id] = new_data));
this.setLoginInformation(client_accounts, client_id);
WS.authorized.getSettings().then(get_settings_response => {
WS.authorized.storage.getSettings().then(get_settings_response => {
this.setAccountSettings(get_settings_response.get_settings);
resolve();
});
Expand Down Expand Up @@ -1591,7 +1583,6 @@ export default class ClientStore extends BaseStore {

this.setLoginId(LocalStore.get('active_loginid'));
this.setAccounts(LocalStore.getObject(storage_key));
this.setSentVerifyEmailsData(LocalStore.getObject('sent_verify_emails_data'));
this.setSwitched('');
const client = this.accounts[this.loginid];
// If there is an authorize_response, it means it was the first login
Expand Down Expand Up @@ -1662,16 +1653,14 @@ export default class ClientStore extends BaseStore {
statement: 1,
})
);

if (Object.keys(this.account_settings).length === 0) {
this.setAccountSettings((await WS.authorized.getSettings()).get_settings);
}
if (this.account_settings) this.setPreferredLanguage(this.account_settings.preferred_language);
const account_settings = (await WS.authorized.cache.getSettings()).get_settings;
if (account_settings) this.setPreferredLanguage(account_settings.preferred_language);
await this.fetchResidenceList();
await this.getTwoFAStatus();
if (this.account_settings && !this.account_settings.residence) {
if (account_settings && !account_settings.residence) {
this.root_store.ui.toggleSetResidenceModal(true);
}

await WS.authorized.cache.landingCompany(this.residence).then(this.responseLandingCompany);
if (!this.is_virtual) await this.getLimits();

Expand Down
1 change: 0 additions & 1 deletion packages/core/src/_common/base/socket_cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ const SocketCache = (() => {
contracts_for: { expire: 10 },
exchange_rates: { expire: 60 },
trading_times: { expire: 120 },
get_settings: { expire: 10 },
// TODO: Enable statement and trade table caching once we have UI design for handling
// transitions between cached table and newly added data to table
// statement : { expire: 10 },
Expand Down
22 changes: 5 additions & 17 deletions packages/hooks/src/useVerifyEmail.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useState } from 'react';
import { useWS } from '@deriv/api';
import { useStore } from '@deriv/stores';
import type { TSocketEndpoints } from '@deriv/api/types';
Expand All @@ -9,31 +10,18 @@ export type TEmailVerificationType = TSocketEndpoints['verify_email']['request']

const useVerifyEmail = (type: TEmailVerificationType) => {
const WS = useWS('verify_email');
const counter = useCountdown({ from: RESEND_COUNTDOWN });
const { client } = useStore();
const { setSentVerifyEmailsData, sent_verify_emails_data } = client;
const { last_time_sent_seconds = 0, sent_count = 0 } = sent_verify_emails_data[type] || {};
const time_now_seconds = Math.floor(Date.now() / 1000);
const seconds_left = last_time_sent_seconds + RESEND_COUNTDOWN - time_now_seconds;
const should_not_allow_resend =
last_time_sent_seconds && time_now_seconds < last_time_sent_seconds + RESEND_COUNTDOWN;
const countdown = should_not_allow_resend ? seconds_left : RESEND_COUNTDOWN;
const counter = useCountdown({ from: countdown });

if (!counter.is_running && should_not_allow_resend) {
counter.start();
}
const [sent_count, setSentCount] = useState(0);

const send = () => {
if (!client.email) return;
if (counter.is_running) return;

counter.reset();
counter.start();
const sent_emails_data = {
...sent_verify_emails_data,
[type]: { last_time_sent_seconds: time_now_seconds, sent_count: sent_count + 1 },
};
setSentVerifyEmailsData(sent_emails_data);

setSentCount(old => old + 1);

WS.send({ verify_email: client.email, type });
};
Expand Down
2 changes: 0 additions & 2 deletions packages/stores/src/mockStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ const mock = (): TRootStore => {
residence: '',
responseMt5LoginList: jest.fn(),
responseTradingPlatformAccountsList: jest.fn(),
sent_verify_emails_data: {},
setSentVerifyEmailsData: jest.fn(),
standpoint: {
iom: '',
},
Expand Down
13 changes: 0 additions & 13 deletions packages/stores/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { GetAccountStatus, Authorize, DetailsOfEachMT5Loginid, LogOutResponse, GetLimits } from '@deriv/api-types';
import type { TEmailVerificationType } from '@deriv/hooks';
import type { RouteComponentProps } from 'react-router';

type TAccount = NonNullable<Authorize['account_list']>[0];
Expand Down Expand Up @@ -166,9 +165,7 @@ type TClientStore = {
trading_platform_dxtrade_password_reset: string;
trading_platform_mt5_password_reset: string;
};
sent_verify_emails_data: TSentVerifyEmailsData;
email: string;
setSentVerifyEmailsData: (sent_verify_emails_data: TSentVerifyEmailsData) => void;
setVerificationCode: (code: string, action: string) => void;
updateAccountStatus: () => Promise<void>;
is_authentication_needed: boolean;
Expand All @@ -180,16 +177,6 @@ type TClientStore = {
is_crypto: boolean;
};

type TSentVerifyEmailsData = Partial<
Record<
TEmailVerificationType,
{
last_time_sent_seconds: number;
sent_count: number;
}
>
>;

type TCommonStoreError = {
header: string | JSX.Element;
message: string | JSX.Element;
Expand Down

0 comments on commit 20d5e7b

Please sign in to comment.