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

yashim/feat: add mock server integration #9003

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
655318a
feat: add mock server integration
yashim-deriv Jun 13, 2023
0f472fb
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jun 15, 2023
74fb661
refactor: use session instead of client
yashim-deriv Jun 15, 2023
5e699be
Merge branch 'master' into add_mockserver_support
yashimwong Jun 16, 2023
6b7a15d
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jun 19, 2023
edd412e
Merge branch 'add_mockserver_support' of github.com:yashim-deriv/deri…
yashim-deriv Jun 19, 2023
056efd5
fix: persistent mock server enable state
yashim-deriv Jun 19, 2023
236995c
chore: draft
yashimwong Jun 20, 2023
605ca36
feat: add mock server control panel UI
yashim-deriv Jun 20, 2023
019b142
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashimwong Jun 20, 2023
d191794
feat: add clear all functionality
yashimwong Jun 21, 2023
6c452f2
Merge branch 'master' into add_mockserver_support
yashim-deriv Jun 21, 2023
3aa9933
feat: completed login mock
yashim-deriv Jun 21, 2023
8e1f70e
fix: tests
yashim-deriv Jun 22, 2023
b7ac6aa
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jun 23, 2023
505119b
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jun 23, 2023
dff4cf6
fix: code
yashim-deriv Jun 23, 2023
08b25fe
feat: add feature toggle
yashim-deriv Jun 28, 2023
a8f4de4
feat: end of day commit
yashim-deriv Jun 30, 2023
55fc22e
fix: review comments + tests
yashim-deriv Jul 1, 2023
3b0d2af
Merge branch 'feature/wallets_with_traders_hub' of github.com:binary-…
yashim-deriv Jul 1, 2023
6320130
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jul 3, 2023
9d59884
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jul 3, 2023
8951a7e
chore: used deriv-api
yashim-deriv Jul 3, 2023
4b8a575
Merge branch 'master' of github.com:binary-com/deriv-app into add_moc…
yashim-deriv Jul 4, 2023
9a747c9
fix: typescript error
yashim-deriv Jul 4, 2023
d94d2ec
Merge branch 'feature/wallets_with_traders_hub' of github.com:binary-…
yashim-deriv Jul 4, 2023
7d97189
chore: update package lock
yashim-deriv Jul 4, 2023
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
4 changes: 4 additions & 0 deletions packages/core/src/App/Components/mock-controls/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import MockDialog from './mock-dialog';
import { MockServerProvider, useMockServer } from './mock-server-provider';

export { MockDialog, MockServerProvider, useMockServer };
32 changes: 32 additions & 0 deletions packages/core/src/App/Components/mock-controls/mock-dialog.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
.mock-dialog {
position: absolute;
bottom: 4.6rem;
right: 1rem;
width: 34rem;
border-radius: $BORDER_RADIUS;
padding: 1rem 1.6rem;
background-color: var(--general-main-1);
box-shadow: 0 20px 13px rgba(0, 0, 0, 0.01), 0 4px 3px rgba(0, 0, 0, 0.02);
border: 1px solid var(--general-hover);
z-index: 69420;
yashim-deriv marked this conversation as resolved.
Show resolved Hide resolved

&__title {
margin-bottom: 1.4rem;
}

&__form {
display: flex;
min-height: 40rem;
flex-direction: column;
gap: 2rem;

&--input {
margin-bottom: unset;
}

&--submit-container {
display: flex;
justify-content: end;
}
}
}
37 changes: 37 additions & 0 deletions packages/core/src/App/Components/mock-controls/mock-dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import React from 'react';
import './mock-dialog.scss';
import { Button, Checkbox, Input, Text } from '@deriv/components';
import { useMockServer } from './mock-server-provider';

const MockDialog = () => {
const { active_mock_id, handleSetActiveMockId } = useMockServer();
const [input_mock_id, setInputMockId] = React.useState(active_mock_id);

const saveHandler = () => {
if (input_mock_id) {
handleSetActiveMockId(input_mock_id);
}
};

return (
<div className='mock-dialog'>
<div className='mock-dialog__title'>
<Text weight='bold'>Mock Server Config</Text>
</div>
<div className='mock-dialog__form'>
<Checkbox checked={!!active_mock_id} label='Enable mock server' />
<Input
type='text'
value={input_mock_id}
onChange={e => setInputMockId(e.target.value)}
className='mock-dialog__form--input'
/>
<div className='mock-dialog__form--submit-container'>
<Button onClick={saveHandler}>Save</Button>
</div>
</div>
</div>
);
};

export default MockDialog;
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import React, { ReactNode } from 'react';
import { useWS } from '@deriv/shared';
import { getLanguage } from '@deriv/translations';

export const ACTIVE_MOCK_ID = 'mock_id';

type MockServerData = {
active_mock_id: string;
handleSetActiveMockId: (mock_id: string) => void;
};

const MockServerContext = React.createContext<MockServerData | null>(null);

type MockServerProviderProps = {
children: ReactNode;
};

export const MockServerProvider = ({ children }: MockServerProviderProps) => {
const WS = useWS();
const [active_mock_id, setActiveMockId] = React.useState(localStorage.getItem('mock_id') || '');

React.useEffect(() => {
if (active_mock_id) {
WS.closeAndOpenNewConnection(getLanguage(), active_mock_id);
}
}, [active_mock_id]);

const handleSetActiveMockId = (mock_id: string) => {
if (!mock_id) return;
window.localStorage.setItem(ACTIVE_MOCK_ID, mock_id);
setActiveMockId(mock_id);
};

return (
<MockServerContext.Provider value={{ active_mock_id, handleSetActiveMockId }}>
{children}
</MockServerContext.Provider>
);
};

export const useMockServer = () => {
const mock_server_contect = React.useContext(MockServerContext);
if (!mock_server_contect) {
throw new Error('useUserData must be within UserDataProvider');
yashim-deriv marked this conversation as resolved.
Show resolved Hide resolved
}

return mock_server_contect;
};
40 changes: 22 additions & 18 deletions packages/core/src/App/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { APIProvider } from '@deriv/api';
import { StoreProvider } from '@deriv/stores';
import WS from 'Services/ws-methods';
import { MobxContentProvider } from 'Stores/connect';
import { MockDialog, MockServerProvider } from './Components/mock-controls';
import SmartTraderIFrame from 'Modules/SmartTraderIFrame';
import BinaryBotIFrame from 'Modules/BinaryBotIFrame';
import AppToastMessages from './Containers/app-toast-messages.jsx';
Expand Down Expand Up @@ -81,24 +82,27 @@ const AppWithoutTranslation = ({ root_store }) => {
<MobxContentProvider store={root_store}>
<StoreProvider store={root_store}>
<APIProvider>
<PlatformContainer>
<Header />
<ErrorBoundary>
<AppContents>
{/* TODO: [trader-remove-client-base] */}
<Routes passthrough={platform_passthrough} />
</AppContents>
</ErrorBoundary>
<DesktopWrapper>
<Footer />
</DesktopWrapper>
<ErrorBoundary>
<AppModals />
</ErrorBoundary>
<SmartTraderIFrame />
<BinaryBotIFrame />
<AppToastMessages />
</PlatformContainer>
<MockServerProvider>
<PlatformContainer>
<Header />
<ErrorBoundary>
<AppContents>
{/* TODO: [trader-remove-client-base] */}
<Routes passthrough={platform_passthrough} />
</AppContents>
</ErrorBoundary>
<DesktopWrapper>
<Footer />
</DesktopWrapper>
<ErrorBoundary>
<AppModals />
</ErrorBoundary>
<SmartTraderIFrame />
<BinaryBotIFrame />
<AppToastMessages />
</PlatformContainer>
<MockDialog />
</MockServerProvider>
</APIProvider>
</StoreProvider>
</MobxContentProvider>
Expand Down
34 changes: 28 additions & 6 deletions packages/core/src/_common/base/socket_base.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,12 @@ const BinarySocketBase = (() => {
is_down: false,
};

const getSocketUrl = language =>
`wss://${getSocketURL()}/websockets/v3?app_id=${getAppId()}&l=${language}&brand=${website_name.toLowerCase()}`;
const getSocketUrl = (language, is_mock_server = false) => {
if (is_mock_server) {
return 'ws://127.0.0.1:42069';
}
return `wss://${getSocketURL()}/websockets/v3?app_id=${getAppId()}&l=${language}&brand=${website_name.toLowerCase()}`;
};

const isReady = () => hasReadyState(1);

Expand All @@ -40,10 +44,10 @@ const BinarySocketBase = (() => {
binary_socket.close();
};

const closeAndOpenNewConnection = (language = getLanguage()) => {
const closeAndOpenNewConnection = (language = getLanguage(), mock_id = '') => {
close();
is_switching_socket = true;
openNewConnection(language);
openNewConnection(language, mock_id);
};

const hasReadyState = (...states) => binary_socket && states.some(s => binary_socket.readyState === s);
Expand All @@ -55,14 +59,32 @@ const BinarySocketBase = (() => {
client_store = client;
};

const openNewConnection = (language = getLanguage()) => {
const openNewConnection = (language = getLanguage(), mock_id = '') => {
if (wrong_app_id === getAppId()) return;

if (!is_switching_socket) config.wsEvent('init');

if (isClose()) {
is_disconnect_called = false;
binary_socket = new WebSocket(getSocketUrl(language));
binary_socket = new WebSocket(getSocketUrl(language, mock_id));

if (mock_id) {
const originalSend = DerivAPIBasic.prototype.send;
const originalSubscribe = DerivAPIBasic.prototype.subscribe;

DerivAPIBasic.prototype.send = function (...args) {
const modifiedArgs = [...args];
modifiedArgs[0] = { ...modifiedArgs[0], mock_id };
return originalSend.apply(this, modifiedArgs);
};

DerivAPIBasic.prototype.subscribe = function (...args) {
const modifiedArgs = [...args];
modifiedArgs[0] = { ...modifiedArgs[0], mock_id };
return originalSubscribe.apply(this, modifiedArgs);
};
}

deriv_api = new DerivAPIBasic({
connection: binary_socket,
storage: SocketCache,
Expand Down
Loading