-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(IT Wallet): [SIW-778] Add actions and reducer for proximity flow (
#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
Showing
4 changed files
with
157 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
ts/features/it-wallet/store/actions/itwProximityActions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
ts/features/it-wallet/store/reducers/itwProximityReducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |