Skip to content

Commit

Permalink
Merge pull request binary-com#276 from utkarsha-deriv/utkarsha/subscr…
Browse files Browse the repository at this point in the history
…ibe-rerender-issue/DERA-496
  • Loading branch information
utkarsha-deriv authored Dec 27, 2023
2 parents 96a8512 + c9ac0cd commit c394743
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion src/features/Apiexplorer/SubscribeRenderer/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@ function SubscribeRenderer<T extends TSocketSubscribableEndpointNames>({
const [is_not_valid, setIsNotValid] = useState(false);

useEffect(() => {
if (error && error.code === 'AuthorizationRequired') {
if (
error &&
typeof error === 'object' &&
'code' in error &&
error.code === 'AuthorizationRequired'
) {
setToggleModal(true);
}
}, [error]);
Expand Down
17 changes: 16 additions & 1 deletion src/hooks/useSubscription/__tests__/useSubscription.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,22 @@ describe('Use WS', () => {
},
});

expect(result.current.error).toEqual({ message: 'this is an error' });
expect(result.current.error).toEqual({
echo_req: {
base_currency: 'USD',
exchange_rates: 1,
req_id: 1,
subscribe: 1,
},
error: {
message: 'this is an error',
},
msg_type: 'exchange_rates',
req_id: 1,
subscription: {
id: '2f39f7e7-d986-33f2-080a-b0ee50cfb85c',
},
});
expect(result.current.is_loading).toBeFalsy();
});
});
9 changes: 2 additions & 7 deletions src/hooks/useSubscription/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ import {
} from '@site/src/configs/websocket/types';
import { useCallback, useState } from 'react';

type TError = {
code?: string;
message?: string;
};

const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) => {
const [is_loading, setIsLoading] = useState(false);
const [is_subscribed, setSubscribed] = useState(false);
const [error, setError] = useState<TError>();
const [error, setError] = useState<unknown>();
const [data, setData] = useState<TSocketResponseData<T>>();
const [full_response, setFullResponse] = useState<TSocketResponse<T>>();
const [subscriber, setSubscriber] = useState<{ unsubscribe?: VoidFunction }>();
Expand All @@ -30,7 +25,7 @@ const useSubscription = <T extends TSocketSubscribableEndpointNames>(name: T) =>
);

const onError = useCallback((response: TSocketResponse<T>) => {
setError(response.error);
setError(response);
setIsLoading(false);
setFullResponse(null);
}, []);
Expand Down

0 comments on commit c394743

Please sign in to comment.