Skip to content

Commit

Permalink
Merge pull request #19 from ThatBrianDude/CollapseBugFix
Browse files Browse the repository at this point in the history
Fixes bug where reblocks component would collapse after state changes in parent component.
  • Loading branch information
goldcaddy77 authored Aug 3, 2018
2 parents df6ab25 + 3d28085 commit 6afc038
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
71 changes: 71 additions & 0 deletions src/components/ReblocksPayment/CollapseBugTest.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
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;
something?: string;
price: number;
onSuccess: (data: PaymentResponse) => void;
accountId: string;
}

export class CollapseBugTest extends React.Component<Props, State> {

constructor(props: Props) {
super(props);
this.state = { accountId: ACCOUNT_ID, currentXRBPrice: undefined, something: undefined, price: 1, onSuccess: (data: PaymentResponse) => {} };
}

componentDidMount() {
this.loadData('USD');
}

loadData = (currency?: Currency) => {
getCurrentXRBPrice(currency as Currency)
.then(response => {
console.log('parsed json', response);
this.setState({
currentXRBPrice: parseFloat(response.price_local)
});
})
.catch(ex => {
console.log('parsing failed', ex);
});
};

formatUSD = (value?: number): string => {
if (!value) {
return '';
}
return `$${value.toFixed(2)}`;
};

render() {
return (
<div>
<p>
<h2>THIS BUG IS FIXED FOR NOW. TEST FOLLOWING FLOW:</h2>
1. Click the reblocks button to initiate a payment.<br />
2. Click the collapse button which sets the state of something irrelevant to the reblocks component.<br /><br />
<button onClick={() => this.setState({ something: "irrelevant" })}>Collapse reblocks component by setting irrevelant state of parent container.</button><br />
<button onClick={() => this.setState({ price: this.state.price + 1 })}>Update component by setting relevant state (price) of parent container.</button><br />
<button onClick={() => this.setState({ onSuccess: (data: PaymentResponse) => console.log(this.state.price) })}>Update component by setting relevant state (onSuccess) of parent container.</button><br />
</p>
<p>
<ReblocksPayment
accountId={this.state.accountId}
amount={this.state.price}
onPaymentSuccess={this.state.onSuccess}
/>
</p>
</div>
);
}
}
5 changes: 5 additions & 0 deletions src/components/ReblocksPayment/ReblocksPayment.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import * as React from 'react';
import { ACCOUNT_ID } from '../../lib/';

import { BeerForm } from './BeerForm';
import { CollapseBugTest } from './CollapseBugTest';

import { PaymentForm } from './PaymentForm';
import { PaymentResponse, ReblocksPayment } from './ReblocksPayment';

Expand Down Expand Up @@ -43,4 +45,7 @@ storiesOf('ReblocksPayment', module)
})
.add('Dynamic Button', () => {
return <PaymentForm onSuccess={onSuccess} />;
})
.add('Collapse Bug Test', () => {
return <CollapseBugTest onSuccess={onSuccess} />;
});
6 changes: 6 additions & 0 deletions src/components/ReblocksPayment/ReblocksPayment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ export class ReblocksPayment extends React.Component<Props, State> {
);
};

shouldComponentUpdate(nextProps: Readonly<Props>, nextState: Readonly<State>) {
return (this.props.accountId !== nextProps.accountId
|| this.props.amount !== nextProps.amount
|| this.props.onPaymentSuccess !== nextProps.onPaymentSuccess)
}

componentDidUpdate() {
this.renderBrainblocksButton();
}
Expand Down

0 comments on commit 6afc038

Please sign in to comment.