Skip to content

Commit

Permalink
feat: bypass toggle restrictions on MobileTokenRequired (#3696)
Browse files Browse the repository at this point in the history
  • Loading branch information
jorelosorio committed Jul 6, 2023
1 parent da7e515 commit 9ea363c
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
30 changes: 20 additions & 10 deletions src/mobile-token/MobileTokenContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ type MobileTokenContextState = {
isLoading: boolean;
isError: boolean;
getSignedToken: () => Promise<string | undefined>;
toggleToken: (tokenId: string) => Promise<boolean>;
toggleToken: (
tokenId: string,
bypassRestrictions: boolean,
) => Promise<boolean>;
retry: () => void;
wipeToken: () => Promise<void>;
fallbackEnabled: boolean;
Expand Down Expand Up @@ -269,15 +272,22 @@ export const MobileTokenContextProvider: React.FC = ({children}) => {
return isInspectable(matchingRemoteToken);
}, [token, remoteTokens]);

const toggleToken = useCallback(async (tokenId: string) => {
try {
const updatedTokens = await tokenService.toggle(tokenId, uuid());
setRemoteTokens(updatedTokens);
return true;
} catch (err) {
return false;
}
}, []);
const toggleToken = useCallback(
async (tokenId: string, bypassRestrictions: boolean) => {
try {
const updatedTokens = await tokenService.toggle(
tokenId,
uuid(),
bypassRestrictions,
);
setRemoteTokens(updatedTokens);
return true;
} catch (err) {
return false;
}
},
[],
);

const getTokenToggleDetails = useCallback(async () => {
try {
Expand Down
14 changes: 11 additions & 3 deletions src/mobile-token/tokenService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ const IsEmulatorHeaderName = 'Atb-Is-Emulator';
export type TokenService = RemoteTokenServiceWithInitiate & {
removeToken: (tokenId: string, traceId: string) => Promise<boolean>;
listTokens: (traceId: string) => Promise<RemoteToken[]>;
toggle: (tokenId: string, traceId: string) => Promise<RemoteToken[]>;
toggle: (
tokenId: string,
traceId: string,
bypassRestrictions: boolean,
) => Promise<RemoteToken[]>;
validate: (
token: ActivatedToken,
secureContainer: string,
Expand Down Expand Up @@ -168,11 +172,15 @@ export const tokenService: TokenService = {
})
.then((res) => res.data.tokens)
.catch(handleError),
toggle: async (tokenId: string, traceId: string) =>
toggle: async (
tokenId: string,
traceId: string,
bypassRestrictions: boolean,
) =>
client
.post<ToggleResponse>(
'/tokens/v4/toggle',
{tokenId},
{tokenId, bypassRestrictions},
{
headers: {
[CorrelationIdHeaderName]: traceId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const SelectTravelTokenScreenComponent = ({onAfterSave}: Props) => {
return;
}
setSaveState({saving: true, error: false});
const success = await toggleToken(selectedToken.id);
const success = await toggleToken(selectedToken.id, false);
if (success) {
onAfterSave();
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ export const Root_ActiveTokenOnPhoneRequiredForFareProductScreen = ({
return;
}
setSaveState({saving: true, error: false});
const success = await toggleToken(selectedToken.id);
const success = await toggleToken(selectedToken.id, true);
if (success && nextScreen) {
navigation.navigate(nextScreen.screen, nextScreen.params as any);
} else {
Expand Down

0 comments on commit 9ea363c

Please sign in to comment.