Skip to content
This repository has been archived by the owner on Apr 28, 2023. It is now read-only.

Commit

Permalink
feat: wait for response before sending another request (#185)
Browse files Browse the repository at this point in the history
* feat: wait for response to send another request

* chore: lint

* refactor: fix test
  • Loading branch information
ImmanuelSegol authored and michael1011 committed May 11, 2019
1 parent 948a1e4 commit c95f361
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 28 deletions.
2 changes: 2 additions & 0 deletions src/__test__/unit/reducers/SwapReducer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ describe('swap reducer', () => {
)
).toEqual({
isFetching: true,
retry: false,
});
});

Expand All @@ -31,6 +32,7 @@ describe('swap reducer', () => {
}
)
).toEqual({
retry: true,
isFetching: false,
swapResponse: 'payload',
});
Expand Down
3 changes: 3 additions & 0 deletions src/reducers/swapReducer.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as actionTypes from '../constants/actions';

export const initialState = {
retry: true,
inSwapMode: false,
webln: null,
isFetching: false,
Expand All @@ -26,13 +27,15 @@ const reducer = (state = initialState, action) => {
case actionTypes.SWAP_REQUEST:
return {
...state,
retry: false,
isFetching: true,
};

case actionTypes.SWAP_RESPONSE:
return {
...state,
isFetching: false,
retry: true,
swapResponse: action.payload,
};

Expand Down
1 change: 1 addition & 0 deletions src/views/swap/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const mapStateToProps = state => ({
webln: state.swapReducer.webln,
swapInfo: state.swapReducer.swapInfo,
swapResponse: state.swapReducer.swapResponse.response,
retrySwap: state.swapReducer.retry,
swapStatus: state.swapReducer.swapStatus,
inSwapMode: state.swapReducer.inSwapMode,
});
Expand Down
58 changes: 30 additions & 28 deletions src/views/swap/swap.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,31 @@ class Swap extends Component {
}
};

onExit = () => {
if (window.confirm('Are you sure you want to exit')) {
this.props.completeSwap();
navigation.navHome();
}
};

startSwap = cb => {
if (this.props.swapInfo.invoice && this.props.retrySwap) {
this.props.startSwap(this.props.swapInfo, cb);
}
};

completeSwap = () => {
this.props.completeSwap();
navigation.navHome();
};

render() {
const {
classes,
webln,
setSwapInvoice,
completeSwap,
swapInfo,
swapResponse,
startSwap,
swapStatus,
} = this.props;
return (
Expand All @@ -57,12 +73,7 @@ class Swap extends Component {
range={4}
stage={1}
id={swapResponse ? swapResponse.id : null}
onExit={() => {
if (window.confirm('Are you sure you want to exit')) {
completeSwap();
navigation.navHome();
}
}}
onExit={this.onExit}
>
<StepsWizard.Steps>
<StepsWizard.Step
Expand Down Expand Up @@ -115,14 +126,14 @@ class Swap extends Component {
num={1}
render={props => (
<Controls
loading={swapStatus.error}
loading={swapStatus.error || !this.props.retrySwap}
text={`Next`}
loadingText={'Invalid invoice'}
onPress={() => {
if (swapInfo.invoice) {
startSwap(swapInfo, props.nextStage);
}
}}
loadingText={
!this.props.retrySwap
? 'Executing swap...'
: 'Invalid invoice'
}
onPress={() => this.startSwap(props.nextStage)}
/>
)}
/>
Expand All @@ -132,9 +143,7 @@ class Swap extends Component {
<Controls
mobile
text={'I have downloaded the refund file'}
onPress={() => {
props.nextStage();
}}
onPress={props.nextStage}
/>
)}
/>
Expand All @@ -149,22 +158,14 @@ class Swap extends Component {
errorText={swapStatus.message}
errorRender={() => {}}
loadingRender={() => <Loading />}
onPress={() => {
props.nextStage();
}}
onPress={props.nextStage}
/>
)}
/>
<StepsWizard.Control
num={4}
render={() => (
<Controls
text={'Swap Again!'}
onPress={() => {
completeSwap();
navigation.navHome();
}}
/>
<Controls text={'Swap Again!'} onPress={this.completeSwap} />
)}
/>
</StepsWizard.Controls>
Expand All @@ -184,6 +185,7 @@ Swap.propTypes = {
completeSwap: PropTypes.func,
setSwapInvoice: PropTypes.func,
onExit: PropTypes.func,
retrySwap: PropTypes.bool,
nextStage: PropTypes.func,
startSwap: PropTypes.func.isRequired,
swapStatus: PropTypes.string.isRequired,
Expand Down

0 comments on commit c95f361

Please sign in to comment.