-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathReducer.ts
43 lines (39 loc) · 1.77 KB
/
Reducer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import * as WebauthnActions from './Actions';
import {WebauthnActionTypes} from './Constants';
import { ActionType } from 'typesafe-actions/dist/types';
import { WebauthnState } from './Types';
/**
* Reducer that can be used to handle actions dispatched from the `webauthnMiddleware` object. Suggested unless you would like to create your own reducer to handle the actions yourself.
*
*/
export function webauthnReducer(state = {} as WebauthnState, action: ActionType<typeof WebauthnActions>): WebauthnState {
switch (action.type) {
case (WebauthnActionTypes.WEBAUTHN_CREATE_CREDENTIAL_REQUEST):
return {};
case (WebauthnActionTypes.WEBAUTHN_CREATE_CREDENTIAL_SUCCESS):
return {newCredential: action.payload, ...state}
case (WebauthnActionTypes.WEBAUTHN_CREATE_CREDENTIAL_FAILURE):
const newState = {
createCredentialError: action.payload.toString()
}
const mergedState = {...newState, ...state};
const {newCredential, ...stateWithoutCredential} = mergedState;
return stateWithoutCredential;
case (WebauthnActionTypes.WEBAUTHN_GET_ASSERTION_FAILURE): {
const newState = {getAssertionError: action.payload.toString()};
const mergedState = {...newState, ...state};
const {newAssertion, ...stateWithoutAssertion} = mergedState;
return stateWithoutAssertion;
}
case (WebauthnActionTypes.WEBAUTHN_GET_ASSERTION_SUCCESS): {
const newState = {
newAssertion: action.payload,
...state
}
delete newState.getAssertionError;
return newState;
}
default:
return state;
}
}