Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit bb56c14

Browse files
authored
Merge pull request #175 from hubert-deriv/fixing_endpoint_link_again
2 parents 28a9379 + 166e588 commit bb56c14

File tree

4 files changed

+33
-47
lines changed

4 files changed

+33
-47
lines changed

src/features/Endpoint/Endpoint.tsx

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ interface IEndpointFormValues {
1111
}
1212
const EndPoint = () => {
1313
const default_endpoint = {
14-
app_id: getAppId(false),
14+
app_id: getAppId(),
1515
server_url: OAUTH_URL,
1616
};
1717

@@ -27,19 +27,20 @@ const EndPoint = () => {
2727
},
2828
});
2929

30+
const refreshWhenSubmitted = () => window.location.reload();
31+
3032
const onSubmit = (data: IEndpointFormValues) => {
3133
localStorage.setItem('config.app_id', data.app_id);
3234
localStorage.setItem('config.server_url', data.server_url);
35+
refreshWhenSubmitted();
3336
};
3437

3538
const onResetClicked = () => {
3639
localStorage.removeItem('config.app_id');
3740
localStorage.removeItem('config.server_url');
38-
window.location.reload();
41+
refreshWhenSubmitted();
3942
};
4043

41-
const refreshWhenSubmitted = () => window.location.reload();
42-
4344
const server_url = localStorage.getItem('config.server_url') ?? default_endpoint.server_url;
4445
const app_id = localStorage.getItem('config.app_id') ?? default_endpoint.app_id;
4546
const current_url = `wss://${server_url}/websockets/v3?app_id=${app_id}&l=EN&brand=deriv`;
@@ -107,12 +108,7 @@ const EndPoint = () => {
107108
<div className={styles.urlLink}>{current_url}</div>
108109
</div>
109110
<div className={styles.buttons}>
110-
<Button
111-
type='submit'
112-
color='primary'
113-
onClick={refreshWhenSubmitted}
114-
disabled={Object.keys(errors).length > 0}
115-
>
111+
<Button type='submit' color='primary' disabled={Object.keys(errors).length > 0}>
116112
Submit
117113
</Button>
118114
<span style={{ marginLeft: '1.6rem' }} />

src/features/Endpoint/__tests__/Endpoint.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe('Endpoint', () => {
2525
const app_id = screen.getByPlaceholderText('e.g. 9999');
2626
expect(server).toHaveValue('oauth.deriv.com');
2727

28-
expect(app_id).toHaveValue('35073');
28+
expect(app_id).toHaveValue('35074');
2929
});
3030

3131
it('validate user inputs, and provides error messages for app id field', async () => {

src/utils/__tests__/utils.test.ts

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import * as utils from '@site/src/utils';
2-
import {
3-
LOCALHOST_APP_ID,
4-
VERCEL_DEPLOYMENT_APP_ID,
5-
OAUTH_URL,
6-
DEFAULT_WS_SERVER,
7-
} from '../constants';
2+
import { LOCALHOST_APP_ID, DEFAULT_WS_SERVER, VERCEL_DEPLOYMENT_APP_ID } from '../constants';
83
const {
94
getAccountsFromSearchParams,
105
getAppId,
@@ -33,23 +28,29 @@ describe('Get an object with currency data', () => {
3328
});
3429

3530
describe('Get App ID', () => {
31+
it('By default it should return vercel staging ap id if hostname is not listed', () => {
32+
window.location.hostname = 'asdfasdf';
33+
const appId = getAppId();
34+
expect(appId).toBe('35073');
35+
});
3636
it("Should return 35074 when it's called in localhost environment", () => {
37-
const appId = getAppId(true);
37+
window.location.hostname = 'localhost';
38+
const appId = getAppId();
3839
expect(appId).toBe('35074');
3940
});
4041
it("Should return 35073 when it's called in vercel environment", () => {
4142
window.location.hostname = 'deriv-api-docs.binary.sx';
42-
const appId = getAppId(false);
43+
const appId = getAppId();
4344
expect(appId).toBe('35073');
4445
});
4546
it("Should return 36545 when it's called in staging environment", () => {
4647
window.location.hostname = 'staging-api.deriv.com';
47-
const appId = getAppId(false);
48+
const appId = getAppId();
4849
expect(appId).toBe('36545');
4950
});
5051
it("Should return 36544 when it's called in production environment", () => {
5152
window.location.hostname = 'api.deriv.com';
52-
const appId = getAppId(false);
53+
const appId = getAppId();
5354
expect(appId).toBe('36544');
5455
});
5556
});
@@ -158,17 +159,17 @@ describe('Get Server Config', () => {
158159
});
159160

160161
describe('Given we are in SSR ( no browser object ) ', () => {
161-
it('Should return default ws server url and vercel deployment appId', () => {
162+
it('Should return default ws server url and appId while SSR', () => {
162163
jest.spyOn(utils, 'getIsBrowser').mockReturnValueOnce(false);
163164
const serverConfig = getServerConfig();
164-
expect(serverConfig.appId).toEqual(VERCEL_DEPLOYMENT_APP_ID);
165+
expect(serverConfig.appId).toEqual(LOCALHOST_APP_ID);
165166
expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER);
166167
});
167168
});
169+
168170
describe('Given we are in Browser', () => {
169171
jest.spyOn(utils, 'getIsBrowser').mockReturnValue(true);
170-
171-
it('Should return default ws server url and vercel deployment appId in LOCALHOST ', () => {
172+
it('Should return default ws server url and localhost appId', () => {
172173
const serverConfig = getServerConfig();
173174
expect(serverConfig.appId).toEqual(LOCALHOST_APP_ID);
174175
expect(serverConfig.serverUrl).toEqual(DEFAULT_WS_SERVER);

src/utils/index.ts

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,10 @@ export const isHost = (hostname: string) => {
5656

5757
/**
5858
* @description based on the environment which the project is running we must use different appIds, to get the proper redirect url
59-
* @param isLocalHost {boolean} pass `true` if the project is running on localhost
6059
* @returns {string} proper appId for the project
6160
*/
62-
export const getAppId = (isLocalHost: boolean) => {
63-
if (isLocalHost) return LOCALHOST_APP_ID;
64-
65-
// if not localhost, then one of the following:
61+
export const getAppId = () => {
62+
if (isHost('localhost')) return LOCALHOST_APP_ID;
6663
if (isHost('staging-api.deriv.com')) return STAGING_APP_ID;
6764
if (isHost('deriv-api-docs.binary.sx')) return VERCEL_DEPLOYMENT_APP_ID;
6865
if (isHost('api.deriv.com')) return PRODUCTION_APP_ID;
@@ -127,31 +124,23 @@ export const getServerConfig = () => {
127124
if (isBrowser) {
128125
const config_server_url = localStorage.getItem('config.server_url');
129126
const config_app_id = localStorage.getItem('config.app_id');
130-
if (config_app_id && config_server_url) {
131-
return {
132-
serverUrl: config_server_url,
133-
appId: config_app_id,
134-
oauth: config_server_url,
135-
};
136-
} else {
137-
const isLocalHost = isHost('localhost');
138-
return {
139-
serverUrl: DEFAULT_WS_SERVER,
140-
appId: getAppId(isLocalHost),
141-
oauth: OAUTH_URL,
142-
};
143-
}
127+
128+
return {
129+
serverUrl: config_server_url ?? DEFAULT_WS_SERVER,
130+
appId: config_app_id ?? getAppId(),
131+
oauth: config_server_url ?? OAUTH_URL,
132+
};
144133
} else {
145134
return {
146135
serverUrl: DEFAULT_WS_SERVER,
147-
appId: getAppId(false),
136+
appId: getAppId(),
148137
oauth: OAUTH_URL,
149138
};
150139
}
151140
};
152141

153-
export const generateLoginUrl = (language: string, serverUrl: string, appId: string) => {
154-
return `https://${serverUrl}/oauth2/authorize?app_id=${appId}&l=${language}`;
142+
export const generateLoginUrl = (language: string, oauthUrl: string, appId: string) => {
143+
return `https://${oauthUrl}/oauth2/authorize?app_id=${appId}&l=${language}`;
155144
};
156145

157146
interface IScopesLike {

0 commit comments

Comments
 (0)