-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[NO CHANGELOG][Add Funds Widget] AddFundContext (#2117)
- Loading branch information
1 parent
3975c96
commit 9ec60ca
Showing
2 changed files
with
124 additions
and
13 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
72 changes: 72 additions & 0 deletions
72
packages/checkout/widgets-lib/src/widgets/add-funds/context/AddFundsContext.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,72 @@ | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { createContext } from 'react'; | ||
import { Checkout } from '@imtbl/checkout-sdk'; | ||
|
||
export interface AddFundsState { | ||
checkout: Checkout | null; | ||
provider: Web3Provider | null; | ||
} | ||
|
||
export const initialAddFundsState: AddFundsState = { | ||
checkout: null, | ||
provider: null, | ||
}; | ||
|
||
export interface AddFundsContextState { | ||
addFundsState: AddFundsState; | ||
addFundsDispatch: React.Dispatch<AddFundsAction>; | ||
} | ||
|
||
export interface AddFundsAction { | ||
payload: ActionPayload; | ||
} | ||
|
||
type ActionPayload = | ||
| SetCheckoutPayload | ||
| SetProviderPayload; | ||
|
||
export enum AddFundsActions { | ||
SET_CHECKOUT = 'SET_CHECKOUT', | ||
SET_PROVIDER = 'SET_PROVIDER', | ||
} | ||
|
||
export interface SetCheckoutPayload { | ||
type: AddFundsActions.SET_CHECKOUT; | ||
checkout: Checkout; | ||
} | ||
|
||
export interface SetProviderPayload { | ||
type: AddFundsActions.SET_PROVIDER; | ||
provider: Web3Provider; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/naming-convention | ||
export const AddFundsContext = createContext<AddFundsContextState>({ | ||
addFundsState: initialAddFundsState, | ||
addFundsDispatch: () => { | ||
}, | ||
}); | ||
|
||
AddFundsContext.displayName = 'AddFundsContext'; | ||
|
||
export type Reducer<S, A> = (prevState: S, action: A) => S; | ||
|
||
export const addFundsReducer: Reducer<AddFundsState, AddFundsAction> = ( | ||
state: AddFundsState, | ||
action: AddFundsAction, | ||
) => { | ||
switch (action.payload.type) { | ||
case AddFundsActions.SET_CHECKOUT: | ||
return { | ||
...state, | ||
checkout: action.payload.checkout, | ||
}; | ||
case AddFundsActions.SET_PROVIDER: | ||
return { | ||
...state, | ||
provider: action.payload.provider, | ||
}; | ||
default: | ||
return state; | ||
} | ||
}; |