diff --git a/src/components/ReblocksPayment/BeerForm.tsx b/src/components/ReblocksPayment/BeerForm.tsx new file mode 100644 index 0000000..f7555b7 --- /dev/null +++ b/src/components/ReblocksPayment/BeerForm.tsx @@ -0,0 +1,72 @@ +import * as React from 'react'; + +import { ACCOUNT_ID, Currency, getCurrentXRBPrice } from '../../lib/'; + +import { PaymentResponse, ReblocksPayment } from './ReblocksPayment'; + +export interface Props { + onSuccess: (data: PaymentResponse) => void; +} + +export interface State { + currentXRBPrice?: number; + xrbPerBeer?: number; +} + +export class BeerForm extends React.Component { + BEER_PRICE = 7.0; + + constructor(props: Props) { + super(props); + this.state = { currentXRBPrice: undefined, xrbPerBeer: undefined }; + } + + componentDidMount() { + this.loadData('USD'); + } + + loadData = (currency?: Currency) => { + getCurrentXRBPrice(currency as Currency) + .then(response => { + console.log('parsed json', response); + console.log('xrb per beer', this.BEER_PRICE / parseFloat(response.price_local)); + this.setState({ + currentXRBPrice: parseFloat(response.price_local), + xrbPerBeer: this.BEER_PRICE / parseFloat(response.price_local) + }); + }) + .catch(ex => { + console.log('parsing failed', ex); + }); + }; + + formatUSD = (value?: number): string => { + if (!value) { + return ''; + } + return `$${value.toFixed(2)}`; + }; + + render() { + return ( +
+

+ Current cost of a delicious IPA in Boston: {this.formatUSD(this.BEER_PRICE)} +
+ {this.state.currentXRBPrice && ( + Current cost of XRB in USD: {this.formatUSD(this.state.currentXRBPrice)} + )} +
+ {this.state.xrbPerBeer && XRBs in a beer: {this.state.xrbPerBeer}} +

+

+ +

+
+ ); + } +} diff --git a/src/components/ReblocksPayment/PaymentForm.tsx b/src/components/ReblocksPayment/PaymentForm.tsx new file mode 100644 index 0000000..84d971d --- /dev/null +++ b/src/components/ReblocksPayment/PaymentForm.tsx @@ -0,0 +1,66 @@ +import * as React from 'react'; +// tslint:disable-next-line:no-duplicate-imports +import { ChangeEvent } from 'react'; + +import { ACCOUNT_ID } from '../../lib/'; + +import { PaymentResponse, ReblocksPayment } from './ReblocksPayment'; + +export interface State { + accountId: string; + amount: number; +} + +export interface Props { + onSuccess: (data: PaymentResponse) => void; +} + +export class PaymentForm extends React.Component { + constructor(props: Props) { + super(props); + this.state = { accountId: ACCOUNT_ID, amount: 10000 }; + } + + onChangeAccountId = (event: ChangeEvent) => { + console.log(event.currentTarget.value); + this.setState({ accountId: event.currentTarget.value }); + }; + + onChangeAmount = (event: ChangeEvent) => { + console.log(event.currentTarget.value); + this.setState({ amount: parseFloat(event.currentTarget.value) }); + }; + + render() { + return ( +
+

+ Filling in the form below will dynamically change the payment button with the new account + and amount specified +

+
+ + + + + + +
+
+ ); + } +} diff --git a/src/components/ReblocksPayment/ReblocksPayment.story.tsx b/src/components/ReblocksPayment/ReblocksPayment.story.tsx index 036b929..c6d5116 100644 --- a/src/components/ReblocksPayment/ReblocksPayment.story.tsx +++ b/src/components/ReblocksPayment/ReblocksPayment.story.tsx @@ -2,102 +2,45 @@ import { action } from '@storybook/addon-actions'; import { storiesOf } from '@storybook/react'; import * as React from 'react'; // tslint:disable-next-line:no-duplicate-imports -import { ChangeEvent } from 'react'; import { ACCOUNT_ID } from '../../lib/'; +import { BeerForm } from './BeerForm'; +import { PaymentForm } from './PaymentForm'; import { PaymentResponse, ReblocksPayment } from './ReblocksPayment'; -interface State { - accountId: string; - amount: number; -} - -class PaymentForm extends React.Component<{}, State> { - constructor(props: {}) { - super(props); - this.state = { accountId: ACCOUNT_ID, amount: 10000 }; - } - - onChangeAccountId = (event: ChangeEvent) => { - console.log(event.currentTarget.value); - this.setState({ accountId: event.currentTarget.value }); - }; - - onChangeAmount = (event: ChangeEvent) => { - console.log(event.currentTarget.value); - this.setState({ amount: parseFloat(event.currentTarget.value) }); - }; - - render() { - return ( -
-

- Filling in the form below will dynamically change the payment button with the new account - and amount specified -

-
- - - - - - -
-
- ); - } -} - const onSuccess = (data: PaymentResponse) => { action('Payment successful.')(); action(`Transaction ID: ${data.token}`)(); }; storiesOf('ReblocksPayment', module) - .add('Small test transaction', () => { + .add('Donate 1 XRB', () => { action('Click the button to start payment')(); return ( -
-

The button below will prompt you to send a test transaction of 1000 rai (~2 cents)

- +
+

+ By clicking the button below and making payment to the address, you'll donate 1 XRB to + this project +

+
); }) - .add('Dynamic Button', () => { - return ( -
- -
- ); + .add('Donate a 🍺', () => { + return ; }) - .add('Buy Dan a 🍺', () => { + .add('Small test transaction', () => { + action('Click the button to start payment')(); + return ( -
-

- If you want to help support the project, you can donate a beer's worth of rai using this - form -

- -

- ...or you can donate a different amount to - xrb_3ritoyx4zcixshfbezg4aycb49xbupw9ggink1rfm43tm6uh87t4ifuxg5dm -

+
+

The button below will prompt you to send a test transaction of 1000 rai (~2 cents)

+
); + }) + .add('Dynamic Button', () => { + return ; });