Skip to content

Commit

Permalink
fix(IT Wallet): [SIW-779,SIW-781] Add proximity saga (#5360)
Browse files Browse the repository at this point in the history
### This PR depends on #5359 

## Short description
This PR adds proximity saga handlers.

NOTE: this is an experimental proposal about proximity flow using
redux-saga and could be change in future

## List of changes proposed in this pull request
- Add itwProximitySaga

## How to test
Static checks should be enough
  • Loading branch information
hevelius committed Dec 21, 2023
1 parent 0dc72d9 commit 20080b1
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 12 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module.exports = {
"^.+\\.js$": "<rootDir>/node_modules/react-native/jest/preprocessor.js"
},
transformIgnorePatterns: [
"node_modules/(?!(jest-)?@react-native|react-native|react-navigation|@react-navigation|react-navigation-redux-helpers|react-native-device-info|native-base|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|rn-placeholder|jsbarcode|@pagopa/react-native-cie|react-native-share|jail-monkey|@react-native-community/art|@react-native-community/push-notification-ios|@react-native-camera-roll/camera-roll|@codler|@react-native-community/datetimepicker|remark|unified|bail|is-plain-obj|trough|vfile|unist-util-stringify-position|mdast-util-from-markdown|mdast-util-to-string|micromark|parse-entities|character-entities|mdast-util-to-markdown|zwitch|longest-streak|io-react-native-zendesk|rn-qr-generator|@pagopa/io-react-native-wallet)"
"node_modules/(?!(jest-)?@react-native|react-native|react-navigation|@react-navigation|react-navigation-redux-helpers|react-native-device-info|native-base|native-base-shoutem-theme|@shoutem/animation|@shoutem/ui|rn-placeholder|jsbarcode|@pagopa/react-native-cie|react-native-share|jail-monkey|@react-native-community/art|@react-native-community/push-notification-ios|@react-native-camera-roll/camera-roll|@codler|@react-native-community/datetimepicker|remark|unified|bail|is-plain-obj|trough|vfile|unist-util-stringify-position|mdast-util-from-markdown|mdast-util-to-string|micromark|parse-entities|character-entities|mdast-util-to-markdown|zwitch|longest-streak|io-react-native-zendesk|rn-qr-generator|@pagopa/io-react-native-wallet|cbor-x)"
],
moduleNameMapper: {
"\\.svg": "<rootDir>/ts/__mocks__/svgMock.js"
Expand Down
1 change: 1 addition & 0 deletions jestSetup.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ jest.mock("@react-native-cookies/cookies", jest.fn());
jest.mock("react-native-share", () => jest.fn());
jest.mock("@react-native-clipboard/clipboard", () => mockClipboard);
jest.mock("@react-native-camera-roll/camera-roll", () => mockRNCameraRoll);
jest.mock("@pagopa/io-react-native-proximity", () => jest.fn());

/**
* adds as for documentation suggestion
Expand Down
2 changes: 2 additions & 0 deletions ts/features/it-wallet/saga/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { watchItwActivationSaga } from "./itwActivationSaga";
import { watchItwRpSaga } from "./itwRpSaga";
import { watchItwPresentationSaga } from "./new/itwPresentationSaga";
import { watchItwIssuanceSaga } from "./new/itwIssuanceSaga";
import { watchItwProximitySaga } from "./itwProximitySaga";

/**
* Watcher for any IT wallet related sagas.
Expand All @@ -18,4 +19,5 @@ export function* watchItwSaga(): SagaIterator {
yield* fork(watchItwRpSaga);
yield* fork(watchItwPresentationSaga);
yield* fork(watchItwIssuanceSaga);
yield* fork(watchItwProximitySaga);
}
97 changes: 97 additions & 0 deletions ts/features/it-wallet/saga/itwProximitySaga.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { ProximityManager } from "@pagopa/io-react-native-proximity";
import { SagaIterator } from "redux-saga";
import { call, put, takeLatest } from "typed-redux-saga/macro";
import {
ProximityManagerStatusEnum,
bleIsEnabled,
generateQrCode,
proximityManagerStatus,
startProximityManager,
stopProximityManager
} from "../store/actions/itwProximityActions";
import { ItWalletErrorTypes } from "../utils/itwErrorsUtils";

export function* watchItwProximitySaga(): SagaIterator {
// Trigger a saga on bleIsEnabled to check if BLE is enabled or not
yield* takeLatest(bleIsEnabled.request, checkBleEnablementSaga);

// Start Proximity Manager
yield* takeLatest(
startProximityManager.request,
handleStartProximityManagerSaga
);

// Get QR code request
yield* takeLatest(generateQrCode.request, handleGenerateQrCodeSaga);

// Stop Proximity Manager
yield* takeLatest(
stopProximityManager.request,
handleStopProximityManagerSaga
);
}

// start proximity manager
function* handleStartProximityManagerSaga(): SagaIterator {
try {
yield* call(ProximityManager.start);
yield* put(startProximityManager.success(true));
yield* put(generateQrCode.request());
} catch {
yield* put(
startProximityManager.failure({
code: ItWalletErrorTypes.PROXIMITY_GENERIC_ERROR
})
);
}
}

// generate qr code
function* handleGenerateQrCodeSaga(): SagaIterator {
try {
const qrCode = yield* call(ProximityManager.generateQrCode);
yield* put(generateQrCode.success(qrCode));
yield* put(
proximityManagerStatus({
status: ProximityManagerStatusEnum.STARTED
})
);
} catch {
yield* put(
generateQrCode.failure({
code: ItWalletErrorTypes.PROXIMITY_GENERIC_ERROR
})
);
}
}

// stop proximity manager
function* handleStopProximityManagerSaga(): SagaIterator {
try {
// TODO: remove all listners before stopping the proximity manager
// we need a new method in the proximity manager [SIW-775]
yield* call(ProximityManager.stop);
yield* put(stopProximityManager.success(true));
} catch {
yield* put(
stopProximityManager.failure({
code: ItWalletErrorTypes.PROXIMITY_GENERIC_ERROR
})
);
}
yield* put(
proximityManagerStatus({
status: ProximityManagerStatusEnum.STOPPED
})
);
}

/**
* checks if the ble is enabled. If it is NOT enbled it checks again with a delay
*/
export function* checkBleEnablementSaga(): SagaIterator {
// TODO: to check ble enablement we need to use the proximity manager
// but we need to update the proximity manager with a new method
// to check if ble is enabled [SIW-774]
yield* put(bleIsEnabled.success(true));
}
11 changes: 6 additions & 5 deletions ts/features/it-wallet/store/actions/itwProximityActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
createAsyncAction,
createStandardAction
} from "typesafe-actions";
import { ItWalletError } from "../../utils/itwErrorsUtils";

export enum ProximityManagerStatusEnum {
STARTED = "STARTED",
Expand All @@ -25,31 +26,31 @@ export const startProximityManager = createAsyncAction(
"ITW_PROXIMITY_START_REQUEST",
"ITW_PROXIMITY_START_SUCCESS",
"ITW_PROXIMITY_START_FAILURE"
)<void, boolean, Error>();
)<void, boolean, ItWalletError>();

export const stopProximityManager = createAsyncAction(
"ITW_PROXIMITY_STOP_REQUEST",
"ITW_PROXIMITY_STOP_SUCCESS",
"ITW_PROXIMITY_STOP_FAILURE"
)<void, boolean, Error>();
)<void, boolean, ItWalletError>();

export const generateQrCode = createAsyncAction(
"ITW_PROXIMITY_QRCODE_REQUEST",
"ITW_PROXIMITY_QRCODE_SUCCESS",
"ITW_PROXIMITY_QRCODE_FAILURE"
)<void, string, Error>();
)<void, string, ItWalletError>();

export const hasBLEFeature = createAsyncAction(
"ITW_PROXIMITY_HAS_BLE_FEATURE_REQUEST",
"ITW_PROXIMITY_HAS_BLE_FEATURE_SUCCESS",
"ITW_PROXIMITY_HAS_BLE_FEATURE_FAILURE"
)<void, boolean, Error>();
)<void, boolean, ItWalletError>();

export const bleIsEnabled = createAsyncAction(
"ITW_BLE_IS_ENABLED_REQUEST",
"ITW_BLE_IS_ENABLED_SUCCESS",
"ITW_BLE_IS_ENABLED_FAILURE"
)<void, boolean, Error>();
)<void, boolean, ItWalletError>();

export type ProximityErrorReason = ProximityEvent["type"] | "GENERIC";

Expand Down
11 changes: 6 additions & 5 deletions ts/features/it-wallet/store/reducers/itwProximityReducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,14 @@ import {
proximityManagerStatus,
ProximityManagerStatusEnum
} from "../actions/itwProximityActions";
import { ItWalletError } from "../../utils/itwErrorsUtils";

export type ItwProximityState = {
hasBLEFeature: pot.Pot<boolean, Error>;
isBleEnabled: pot.Pot<boolean, Error>;
qrCode: pot.Pot<string, Error>;
readingEvent: pot.Pot<string, Error>;
status: pot.Pot<ProximityManagerStatusEnum, Error>;
hasBLEFeature: pot.Pot<boolean, ItWalletError>;
isBleEnabled: pot.Pot<boolean, ItWalletError>;
qrCode: pot.Pot<string, ItWalletError>;
readingEvent: pot.Pot<string, ItWalletError>;
status: pot.Pot<ProximityManagerStatusEnum, ItWalletError>;
};

const INITIAL_STATE: ItwProximityState = {
Expand Down
3 changes: 2 additions & 1 deletion ts/features/it-wallet/utils/itwErrorsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ export enum ItWalletErrorTypes {
RP_PRESENTATION_ERROR = "RP_PRESENTATION_ERROR", // not mapped yet
CREDENTIAL_ALREADY_EXISTING_ERROR = "CREDENTIAL_ALREADY_EXISTING_ERROR",
CREDENTIAL_CHECKS_GENERIC_ERROR = "CREDENTIAL_CHECKS_GENERIC_ERROR", // not mapped yet
CREDENTIAL_ADD_ERROR = "CREDENTIAL_ADD_ERROR" // not mapped yet
CREDENTIAL_ADD_ERROR = "CREDENTIAL_ADD_ERROR", // not mapped yet
PROXIMITY_GENERIC_ERROR = "PROXIMITY_GENERIC_ERROR" // not mapped yet
}

/**
Expand Down

0 comments on commit 20080b1

Please sign in to comment.