diff --git a/jest.config.js b/jest.config.js index ac7663a7202..f1a6e17453c 100644 --- a/jest.config.js +++ b/jest.config.js @@ -4,7 +4,7 @@ module.exports = { "^.+\\.js$": "/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": "/ts/__mocks__/svgMock.js" diff --git a/jestSetup.js b/jestSetup.js index cf777d2db89..305684b5c56 100644 --- a/jestSetup.js +++ b/jestSetup.js @@ -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 diff --git a/ts/features/it-wallet/saga/index.ts b/ts/features/it-wallet/saga/index.ts index a1ac9830939..6a6d5417b6a 100644 --- a/ts/features/it-wallet/saga/index.ts +++ b/ts/features/it-wallet/saga/index.ts @@ -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. @@ -18,4 +19,5 @@ export function* watchItwSaga(): SagaIterator { yield* fork(watchItwRpSaga); yield* fork(watchItwPresentationSaga); yield* fork(watchItwIssuanceSaga); + yield* fork(watchItwProximitySaga); } diff --git a/ts/features/it-wallet/saga/itwProximitySaga.ts b/ts/features/it-wallet/saga/itwProximitySaga.ts new file mode 100644 index 00000000000..5306a78415b --- /dev/null +++ b/ts/features/it-wallet/saga/itwProximitySaga.ts @@ -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)); +} diff --git a/ts/features/it-wallet/store/actions/itwProximityActions.ts b/ts/features/it-wallet/store/actions/itwProximityActions.ts index 7516cfb9adb..18152c34563 100644 --- a/ts/features/it-wallet/store/actions/itwProximityActions.ts +++ b/ts/features/it-wallet/store/actions/itwProximityActions.ts @@ -9,6 +9,7 @@ import { createAsyncAction, createStandardAction } from "typesafe-actions"; +import { ItWalletError } from "../../utils/itwErrorsUtils"; export enum ProximityManagerStatusEnum { STARTED = "STARTED", @@ -25,31 +26,31 @@ export const startProximityManager = createAsyncAction( "ITW_PROXIMITY_START_REQUEST", "ITW_PROXIMITY_START_SUCCESS", "ITW_PROXIMITY_START_FAILURE" -)(); +)(); export const stopProximityManager = createAsyncAction( "ITW_PROXIMITY_STOP_REQUEST", "ITW_PROXIMITY_STOP_SUCCESS", "ITW_PROXIMITY_STOP_FAILURE" -)(); +)(); export const generateQrCode = createAsyncAction( "ITW_PROXIMITY_QRCODE_REQUEST", "ITW_PROXIMITY_QRCODE_SUCCESS", "ITW_PROXIMITY_QRCODE_FAILURE" -)(); +)(); export const hasBLEFeature = createAsyncAction( "ITW_PROXIMITY_HAS_BLE_FEATURE_REQUEST", "ITW_PROXIMITY_HAS_BLE_FEATURE_SUCCESS", "ITW_PROXIMITY_HAS_BLE_FEATURE_FAILURE" -)(); +)(); export const bleIsEnabled = createAsyncAction( "ITW_BLE_IS_ENABLED_REQUEST", "ITW_BLE_IS_ENABLED_SUCCESS", "ITW_BLE_IS_ENABLED_FAILURE" -)(); +)(); export type ProximityErrorReason = ProximityEvent["type"] | "GENERIC"; diff --git a/ts/features/it-wallet/store/reducers/itwProximityReducer.ts b/ts/features/it-wallet/store/reducers/itwProximityReducer.ts index 4747537d912..2ceec410043 100644 --- a/ts/features/it-wallet/store/reducers/itwProximityReducer.ts +++ b/ts/features/it-wallet/store/reducers/itwProximityReducer.ts @@ -13,13 +13,14 @@ import { proximityManagerStatus, ProximityManagerStatusEnum } from "../actions/itwProximityActions"; +import { ItWalletError } from "../../utils/itwErrorsUtils"; export type ItwProximityState = { - hasBLEFeature: pot.Pot; - isBleEnabled: pot.Pot; - qrCode: pot.Pot; - readingEvent: pot.Pot; - status: pot.Pot; + hasBLEFeature: pot.Pot; + isBleEnabled: pot.Pot; + qrCode: pot.Pot; + readingEvent: pot.Pot; + status: pot.Pot; }; const INITIAL_STATE: ItwProximityState = { diff --git a/ts/features/it-wallet/utils/itwErrorsUtils.ts b/ts/features/it-wallet/utils/itwErrorsUtils.ts index f7c70c763fa..423fc19e28c 100644 --- a/ts/features/it-wallet/utils/itwErrorsUtils.ts +++ b/ts/features/it-wallet/utils/itwErrorsUtils.ts @@ -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 } /**