-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #157 from getAlby/feat/pay-button
feat: add bc-pay-button web component
- Loading branch information
Showing
17 changed files
with
261 additions
and
47 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
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
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
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,44 @@ | ||
import React from 'react'; | ||
import '@getalby/bitcoin-connect'; | ||
import {ComponentProps} from '../types/ComponentProps'; | ||
import {useCoreEvents} from '../hooks/useCoreEvents'; | ||
import {SendPaymentResponse} from '@webbtc/webln-types'; | ||
import {useOnPaid} from '../hooks/useOnPaid'; | ||
|
||
type PayButtonProps = ComponentProps & { | ||
/** | ||
* Bolt 11 invoice to pay | ||
*/ | ||
invoice?: string; | ||
/** | ||
* @param response response of the WebLN send payment call | ||
*/ | ||
onPaid?: (response: SendPaymentResponse) => void; | ||
/** | ||
* Mark that an external payment was made | ||
*/ | ||
payment?: SendPaymentResponse; | ||
|
||
/** | ||
* Listen to when the pay button is clicked. | ||
* This is a good time to fetch an invoice to pay. | ||
*/ | ||
onClick?: () => void; | ||
}; | ||
|
||
export const PayButton: React.FC<PayButtonProps> = (props) => { | ||
useCoreEvents(props); | ||
|
||
const {onPaid, payment} = props; | ||
useOnPaid(onPaid, payment); | ||
|
||
return ( | ||
// eslint-disable-next-line @typescript-eslint/ban-ts-comment | ||
// @ts-ignore | ||
<bc-pay-button | ||
invoice={props.invoice} | ||
preimage={payment?.preimage} | ||
onClick={props.onClick} | ||
/> | ||
); | ||
}; |
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,27 @@ | ||
import {SendPaymentResponse} from '@webbtc/webln-types'; | ||
import React from 'react'; | ||
|
||
export function useOnPaid( | ||
onPaid?: (response: SendPaymentResponse) => void, | ||
payment?: SendPaymentResponse | ||
) { | ||
React.useEffect(() => { | ||
const onPaidEventHandler = (event: Event) => { | ||
onPaid?.((event as CustomEvent).detail); | ||
}; | ||
window.addEventListener('bc:onpaid', onPaidEventHandler); | ||
|
||
if (payment) { | ||
// TODO: remove once bc-send-payment accepts preimage | ||
window.dispatchEvent( | ||
new CustomEvent('bc:onpaid', { | ||
detail: payment, | ||
}) | ||
); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener('bc:onpaid', onPaidEventHandler); | ||
}; | ||
}, [onPaid, payment]); | ||
} |
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
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
Oops, something went wrong.