Skip to content

Commit

Permalink
feat(storybook): make stories prettier
Browse files Browse the repository at this point in the history
  • Loading branch information
goldcaddy77 committed Jan 15, 2018
1 parent 30f41fc commit d78e113
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 22 deletions.
4 changes: 3 additions & 1 deletion .storybook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { withKnobs } from '@storybook/addon-knobs';
import { setOptions } from '@storybook/addon-options';
import { addDecorator, configure } from '@storybook/react';

import 'milligram/dist/milligram.css';

// See https://github.com/storybooks/storybook/tree/master/addons/options
setOptions({
name: 'Reblocks',
Expand All @@ -12,7 +14,7 @@ setOptions({
addDecorator(withKnobs);

addDecorator(story => {
return <div>{story()}</div>;
return <div style={{ margin: '10px 80px' }}>{story()}</div>;
});

const req = require.context('../src', true, /story.tsx$/);
Expand Down
3 changes: 2 additions & 1 deletion .storybook/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const path = require('path');

const SRC_PATH = path.join(__dirname, '../src');
const CONFIG_PATH = path.join(__dirname, '../.storybook');
const CSS_PATH = path.join(__dirname, '../node_modules/milligram/');

module.exports = {
module: {
Expand All @@ -19,7 +20,7 @@ module.exports = {
{
test: /\.css$/,
use: [{ loader: 'style-loader' }, { loader: 'css-loader' }],
include: [SRC_PATH]
include: [SRC_PATH, CSS_PATH]
},
{
test: /\.md$/,
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Reblocks - Raiblocks Payments for React
# Reblocks - Raiblocks Payments with React

[![styled with prettier](https://img.shields.io/badge/styled_with-prettier-56b3b4.svg)](https://github.com/prettier/prettier)
[![npm version](https://img.shields.io/npm/v/reblocks.svg)](https://www.npmjs.org/package/reblocks)
Expand All @@ -8,6 +8,8 @@
A React wrapper around the [Brainblocks](https://github.com/brainblocks/brainblocks) payment button that makes
it simple to start taking Raiblocks payments in React projects.

Demo: [goldcaddy77.github.io/reblocks](https://goldcaddy77.github.io/reblocks)

## Table of Contents

- [Install](#install)
Expand All @@ -32,7 +34,7 @@ const onSuccess = (data: PaymentResponse) => {
const Button = (
<ReblocksButton
accountId="xrb_3ritoyx4zcixshfbezg4aycb49xbupw9ggink1rfm43tm6uh87t4ifuxg5dm"
amountXrb={200000}
amount={200000}
onPaymentSuccess={onSuccess}
/>
);
Expand All @@ -45,7 +47,7 @@ const Button = (
The `ReblocksButton` takes in the following props:

- `accountId` {string} account to send funds to
- `amountXrb` {string} ammount of `rai` to send (Note: 1 rai = 1/1,000,000 XRB)
- `amount` {string} ammount of `rai` to send (Note: 1 rai = 1/1,000,000 XRB)
- `onPaymentSuccess` {function} function to run on successful payment. This is passed { token: 'TOKEN'}

## Donate
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
"jest-cli": "^21.2.1",
"lint-staged": "^6.0.0",
"markdownlint-cli": "^0.5.0",
"milligram": "^1.3.0",
"prettier": "^1.9.2",
"prompt": "^1.0.0",
"prop-types": "^15.6.0",
Expand Down
101 changes: 90 additions & 11 deletions src/ReblocksButton/ReblocksButton.story.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,97 @@
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 { PaymentResponse, ReblocksButton } from './ReblocksButton';
import { ACCOUNT_ID, PaymentResponse, ReblocksButton } from '../reblocks';

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<HTMLInputElement>) => {
console.log(event.currentTarget.value);
this.setState({ accountId: event.currentTarget.value });
};

onChangeAmount = (event: ChangeEvent<HTMLInputElement>) => {
console.log(event.currentTarget.value);
this.setState({ amount: parseFloat(event.currentTarget.value) });
};

render() {
return (
<form>
<p>
Filling in the form below will dynamically change the payment button with the new account
and amount specified
</p>
<fieldset>
<label>Account ID</label>
<input
type="text"
value={this.state.accountId}
placeholder="Account ID"
onChange={this.onChangeAccountId}
/>
<label>Amount (in rai, 1,000,000 rai = 1 XRB)</label>

<input
type="number"
value={this.state.amount}
placeholder="Amount of rai"
onChange={this.onChangeAmount}
/>
<ReblocksButton
accountId={this.state.accountId}
amount={this.state.amount}
onPaymentSuccess={onSuccess}
/>
</fieldset>
</form>
);
}
}

const onSuccess = (data: PaymentResponse) => {
console.log('Got transaction token', data.token);
action('Payment successful.')();
action(`Transaction ID: ${data.token}`)();
};

storiesOf('ReblocksButton', module).add('Interactive', () => {
return (
<ReblocksButton
accountId="xrb_3ritoyx4zcixshfbezg4aycb49xbupw9ggink1rfm43tm6uh87t4ifuxg5dm"
amountXrb={200000}
onPaymentSuccess={onSuccess}
/>
);
});
storiesOf('ReblocksButton', module)
.add('Small test transaction', () => {
action('Click the button to start payment')();

return (
<div>
<p>The button below will prompt you to send a test transaction of 1000 rai (~2 cents)</p>
<ReblocksButton accountId={ACCOUNT_ID} amount={1000} onPaymentSuccess={onSuccess} />
</div>
);
})
.add('Dynamic Button', () => {
return <PaymentForm />;
})
.add('Buy Dan a 🍺', () => {
return (
<div>
<p>
If you want to help support the project, you can donate a beer's worth of rai using this
form
</p>
<ReblocksButton accountId={ACCOUNT_ID} amount={250000} onPaymentSuccess={onSuccess} />
<p style={{ marginTop: 30 }}>
...or you can donate a different amount to
xrb_3ritoyx4zcixshfbezg4aycb49xbupw9ggink1rfm43tm6uh87t4ifuxg5dm
</p>
</div>
);
});
23 changes: 18 additions & 5 deletions src/ReblocksButton/ReblocksButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export interface PaymentResponse {

export interface Props {
accountId: string;
amountXrb: number;
amount: number;
onPaymentSuccess: (data: PaymentResponse) => void;
}

Expand All @@ -36,26 +36,39 @@ export class ReblocksButton extends React.Component<Props, State> {
}
};

emptyReblocksDiv = () => {
const node = document.getElementById('reblocks');

while (node && node.hasChildNodes()) {
const child = node && node.firstChild;
if (child) {
node.removeChild(child);
}
}
};

renderBrainblocksButton = () => {
this.emptyReblocksDiv();

// tslint:disable-next-line:no-any
(window as any).brainblocks.Button.render(
{
onPayment: this.onPaymentSuccess,
payment: {
amount: this.props.amountXrb,
amount: this.props.amount,
currency: 'rai',
destination: this.props.accountId
}
},
'#raiblocks-button'
'#reblocks'
);
};

componentDidMount() {
componentDidUpdate() {
this.renderBrainblocksButton();
}

render() {
return <div id="raiblocks-button" />;
return <div id="reblocks" />;
}
}
4 changes: 3 additions & 1 deletion src/reblocks.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { ReblocksButton } from './ReblocksButton/ReblocksButton';
export { PaymentResponse, ReblocksButton } from './ReblocksButton/ReblocksButton';

export const ACCOUNT_ID = 'xrb_3ritoyx4zcixshfbezg4aycb49xbupw9ggink1rfm43tm6uh87t4ifuxg5dm';
10 changes: 10 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6012,6 +6012,12 @@ miller-rabin@^4.0.0:
bn.js "^4.0.0"
brorand "^1.0.1"

milligram@^1.3.0:
version "1.3.0"
resolved "https://registry.yarnpkg.com/milligram/-/milligram-1.3.0.tgz#a5d980ef8eaf79337c96a8d7c7e176764931042c"
dependencies:
normalize.css "~5.0.0"

mime-db@~1.30.0:
version "1.30.0"
resolved "https://registry.yarnpkg.com/mime-db/-/mime-db-1.30.0.tgz#74c643da2dd9d6a45399963465b26d5ca7d71f01"
Expand Down Expand Up @@ -6298,6 +6304,10 @@ normalize-url@^2.0.1:
query-string "^5.0.1"
sort-keys "^2.0.0"

normalize.css@~5.0.0:
version "5.0.0"
resolved "https://registry.yarnpkg.com/normalize.css/-/normalize.css-5.0.0.tgz#7cec875ce8178a5333c4de80b68ea9c18b9d7c37"

npm-conf@^1.1.3:
version "1.1.3"
resolved "https://registry.yarnpkg.com/npm-conf/-/npm-conf-1.1.3.tgz#256cc47bd0e218c259c4e9550bf413bc2192aff9"
Expand Down

0 comments on commit d78e113

Please sign in to comment.