diff --git a/src/botPage/view/Dialogs/Limits.js b/src/botPage/view/Dialogs/Limits.js index 33332b2b59..e0b87119c5 100644 --- a/src/botPage/view/Dialogs/Limits.js +++ b/src/botPage/view/Dialogs/Limits.js @@ -1,59 +1,122 @@ +import { LiveApi } from 'binary-live-api'; import React, { PureComponent } from 'react'; import PropTypes from 'prop-types'; -import { translate } from '../../../common/i18n'; -import * as style from '../style'; import Dialog from './Dialog'; import { restrictInputCharacter } from '../shared'; +import * as style from '../style'; +import { getToken } from '../../../common/utils/storageManager'; +import { showSpinnerInButton, removeSpinnerInButton, createUrl, translate } from '../../../common/utils/tools'; class LimitsContent extends PureComponent { constructor() { super(); this.state = { error : '', - maxLoss : '', - maxTrades: '', + maxTrades: 0, + maxLosses: null, }; } + submit() { - const maxLoss = parseFloat(this.state.maxLoss || 0); - const maxTrades = parseInt(this.state.maxTrades || 0); this.setState({ error: '' }); - if (maxTrades <= 0 || maxTrades > 100) { - this.setState({ error: 'Maximum number of trades should be between 1 and 100.' }); + + const onSave = () => { + this.props.onSave({ + maxTrades: this.state.maxTrades, + maxLoss : this.state.maxLosses, + }); + }; + + if (!this.state.maxLosses) { + this.updateMaxLosses() + .then(() => { + onSave(); + }) + .catch(() => { + this.setState({ + error: translate( + 'Please set your daily loss limit in the Self-Exclusion Facilities page to allow trading.' + ), + }); + }); return; } - if (!maxLoss) { - this.setState({ error: 'Please enter a Maximum Loss amount greater than zero.' }); + + if (this.state.maxTrades <= 0 || this.state.maxTrades > 100) { + this.setState({ error: translate('Maximum consecutive trades should be between 1 and 100') }); return; } - this.props.onSave({ - maxLoss, - maxTrades, + + onSave(); + } + + updateMaxLosses() { + return new Promise((resolve, reject) => { + const { api } = this.props; + const $startButton = $('#submit-trade-limits'); + const initialText = $startButton.text(); + + showSpinnerInButton($startButton); + + api.getSelfExclusion() + .then(response => { + const { max_losses: maxLosses } = response.get_self_exclusion; + let callback; + + if (maxLosses) { + this.setState({ maxLosses }); + callback = resolve; + } else { + callback = reject; + } + + removeSpinnerInButton($startButton, initialText); + callback(); + }) + .catch(() => { + removeSpinnerInButton($startButton, initialText); + reject(); + }); }); } + componentDidMount() { const cleanupLayout = () => { this.setState({ - maxTrades: '', - maxLoss : '', + maxTrades: 0, error : '', }); }; + + const onDialogOpen = () => { + this.updateMaxLosses().catch(() => {}); + }; + $('#limits-dialog-component').dialog({ + open : onDialogOpen, close : cleanupLayout, autoOpen: false, }); } + onMaxTradeChange(e) { if (restrictInputCharacter({ input: e.target.value, whitelistRegEx: '^[\\d]*$' })) { this.setState({ maxTrades: e.target.value }); } } - onMaxLossChange(e) { - if (restrictInputCharacter({ input: e.target.value, whitelistRegEx: '^\\d*\\.?\\d*$' })) { - this.setState({ maxLoss: e.target.value }); + + getDailyLossesLimit() { + if (this.state.maxLosses) { + const token = $('.account-id') + .first() + .attr('value'); + const tokenObj = getToken(token); + const currency = tokenObj && tokenObj.loginInfo.currency; + return currency ? `${this.state.maxLosses} ${currency}` : `${this.state.maxLosses}`; } + return translate('Not set'); } + render() { return (