diff --git a/packages/react-shopper-hooks/src/index.ts b/packages/react-shopper-hooks/src/index.ts index b15d0e94..12ebe77c 100644 --- a/packages/react-shopper-hooks/src/index.ts +++ b/packages/react-shopper-hooks/src/index.ts @@ -1,7 +1,6 @@ export * from "./cart" export * from "./event" export * from "./shared" -export * from "./payment-gateway-register" export * from "./store" export * from "./product" export * from "./account" diff --git a/packages/react-shopper-hooks/src/payment-gateway-register/index.ts b/packages/react-shopper-hooks/src/payment-gateway-register/index.ts deleted file mode 100644 index 9a5802b7..00000000 --- a/packages/react-shopper-hooks/src/payment-gateway-register/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -export * from "./payment-gateway-provider" -export * from "./types/payment-gateway-reducer-types" -export * from "./use-payment-gateway" diff --git a/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-provider.tsx b/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-provider.tsx deleted file mode 100644 index fb263b58..00000000 --- a/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-provider.tsx +++ /dev/null @@ -1,23 +0,0 @@ -import React, { createContext, ReactNode, useReducer } from "react" -import { PGRAction, PGRState } from "./types/payment-gateway-reducer-types" -import { pgrReducer } from "./payment-gateway-reducer" - -export const PGRContext = createContext< - { state: PGRState; dispatch: (action: PGRAction) => void } | undefined ->(undefined) - -interface PGRProviderProps { - children: ReactNode -} - -export function PGRProvider({ children }: PGRProviderProps) { - const [state, dispatch] = useReducer(pgrReducer, { - kind: "uninitialized-payment-gateway-register-state", - }) - - return ( - - {children} - - ) -} diff --git a/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-reducer.ts b/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-reducer.ts deleted file mode 100644 index 4c8cdda4..00000000 --- a/packages/react-shopper-hooks/src/payment-gateway-register/payment-gateway-reducer.ts +++ /dev/null @@ -1,16 +0,0 @@ -import { PGRAction, PGRState } from "./types/payment-gateway-reducer-types"; - -export function pgrReducer(state: PGRState, action: PGRAction): PGRState { - switch (action.type) { - case "update-payment-gateway-register": { - const { type, resolvePayment } = action.payload; - return { - kind: "registered-payment-gateway-register-state", - resolvePayment, - type, - }; - } - default: - return state; - } -} diff --git a/packages/react-shopper-hooks/src/payment-gateway-register/types/payment-gateway-reducer-types.ts b/packages/react-shopper-hooks/src/payment-gateway-register/types/payment-gateway-reducer-types.ts deleted file mode 100644 index 0566f3de..00000000 --- a/packages/react-shopper-hooks/src/payment-gateway-register/types/payment-gateway-reducer-types.ts +++ /dev/null @@ -1,58 +0,0 @@ -import { PaymentRequestBody } from "@moltin/sdk"; - -/** --------------------------------- Payment Gateway Register (PGR) State --------------------------------- */ - -interface PaymentGatewayStateBase {} - -/** - * State the PGR is in when no gateways have been registered - */ -export interface UninitializedPGRState extends PaymentGatewayStateBase { - readonly kind: "uninitialized-payment-gateway-register-state"; -} - -export type SupportedGateway = "braintree" | "manual"; - -export type ResolvePaymentFunction = RegisteredPGRState["resolvePayment"]; - -/** - * State the PGR is in when a payment gateway has been registered and is ready to process payments - */ -export interface RegisteredPGRState extends PaymentGatewayStateBase { - readonly kind: "registered-payment-gateway-register-state"; - - /** - * the type of payment gateway that has been registered - */ - readonly type: SupportedGateway; - - /** - * this function when called will resolve the payment specification for the registered gateway - */ - readonly resolvePayment: () => - | Promise - | PaymentRequestBody; -} - -/** - * Representing a state the PGR can be in. - */ -export type PGRState = UninitializedPGRState | RegisteredPGRState; - -/** --------------------------------- Payment Gateway Register (PGR) Actions --------------------------------- */ - -/** - * Register the cart with updated items and meta - */ -export interface UpdatePGRAction { - type: "update-payment-gateway-register"; - payload: { - type: SupportedGateway; - resolvePayment: ResolvePaymentFunction; - }; -} - -/** - * Actions that can be performed to change the state of the cart - */ -export type PGRAction = UpdatePGRAction; diff --git a/packages/react-shopper-hooks/src/payment-gateway-register/use-payment-gateway.ts b/packages/react-shopper-hooks/src/payment-gateway-register/use-payment-gateway.ts deleted file mode 100644 index e109389a..00000000 --- a/packages/react-shopper-hooks/src/payment-gateway-register/use-payment-gateway.ts +++ /dev/null @@ -1,41 +0,0 @@ -import { useContext } from "react" -import { PGRContext } from "./payment-gateway-provider" -import { - PGRAction, - PGRState, - ResolvePaymentFunction, - SupportedGateway, -} from "./types/payment-gateway-reducer-types" - -export function usePaymentGateway(): { - registerGateway: ( - resolvePayment: ResolvePaymentFunction, - type: SupportedGateway, - ) => void - state: PGRState -} { - const context = useContext(PGRContext) - - if (context === undefined) { - throw new Error("usePaymentGateway must be used within a PGRProvider") - } - - const { state, dispatch } = context - - return { - registerGateway: _registerGateway(dispatch), - state, - } -} - -function _registerGateway(dispatch: (action: PGRAction) => void) { - return ( - resolvePayment: ResolvePaymentFunction, - type: SupportedGateway, - ): void => { - dispatch({ - type: "update-payment-gateway-register", - payload: { type, resolvePayment }, - }) - } -} diff --git a/packages/react-shopper-hooks/src/store/store-provider.tsx b/packages/react-shopper-hooks/src/store/store-provider.tsx index dd916959..9b36cc94 100644 --- a/packages/react-shopper-hooks/src/store/store-provider.tsx +++ b/packages/react-shopper-hooks/src/store/store-provider.tsx @@ -1,5 +1,4 @@ import { StoreProviderProps } from "./types/store-context-types" -import { PGRProvider } from "../payment-gateway-register/payment-gateway-provider" import { emitter } from "../event/event-context" import { CartProvider } from "../cart" import React, { createContext } from "react" @@ -23,17 +22,15 @@ export const StoreProvider = ({ return ( - - - {children} - - + + {children} + ) }