This repository has been archived by the owner on Feb 23, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
77 additions
and
0 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
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,8 @@ | ||
export const STATUS = { | ||
PRISTINE: 'pristine', | ||
STARTED: 'started', | ||
FINISHED: 'finished', | ||
ERROR: 'has_error', | ||
FAILED: 'failed', | ||
SUCCESS: 'success', | ||
}; |
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,2 @@ | ||
export { default as useActivePaymentMethod } from './use-active-payment-method'; | ||
export { default as usePaymentEvents } from './use-payment-events'; |
28 changes: 28 additions & 0 deletions
28
assets/js/base/hooks/payment-methods/use-active-payment-method.js
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,28 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useCheckoutContext } from '@woocommerce/base-context/checkout-context'; | ||
import { useEffect } from '@wordpress/element'; | ||
import { getPaymentMethods } from '@woocommerce/blocks-registry'; | ||
|
||
const useActivePaymentMethod = () => { | ||
const { | ||
activePaymentMethod, | ||
setActivePaymentMethod, | ||
} = useCheckoutContext(); | ||
// if payment method has not been set yet, let's set it. | ||
useEffect( () => { | ||
if ( ! activePaymentMethod && activePaymentMethod !== null ) { | ||
const paymentMethods = getPaymentMethods(); | ||
const paymentMethodNames = Object.keys( paymentMethods ); | ||
setActivePaymentMethod( | ||
paymentMethodNames.length > 0 | ||
? paymentMethods[ paymentMethodNames[ 0 ] ].name | ||
: null | ||
); | ||
} | ||
}, [ activePaymentMethod, setActivePaymentMethod ] ); | ||
return { activePaymentMethod, setActivePaymentMethod }; | ||
}; | ||
|
||
export default useActivePaymentMethod; |
38 changes: 38 additions & 0 deletions
38
assets/js/base/hooks/payment-methods/use-payment-events.js
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,38 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useState, useMemo } from '@wordpress/element'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { STATUS } from './constants'; | ||
|
||
const usePaymentEvents = () => { | ||
const { paymentStatus, setPaymentStatus } = useState( STATUS.PRISTINE ); | ||
const dispatch = useMemo( | ||
() => ( { | ||
pristine: () => setPaymentStatus( STATUS.PRISTINE ), | ||
started: () => setPaymentStatus( STATUS.STARTED ), | ||
finished: () => setPaymentStatus( STATUS.FINISHED ), | ||
error: () => setPaymentStatus( STATUS.ERROR ), | ||
failed: () => setPaymentStatus( STATUS.FAILED ), | ||
success: () => setPaymentStatus( STATUS.SUCCESS ), | ||
} ), | ||
[ setPaymentStatus ] | ||
); | ||
const select = useMemo( | ||
() => ( { | ||
isPristine: () => paymentStatus === STATUS.PRISTINE, | ||
isStarted: () => paymentStatus === STATUS.STARTED, | ||
isFinished: () => paymentStatus === STATUS.FINISHED, | ||
hasError: () => paymentStatus === STATUS.ERROR, | ||
hasFailed: () => paymentStatus === STATUS.FAILED, | ||
isSuccessful: () => paymentStatus === STATUS.SUCCESS, | ||
} ), | ||
[ paymentStatus ] | ||
); | ||
return { dispatch, select }; | ||
}; | ||
|
||
export default usePaymentEvents; |