Skip to content

Commit

Permalink
chore(IT Wallet): [SIW-778] Add actions and reducer for proximity flow (
Browse files Browse the repository at this point in the history
#5359)

## Short description
This PR adds actions and reducers for proximity flow.

NOTE: this is an experimental proposal to get proximity flow using
redux-saga and could change in the future

## List of changes proposed in this pull request
- Add actions
- Add reducers

## How to test
Static checks should be enough
  • Loading branch information
hevelius authored Dec 21, 2023
1 parent a9242c1 commit 0dc72d9
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 2 deletions.
4 changes: 3 additions & 1 deletion ts/features/it-wallet/store/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { ItwLifecycleActions } from "./itwLifecycleActions";
import { ItwRpActions } from "./itwRpActions";
import { ItwPresentationChecks } from "./new/itwPresentationActions";
import { itwIssuanceActions } from "./new/itwIssuanceActions";
import { ItwProximityActions } from "./itwProximityActions";

/**
* Action types for the IT Wallet feature
Expand All @@ -18,4 +19,5 @@ export type ItWalletActions =
| ItwCredentialsActions
| ItwCieAuthenticationActions
| ItwRpActions
| ItwPresentationChecks;
| ItwPresentationChecks
| ItwProximityActions;
62 changes: 62 additions & 0 deletions ts/features/it-wallet/store/actions/itwProximityActions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { EventData as ProximityEvent } from "@pagopa/io-react-native-proximity";

/**
* Action types and action creator related to Proximity
*/

import {
ActionType,
createAsyncAction,
createStandardAction
} from "typesafe-actions";

export enum ProximityManagerStatusEnum {
STARTED = "STARTED",
STOPPED = "STOPPED"
}

export const proximityManagerStatus = createStandardAction(
"ITW_PROXIMITY_MANAGER_STATUS"
)<{
status: ProximityManagerStatusEnum;
}>();

export const startProximityManager = createAsyncAction(
"ITW_PROXIMITY_START_REQUEST",
"ITW_PROXIMITY_START_SUCCESS",
"ITW_PROXIMITY_START_FAILURE"
)<void, boolean, Error>();

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

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

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>();

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

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

export type ItwProximityActions =
| ActionType<typeof bleIsEnabled>
| ActionType<typeof hasBLEFeature>
| ActionType<typeof startProximityManager>
| ActionType<typeof stopProximityManager>
| ActionType<typeof generateQrCode>
| ActionType<typeof proximityManagerStatus>;
5 changes: 4 additions & 1 deletion ts/features/it-wallet/store/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import itwPresentationReducer, {
ItwPresentationState
} from "./new/itwPresentationReducer";
import itwIssuanceReducer, { ItwIssuanceState } from "./new/itwIssuanceReducer";
import itwProximityReducer, { ItwProximityState } from "./itwProximityReducer";

const CURRENT_REDUX_ITW_STORE_VERSION = 3;

Expand Down Expand Up @@ -69,6 +70,7 @@ export type ItWalletState = {
rpPresentation: ItwRpPresentationState;
presentation: ItwPresentationState;
issuance: ItwIssuanceState;
proximity: ItwProximityState;
};

export type PersistedItWalletState = ItWalletState & PersistPartial;
Expand Down Expand Up @@ -99,7 +101,8 @@ const reducers = combineReducers<ItWalletState, Action>({
rpInit: itwRpInitializationReducer,
rpPresentation: itwRpPresentationReducer,
presentation: itwPresentationReducer,
issuance: itwIssuanceReducer
issuance: itwIssuanceReducer,
proximity: itwProximityReducer
});

const itwReducer = persistReducer<ItWalletState, Action>(
Expand Down
88 changes: 88 additions & 0 deletions ts/features/it-wallet/store/reducers/itwProximityReducer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* A reducer for proximity
*/
import * as pot from "@pagopa/ts-commons/lib/pot";
import { getType } from "typesafe-actions";

import { Action } from "../../../../store/actions/types";
import { GlobalState } from "../../../../store/reducers/types";
import {
hasBLEFeature,
bleIsEnabled,
generateQrCode,
proximityManagerStatus,
ProximityManagerStatusEnum
} from "../actions/itwProximityActions";

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>;
};

const INITIAL_STATE: ItwProximityState = {
hasBLEFeature: pot.none,
isBleEnabled: pot.none,
qrCode: pot.none,
readingEvent: pot.none,
status: pot.none
};

export default function itwProximityReducer(
state: ItwProximityState = INITIAL_STATE,
action: Action
): ItwProximityState {
switch (action.type) {
case getType(proximityManagerStatus):
return {
...state,
status: pot.some(action.payload.status)
};
case getType(hasBLEFeature.success):
return {
...state,
hasBLEFeature: pot.some(action.payload)
};
case getType(hasBLEFeature.failure):
return {
...state,
hasBLEFeature: pot.toError(state.hasBLEFeature, action.payload)
};
case getType(bleIsEnabled.success):
return {
...state,
isBleEnabled: pot.some(action.payload)
};
case getType(bleIsEnabled.failure):
return {
...state,
isBleEnabled: pot.toError(state.isBleEnabled, action.payload)
};
case getType(generateQrCode.success):
return {
...state,
qrCode: pot.some(action.payload)
};

default:
return state;
}
}

// Selectors
export const hasBLEFeatureSelector = (state: GlobalState) =>
state.features.itWallet.proximity.hasBLEFeature;

export const proximityStatusSelector = (state: GlobalState) =>
pot.getOrElse(
pot.map(state.features.itWallet.proximity.status, status => status),
ProximityManagerStatusEnum.STOPPED
);

export const isBleEnabledSelector = (state: GlobalState) =>
state.features.itWallet.proximity.isBleEnabled;

export const qrcodeSelector = (state: GlobalState) =>
state.features.itWallet.proximity.qrCode;

0 comments on commit 0dc72d9

Please sign in to comment.