Skip to content
This repository has been archived by the owner on May 6, 2020. It is now read-only.

Commit

Permalink
Merge pull request #15 from PMET-public/develop
Browse files Browse the repository at this point in the history
Fixed Braintree onSubmit
  • Loading branch information
fnhipster authored Dec 2, 2019
2 parents dd0c0dc + a8b717f commit 159ebbd
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pmet-public/luma-ui",
"version": "1.0.0-beta.8",
"version": "1.0.0-beta.9",
"description": "Luma React UI LIbrary",
"repository": "https://github.com/PMET-public/luma-ui.git",
"author": "Carlos A. Cabrera @fnhhipster",
Expand Down Expand Up @@ -58,7 +58,7 @@
"react": "^16.11.0",
"react-content-loader": "^4.2.2",
"react-dom": "^16.11.0",
"react-hook-form": "^3.24.0",
"react-hook-form": "^3.28.11",
"react-spring": "^8.0.25",
"remixicon": "^1.3.1",
"rimraf": "^2.6.3",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ storiesOf('📦 Components/Checkout/PaymentMethodForm', module).add('Default', (
const braintree = {
authorization: text(
'braintree token',
'eyJ2ZXJzaW9uIjoyLCJhdXRob3JpemF0aW9uRmluZ2VycHJpbnQiOiJleUowZVhBaU9pSktWMVFpTENKaGJHY2lPaUpGVXpJMU5pSXNJbXRwWkNJNklqSXdNVGd3TkRJMk1UWXRjMkZ1WkdKdmVDSXNJbWx6Y3lJNklrRjFkR2g1SW4wLmV5SmxlSEFpT2pFMU56UTVNVEF6Tmpnc0ltcDBhU0k2SW1Gak5tVTNPR0U0TFdFeU9HTXROREJoT1MwNE9EVXdMVFZqWmpNMk56Z3haalkzWXlJc0luTjFZaUk2SW5wcmR6SmpkSEpyYWpjMWJtUjJhMk1pTENKcGMzTWlPaUpCZFhSb2VTSXNJbTFsY21Ob1lXNTBJanA3SW5CMVlteHBZMTlwWkNJNklucHJkekpqZEhKcmFqYzFibVIy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YWx5dGljcy1zYW5kLnNhbmRib3guYnJhaW50cmVlLWFwaS5jb20vemt3MmN0cmtqNzVuZHZrYyJ9LCJ0aHJlZURTZWN1cmVFbmFibGVkIjpmYWxzZSwicGF5cGFsRW5hYmxlZCI6dHJ1ZSwicGF5cGFsIjp7ImRpc3BsYXlOYW1lIjoiTWFnZW50byIsImNsaWVudElkIjpudWxsLCJwcml2YWN5VXJsIjoiaHR0cDovL2V4YW1wbGUuY29tL3BwIiwidXNlckFncmVlbWVudFVybCI6Imh0dHA6Ly9leGFtcGxlLmNvbS90b3MiLCJiYXNlVXJsIjoiaHR0cHM6Ly9hc3NldHMuYnJhaW50cmVlZ2F0ZXdheS5jb20iLCJhc3NldHNVcmwiOiJodHRwczovL2NoZWNrb3V0LnBheXBhbC5jb20iLCJkaXJlY3RCYXNlVXJsIjpudWxsLCJhbGxvd0h0dHAiOnRydWUsImVudmlyb25tZW50Tm9OZXR3b3JrIjp0cnVlLCJlbnZpcm9ubWVudCI6Im9mZmxpbmUiLCJ1bnZldHRlZE1lcmNoYW50IjpmYWxzZSwiYnJhaW50cmVlQ2xpZW50SWQiOiJtYXN0ZXJjbGllbnQzIiwiYmlsbGluZ0FncmVlbWVudHNFbmFibGVkIjp0cnVlLCJtZXJjaGFudEFjY291bnRJZCI6Im1hZ2VudG8iLCJjdXJyZW5jeUlzb0NvZGUiOiJVU0QifSwibWVyY2hhbnRJZCI6InprdzJjdHJrajc1bmR2a2MiLCJ2ZW5tbyI6Im9mZiIsIm1lcmNoYW50QWNjb3VudElkIjoibWFnZW50byJ9'
),
vaultManager: false,
preselectVaultedPaymentMethod: false,
Expand Down
41 changes: 22 additions & 19 deletions src/components/Checkout/PaymentMethodForm/PaymentMethodForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type PaymentMethodFormProps = {
}

type ReducerState = {
instance: Braintree | null
braintreeInstance: Braintree | null
loading: boolean
formError?: string | null
editable: boolean
Expand All @@ -28,11 +28,11 @@ type ReducerState = {

type ReducerActions =
| {
type: 'setInstance'
type: 'setBraintree'
payload: Braintree
}
| {
type: 'unsetInstance'
type: 'unsetBraintree'
}
| {
type: 'setLoader'
Expand All @@ -57,7 +57,7 @@ type ReducerActions =
}

const initialState: ReducerState = {
instance: null,
braintreeInstance: null,
loading: false,
formError: null,
editable: true,
Expand All @@ -66,15 +66,15 @@ const initialState: ReducerState = {

const reducer: Reducer<ReducerState, ReducerActions> = (state, action) => {
switch (action.type) {
case 'setInstance':
case 'setBraintree':
return {
...state,
instance: action.payload,
braintreeInstance: action.payload,
}
case 'unsetInstance':
case 'unsetBraintree':
return {
...state,
instance: null,
braintreeInstance: null,
}
case 'setLoader':
return {
Expand Down Expand Up @@ -102,7 +102,7 @@ const reducer: Reducer<ReducerState, ReducerActions> = (state, action) => {
...state,
paymentInfo: action.payload,
editable: false,
instance: null,
braintreeInstance: null,
}

default:
Expand All @@ -123,7 +123,10 @@ export const PaymentMethodForm: Component<PaymentMethodFormProps> = ({

const containerElem = useRef(null)

const [{ instance, editable, loading, formError, paymentInfo }, dispatch] = useReducer(reducer, initialState)
const [{ braintreeInstance, editable, loading, formError, paymentInfo }, dispatch] = useReducer(
reducer,
initialState
)

const createBraintreeInstance = useCallback(async () => {
if (!containerElem) return
Expand Down Expand Up @@ -153,27 +156,27 @@ export const PaymentMethodForm: Component<PaymentMethodFormProps> = ({
...braintree,
})

dispatch({ type: 'setInstance', payload })
dispatch({ type: 'setBraintree', payload })
} catch (error) {
dispatch({ type: 'unsetInstance' })
dispatch({ type: 'unsetBraintree' })
console.error(error)
}
}, [containerElem, JSON.stringify(braintree)])

useEffect(() => {
if (editable && !instance) createBraintreeInstance()
if (editable && !braintreeInstance) createBraintreeInstance()

return () => {
if (instance) instance.teardown()
if (braintreeInstance) braintreeInstance.teardown()
}
}, [instance])
}, [braintreeInstance])

const handleRequestPaymentMethod = useCallback(async () => {
if (!instance) return
const payload = await instance.requestPaymentMethod()
if (!braintreeInstance) return
const payload = await braintreeInstance.requestPaymentMethod()
dispatch({ type: 'setPaymentInfo', payload })
return onSubmit(payload)
}, [instance])
}, [braintreeInstance, onSubmit])

const handleOnEdit = useCallback(
async (e: Event) => {
Expand All @@ -197,7 +200,7 @@ export const PaymentMethodForm: Component<PaymentMethodFormProps> = ({

dispatch({ type: 'setLoader', payload: false })
},
[handleSubmit, onSubmit]
[handleSubmit, braintreeInstance]
)

return (
Expand Down

0 comments on commit 159ebbd

Please sign in to comment.