2017-12-11 17:44:53 +00:00
|
|
|
import { TChangeStepSwap, TInitSwap } from 'actions/swap';
|
|
|
|
import { NormalizedBityRates, NormalizedOptions, SwapInput } from 'reducers/swap/types';
|
2017-09-25 02:06:28 +00:00
|
|
|
import SimpleButton from 'components/ui/SimpleButton';
|
2017-12-11 17:44:53 +00:00
|
|
|
import bityConfig, { generateKindMax, generateKindMin, WhitelistedCoins } from 'config/bity';
|
2017-07-31 23:14:30 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import translate from 'translations';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { combineAndUpper } from 'utils/formatters';
|
2017-10-04 03:21:10 +00:00
|
|
|
import { Dropdown } from 'components/ui';
|
2017-11-14 18:37:56 +00:00
|
|
|
import Spinner from 'components/ui/Spinner';
|
2017-12-30 20:29:04 +00:00
|
|
|
import intersection from 'lodash/intersection';
|
|
|
|
import without from 'lodash/without';
|
2017-12-11 17:44:53 +00:00
|
|
|
import './CurrencySwap.scss';
|
2017-09-25 02:06:28 +00:00
|
|
|
|
|
|
|
export interface StateProps {
|
2017-12-11 17:44:53 +00:00
|
|
|
bityRates: NormalizedBityRates;
|
|
|
|
options: NormalizedOptions;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export interface ActionProps {
|
|
|
|
changeStepSwap: TChangeStepSwap;
|
2017-12-11 17:44:53 +00:00
|
|
|
initSwap: TInitSwap;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
|
|
|
disabled: boolean;
|
2017-12-11 17:44:53 +00:00
|
|
|
origin: SwapInput;
|
|
|
|
destination: SwapInput;
|
|
|
|
originKindOptions: WhitelistedCoins[];
|
|
|
|
destinationKindOptions: WhitelistedCoins[];
|
2017-11-14 18:37:56 +00:00
|
|
|
originErr: string;
|
|
|
|
destinationErr: string;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
2017-09-25 03:29:49 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
type Props = StateProps & ActionProps;
|
|
|
|
|
|
|
|
export default class CurrencySwap extends Component<Props, State> {
|
2017-09-25 02:06:28 +00:00
|
|
|
public state = {
|
2017-07-31 23:14:30 +00:00
|
|
|
disabled: true,
|
2017-12-11 17:44:53 +00:00
|
|
|
origin: { id: 'BTC', amount: NaN } as SwapInput,
|
|
|
|
destination: { id: 'ETH', amount: NaN } as SwapInput,
|
|
|
|
originKindOptions: ['BTC', 'ETH'] as WhitelistedCoins[],
|
|
|
|
destinationKindOptions: ['ETH'] as WhitelistedCoins[],
|
2017-11-14 18:37:56 +00:00
|
|
|
originErr: '',
|
|
|
|
destinationErr: ''
|
2017-07-31 23:14:30 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public componentDidUpdate(prevProps: Props, prevState: State) {
|
|
|
|
const { origin, destination } = this.state;
|
|
|
|
const { options } = this.props;
|
|
|
|
if (origin !== prevState.origin) {
|
|
|
|
this.setDisabled(origin, destination);
|
|
|
|
}
|
|
|
|
if (options.allIds !== prevProps.options.allIds) {
|
|
|
|
const originKindOptions: WhitelistedCoins[] = intersection<any>(
|
|
|
|
options.allIds,
|
|
|
|
this.state.originKindOptions
|
2017-11-14 18:37:56 +00:00
|
|
|
);
|
2017-12-11 17:44:53 +00:00
|
|
|
const destinationKindOptions: WhitelistedCoins[] = without<any>(options.allIds, origin.id);
|
|
|
|
this.setState({
|
|
|
|
originKindOptions,
|
|
|
|
destinationKindOptions
|
|
|
|
});
|
2017-11-14 18:37:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public getMinMax = (kind: WhitelistedCoins) => {
|
|
|
|
let min;
|
|
|
|
let max;
|
2017-07-31 23:14:30 +00:00
|
|
|
if (kind !== 'BTC') {
|
2017-12-11 17:44:53 +00:00
|
|
|
const bityPairRate = this.props.bityRates.byId['BTC' + kind].rate;
|
|
|
|
min = generateKindMin(bityPairRate, kind);
|
|
|
|
max = generateKindMax(bityPairRate, kind);
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
2017-12-11 17:44:53 +00:00
|
|
|
min = bityConfig.BTCMin;
|
|
|
|
max = bityConfig.BTCMax;
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
2017-12-11 17:44:53 +00:00
|
|
|
return { min, max };
|
2017-07-31 23:14:30 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public isMinMaxValid = (amount: number, kind: WhitelistedCoins) => {
|
|
|
|
const rate = this.getMinMax(kind);
|
|
|
|
const higherThanMin = amount >= rate.min;
|
|
|
|
const lowerThanMax = amount <= rate.max;
|
|
|
|
return higherThanMin && lowerThanMax;
|
2017-07-31 23:14:30 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public setDisabled(origin: SwapInput, destination: SwapInput) {
|
|
|
|
const amountsValid = origin.amount && destination.amount;
|
|
|
|
const minMaxValid = this.isMinMaxValid(origin.amount, origin.id);
|
2017-07-31 23:14:30 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
const disabled = !(amountsValid && minMaxValid);
|
2017-07-31 23:14:30 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
const createErrString = (kind: WhitelistedCoins, amount: number) => {
|
|
|
|
const rate = this.getMinMax(kind);
|
|
|
|
let errString;
|
|
|
|
if (amount > rate.max) {
|
|
|
|
errString = `Maximum ${rate.max} ${kind}`;
|
|
|
|
} else {
|
|
|
|
errString = `Minimum ${rate.min} ${kind}`;
|
|
|
|
}
|
|
|
|
return errString;
|
|
|
|
};
|
2017-11-14 18:37:56 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
const showError = disabled && amountsValid;
|
|
|
|
const originErr = showError ? createErrString(origin.id, origin.amount) : '';
|
|
|
|
const destinationErr = showError ? createErrString(destination.id, destination.amount) : '';
|
2017-11-14 18:37:56 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
this.setState({
|
|
|
|
disabled,
|
|
|
|
originErr,
|
|
|
|
destinationErr
|
|
|
|
});
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public onClickStartSwap = () => {
|
2017-12-11 17:44:53 +00:00
|
|
|
const { origin, destination } = this.state;
|
|
|
|
const { changeStepSwap, initSwap } = this.props;
|
|
|
|
initSwap({ origin, destination });
|
|
|
|
changeStepSwap(2);
|
2017-07-31 23:14:30 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public setOriginAndDestinationToInitialVal = () => {
|
|
|
|
this.setState({
|
|
|
|
origin: { ...this.state.origin, amount: NaN },
|
|
|
|
destination: { ...this.state.destination, amount: NaN }
|
|
|
|
});
|
2017-07-31 23:14:30 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public updateOriginAmount = (origin: SwapInput, destination: SwapInput, amount: number) => {
|
|
|
|
if (amount || amount === 0) {
|
|
|
|
const pairName = combineAndUpper(origin.id, destination.id);
|
|
|
|
const bityRate = this.props.bityRates.byId[pairName].rate;
|
|
|
|
const destinationAmount = amount * bityRate;
|
|
|
|
this.setState({
|
|
|
|
origin: { ...this.state.origin, amount },
|
|
|
|
destination: { ...this.state.destination, amount: destinationAmount }
|
|
|
|
});
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
2017-12-11 17:44:53 +00:00
|
|
|
this.setOriginAndDestinationToInitialVal();
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
public updateDestinationAmount = (origin: SwapInput, destination: SwapInput, amount: number) => {
|
|
|
|
if (amount || amount === 0) {
|
|
|
|
const pairNameReversed = combineAndUpper(destination.id, origin.id);
|
|
|
|
const bityRate = this.props.bityRates.byId[pairNameReversed].rate;
|
|
|
|
const originAmount = amount * bityRate;
|
|
|
|
this.setState({
|
|
|
|
origin: { ...this.state.origin, amount: originAmount },
|
|
|
|
destination: {
|
|
|
|
...this.state.destination,
|
|
|
|
amount
|
|
|
|
}
|
|
|
|
});
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
2017-12-11 17:44:53 +00:00
|
|
|
this.setOriginAndDestinationToInitialVal();
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-19 22:46:34 +00:00
|
|
|
public onChangeAmount = (event: React.FormEvent<HTMLInputElement>) => {
|
|
|
|
const type = event.currentTarget.id;
|
2017-12-11 17:44:53 +00:00
|
|
|
const { origin, destination } = this.state;
|
2017-12-19 22:46:34 +00:00
|
|
|
const amount = parseFloat(event.currentTarget.value);
|
2017-12-11 17:44:53 +00:00
|
|
|
type === 'origin-swap-input'
|
|
|
|
? this.updateOriginAmount(origin, destination, amount)
|
|
|
|
: this.updateDestinationAmount(origin, destination, amount);
|
|
|
|
};
|
|
|
|
|
|
|
|
public onChangeOriginKind = (newOption: WhitelistedCoins) => {
|
|
|
|
const { origin, destination, destinationKindOptions } = this.state;
|
|
|
|
const newDestinationAmount = () => {
|
|
|
|
const pairName = combineAndUpper(destination.id, origin.id);
|
|
|
|
const bityRate = this.props.bityRates.byId[pairName].rate;
|
|
|
|
return bityRate * origin.amount;
|
|
|
|
};
|
|
|
|
this.setState({
|
|
|
|
origin: { ...origin, id: newOption },
|
|
|
|
destination: {
|
|
|
|
id: newOption === destination.id ? origin.id : destination.id,
|
|
|
|
amount: newDestinationAmount() ? newDestinationAmount() : destination.amount
|
|
|
|
},
|
|
|
|
destinationKindOptions: without([...destinationKindOptions, origin.id], newOption)
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
public onChangeDestinationKind = (newOption: WhitelistedCoins) => {
|
|
|
|
const { origin, destination } = this.state;
|
|
|
|
const newOriginAmount = () => {
|
|
|
|
const pairName = combineAndUpper(newOption, origin.id);
|
|
|
|
const bityRate = this.props.bityRates.byId[pairName].rate;
|
|
|
|
return bityRate * destination.amount;
|
|
|
|
};
|
|
|
|
this.setState({
|
|
|
|
origin: {
|
|
|
|
...origin,
|
|
|
|
amount: newOriginAmount() ? newOriginAmount() : origin.amount
|
|
|
|
},
|
|
|
|
destination: { ...destination, id: newOption }
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public render() {
|
2017-12-11 17:44:53 +00:00
|
|
|
const { bityRates } = this.props;
|
2017-07-31 23:14:30 +00:00
|
|
|
const {
|
2017-12-11 17:44:53 +00:00
|
|
|
origin,
|
|
|
|
destination,
|
2017-11-14 18:37:56 +00:00
|
|
|
originKindOptions,
|
2017-12-11 17:44:53 +00:00
|
|
|
destinationKindOptions,
|
|
|
|
originErr,
|
|
|
|
destinationErr
|
|
|
|
} = this.state;
|
2017-11-14 18:37:56 +00:00
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
const OriginKindDropDown = Dropdown as new () => Dropdown<any>;
|
|
|
|
const DestinationKindDropDown = Dropdown as new () => Dropdown<typeof destination.id>;
|
|
|
|
const pairName = combineAndUpper(origin.id, destination.id);
|
|
|
|
const bityLoaded = bityRates.byId[pairName] ? bityRates.byId[pairName].id : false;
|
2017-07-31 23:14:30 +00:00
|
|
|
return (
|
v3 Style Import (#151)
* Convert bootstrap to sass instead of checked in and less
* Darken body, adjust header.
* First pass at tab styles, each tab will need a lot of individual love tho.
* Update footer to main site content, improve responsiveness.
* Missing key added.
* Fix dropdowns.
* Convert GenerateWallet HTML over, still needs styling.
* Send form.
* Current rates styled.
* CurrencySwap form styles.
* SwapInfoHeader styled.
* Finish up swap restyling, minor usability improvements for mobile.
* Fix up notifications / alert customizations
* Import v3 variables.
* Fix notification spacing.
* Align input height base with buttons.
* Revert height base, add additional bootstrap overrides.
* Grid overrides.
* Move overrides to their own folder. Adjust naming.
* Fix inconsistencies.
* Style generate wallet pt 1.
* Style generate wallet pt 2
* Style generate wallet pt 3
* Fix swap
* Added some missing overries, fixed the fallout.
* Remove header text, indicate alpha version.
* Fix radio / checkbox weights.
* Bind => arrow
* Convert simpledropdown to proper form select, instead of weirdly implemented nonfuncitoning dropdown.
* Fix token balances buttons, footr icons.
2017-09-05 19:52:01 +00:00
|
|
|
<article className="CurrencySwap">
|
2017-09-25 02:06:28 +00:00
|
|
|
<h1 className="CurrencySwap-title">{translate('SWAP_init_1')}</h1>
|
2017-11-14 18:37:56 +00:00
|
|
|
{bityLoaded ? (
|
|
|
|
<div className="form-inline CurrencySwap-inner-wrap">
|
|
|
|
<div className="CurrencySwap-input-group">
|
2017-12-14 05:08:45 +00:00
|
|
|
{originErr && <span className="CurrencySwap-error-message">{originErr}</span>}
|
2017-11-14 18:37:56 +00:00
|
|
|
<input
|
2017-12-11 17:44:53 +00:00
|
|
|
id="origin-swap-input"
|
2017-11-14 18:37:56 +00:00
|
|
|
className={`CurrencySwap-input form-control ${
|
2017-12-11 17:44:53 +00:00
|
|
|
String(origin.amount) !== '' && this.isMinMaxValid(origin.amount, origin.id)
|
2017-11-14 18:37:56 +00:00
|
|
|
? 'is-valid'
|
|
|
|
: 'is-invalid'
|
|
|
|
}`}
|
|
|
|
type="number"
|
|
|
|
placeholder="Amount"
|
2017-12-11 17:44:53 +00:00
|
|
|
value={isNaN(origin.amount) ? '' : origin.amount}
|
|
|
|
onChange={this.onChangeAmount}
|
2017-11-14 18:37:56 +00:00
|
|
|
/>
|
|
|
|
<div className="CurrencySwap-dropdown">
|
|
|
|
<OriginKindDropDown
|
2017-12-11 17:44:53 +00:00
|
|
|
ariaLabel={`change origin kind. current origin kind ${origin.id}`}
|
2017-11-14 18:37:56 +00:00
|
|
|
options={originKindOptions}
|
2017-12-11 17:44:53 +00:00
|
|
|
value={origin.id}
|
|
|
|
onChange={this.onChangeOriginKind}
|
2017-11-14 18:37:56 +00:00
|
|
|
color="default"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<h1 className="CurrencySwap-divider">{translate('SWAP_init_2')}</h1>
|
|
|
|
<div className="CurrencySwap-input-group">
|
2017-12-14 05:08:45 +00:00
|
|
|
{destinationErr && (
|
|
|
|
<span className="CurrencySwap-error-message">{destinationErr}</span>
|
|
|
|
)}
|
2017-11-14 18:37:56 +00:00
|
|
|
<input
|
2017-12-11 17:44:53 +00:00
|
|
|
id="destination-swap-input"
|
2017-11-14 18:37:56 +00:00
|
|
|
className={`CurrencySwap-input form-control ${
|
2017-12-11 17:44:53 +00:00
|
|
|
String(destination.amount) !== '' && this.isMinMaxValid(origin.amount, origin.id)
|
2017-11-14 18:37:56 +00:00
|
|
|
? 'is-valid'
|
|
|
|
: 'is-invalid'
|
|
|
|
}`}
|
|
|
|
type="number"
|
|
|
|
placeholder="Amount"
|
2017-12-11 17:44:53 +00:00
|
|
|
value={isNaN(destination.amount) ? '' : destination.amount}
|
|
|
|
onChange={this.onChangeAmount}
|
2017-11-14 18:37:56 +00:00
|
|
|
/>
|
|
|
|
<div className="CurrencySwap-dropdown">
|
|
|
|
<DestinationKindDropDown
|
2017-12-11 17:44:53 +00:00
|
|
|
ariaLabel={`change destination kind. current destination kind ${destination.id}`}
|
2017-11-14 18:37:56 +00:00
|
|
|
options={destinationKindOptions}
|
2017-12-11 17:44:53 +00:00
|
|
|
value={destination.id}
|
|
|
|
onChange={this.onChangeDestinationKind}
|
2017-11-14 18:37:56 +00:00
|
|
|
color="default"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
) : (
|
|
|
|
<Spinner />
|
|
|
|
)}
|
v3 Style Import (#151)
* Convert bootstrap to sass instead of checked in and less
* Darken body, adjust header.
* First pass at tab styles, each tab will need a lot of individual love tho.
* Update footer to main site content, improve responsiveness.
* Missing key added.
* Fix dropdowns.
* Convert GenerateWallet HTML over, still needs styling.
* Send form.
* Current rates styled.
* CurrencySwap form styles.
* SwapInfoHeader styled.
* Finish up swap restyling, minor usability improvements for mobile.
* Fix up notifications / alert customizations
* Import v3 variables.
* Fix notification spacing.
* Align input height base with buttons.
* Revert height base, add additional bootstrap overrides.
* Grid overrides.
* Move overrides to their own folder. Adjust naming.
* Fix inconsistencies.
* Style generate wallet pt 1.
* Style generate wallet pt 2
* Style generate wallet pt 3
* Fix swap
* Added some missing overries, fixed the fallout.
* Remove header text, indicate alpha version.
* Fix radio / checkbox weights.
* Bind => arrow
* Convert simpledropdown to proper form select, instead of weirdly implemented nonfuncitoning dropdown.
* Fix token balances buttons, footr icons.
2017-09-05 19:52:01 +00:00
|
|
|
|
|
|
|
<div className="CurrencySwap-submit">
|
2017-07-31 23:14:30 +00:00
|
|
|
<SimpleButton
|
|
|
|
onClick={this.onClickStartSwap}
|
|
|
|
text={translate('SWAP_init_CTA')}
|
|
|
|
disabled={this.state.disabled}
|
2017-12-14 05:08:45 +00:00
|
|
|
type="primary"
|
2017-07-31 23:14:30 +00:00
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|