2018-02-25 01:29:34 +00:00
|
|
|
import React, { PureComponent } from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import sample from 'lodash/sample';
|
|
|
|
import times from 'lodash/times';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
NormalizedBityRates,
|
|
|
|
NormalizedShapeshiftRates,
|
|
|
|
NormalizedShapeshiftRate
|
|
|
|
} from 'reducers/swap/types';
|
2017-09-25 02:06:28 +00:00
|
|
|
import bityLogoWhite from 'assets/images/logo-bity-white.svg';
|
2018-01-02 18:04:50 +00:00
|
|
|
import shapeshiftLogoWhite from 'assets/images/logo-shapeshift.svg';
|
2017-09-25 02:06:28 +00:00
|
|
|
import Spinner from 'components/ui/Spinner';
|
2018-01-20 20:06:28 +00:00
|
|
|
import { bityReferralURL, shapeshiftReferralURL } from 'config';
|
2017-06-12 01:02:39 +00:00
|
|
|
import translate from 'translations';
|
2018-01-02 18:04:50 +00:00
|
|
|
import { SHAPESHIFT_WHITELIST } from 'api/shapeshift';
|
2018-02-25 01:29:34 +00:00
|
|
|
import {
|
|
|
|
loadShapeshiftRatesRequestedSwap,
|
|
|
|
TLoadShapeshiftRatesRequestedSwap,
|
|
|
|
stopLoadShapeshiftRatesSwap,
|
|
|
|
TStopLoadShapeshiftRatesSwap,
|
|
|
|
ProviderName
|
|
|
|
} from 'actions/swap';
|
|
|
|
import { getOffline } from 'selectors/config';
|
2018-01-02 18:04:50 +00:00
|
|
|
import Rates from './Rates';
|
2018-02-25 01:29:34 +00:00
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import './CurrentRates.scss';
|
2018-03-07 23:36:05 +00:00
|
|
|
import { Optional } from 'utils/types';
|
2017-06-12 01:02:39 +00:00
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
interface StateProps {
|
|
|
|
isOffline: boolean;
|
2018-01-02 18:04:50 +00:00
|
|
|
provider: ProviderName;
|
|
|
|
bityRates: NormalizedBityRates;
|
|
|
|
shapeshiftRates: NormalizedShapeshiftRates;
|
2017-12-11 17:44:53 +00:00
|
|
|
}
|
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
interface ActionProps {
|
|
|
|
loadShapeshiftRatesRequestedSwap: TLoadShapeshiftRatesRequestedSwap;
|
|
|
|
stopLoadShapeshiftRatesSwap: TStopLoadShapeshiftRatesSwap;
|
|
|
|
}
|
|
|
|
|
|
|
|
type Props = StateProps & ActionProps;
|
|
|
|
|
|
|
|
class CurrentRates extends PureComponent<Props> {
|
2018-03-07 23:36:05 +00:00
|
|
|
private shapeShiftRateCache: any = null;
|
2017-12-11 17:44:53 +00:00
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
public componentDidMount() {
|
|
|
|
if (!this.props.isOffline) {
|
|
|
|
this.loadRates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-11 02:34:27 +00:00
|
|
|
public UNSAFE_componentWillReceiveProps(nextProps: Props) {
|
2018-02-25 01:29:34 +00:00
|
|
|
if (this.props.isOffline && !nextProps.isOffline) {
|
|
|
|
this.loadRates();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public componentWillUnmount() {
|
|
|
|
this.props.stopLoadShapeshiftRatesSwap();
|
|
|
|
}
|
|
|
|
|
|
|
|
public loadRates() {
|
|
|
|
this.props.loadShapeshiftRatesRequestedSwap();
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
public getRandomSSPairData = (
|
|
|
|
shapeshiftRates: NormalizedShapeshiftRates
|
|
|
|
): NormalizedShapeshiftRate => {
|
|
|
|
const coinOne = sample(SHAPESHIFT_WHITELIST) as string;
|
|
|
|
const coinTwo = sample(SHAPESHIFT_WHITELIST) as string;
|
|
|
|
const pair = coinOne + coinTwo;
|
|
|
|
const pairData = shapeshiftRates.byId[pair];
|
|
|
|
if (pairData) {
|
|
|
|
return pairData;
|
|
|
|
} else {
|
|
|
|
// if random pairing is unavailable / missing in state
|
|
|
|
return this.getRandomSSPairData(shapeshiftRates);
|
|
|
|
}
|
2017-06-19 05:39:07 +00:00
|
|
|
};
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
public buildSSPairs = (shapeshiftRates: NormalizedShapeshiftRates, n: number = 4) => {
|
|
|
|
const pairCollection = times(n, () => this.getRandomSSPairData(shapeshiftRates));
|
2018-03-07 23:36:05 +00:00
|
|
|
const byId = pairCollection.reduce<{ [id: string]: NormalizedShapeshiftRate }>((acc, cur) => {
|
2018-01-02 18:04:50 +00:00
|
|
|
acc[cur.id] = cur;
|
|
|
|
return acc;
|
|
|
|
}, {});
|
|
|
|
const allIds = pairCollection.map(SSData => SSData.id);
|
|
|
|
return {
|
|
|
|
byId,
|
|
|
|
allIds
|
|
|
|
};
|
2017-06-19 05:39:07 +00:00
|
|
|
};
|
2017-06-12 01:02:39 +00:00
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
public isValidRates = (rates: Optional<NormalizedShapeshiftRates>) => {
|
2018-01-02 18:04:50 +00:00
|
|
|
return rates && rates.allIds && rates.allIds.length > 0;
|
2017-08-31 15:56:49 +00:00
|
|
|
};
|
2017-07-22 21:24:03 +00:00
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
public setupRates = () => {
|
|
|
|
const { shapeshiftRates, bityRates, provider } = this.props;
|
|
|
|
|
|
|
|
let fixedRates;
|
|
|
|
if (provider === 'bity') {
|
|
|
|
fixedRates = bityRates;
|
|
|
|
} else if (provider === 'shapeshift') {
|
|
|
|
// if ShapeShift rates are valid, filter to 4 random pairs
|
|
|
|
if (this.isValidRates(shapeshiftRates)) {
|
|
|
|
if (!this.shapeShiftRateCache) {
|
|
|
|
fixedRates = this.buildSSPairs(shapeshiftRates);
|
|
|
|
this.shapeShiftRateCache = fixedRates;
|
|
|
|
} else {
|
|
|
|
fixedRates = this.shapeShiftRateCache;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// else, pass along invalid rates. Child component will handle showing spinner until they become valid
|
|
|
|
fixedRates = shapeshiftRates;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return fixedRates;
|
|
|
|
};
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
public swapEl = (providerURL: string, providerLogo: string, children: any) => {
|
2017-06-19 05:39:07 +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="SwapRates">
|
2018-03-22 03:50:25 +00:00
|
|
|
<h3 className="SwapRates-title">{translate('SWAP_RATES')}</h3>
|
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
|
|
|
|
|
|
|
<section className="SwapRates-panel row">
|
2018-01-02 18:04:50 +00:00
|
|
|
{children}
|
2018-01-10 05:17:52 +00:00
|
|
|
<a
|
|
|
|
className="SwapRates-panel-logo"
|
|
|
|
href={providerURL}
|
|
|
|
target="_blank"
|
|
|
|
rel="noopener noreferrer"
|
|
|
|
>
|
2018-03-08 19:28:43 +00:00
|
|
|
<img src={providerLogo} width={120} height={49} alt="Shapeshift Logo" />
|
2018-01-02 18:04:50 +00:00
|
|
|
</a>
|
|
|
|
</section>
|
|
|
|
</article>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
public render() {
|
|
|
|
const { provider } = this.props;
|
|
|
|
const rates = this.setupRates();
|
|
|
|
const providerLogo = provider === 'shapeshift' ? shapeshiftLogoWhite : bityLogoWhite;
|
|
|
|
const providerURL = provider === 'shapeshift' ? shapeshiftReferralURL : bityReferralURL;
|
|
|
|
|
|
|
|
let children;
|
|
|
|
|
|
|
|
if (this.isValidRates(rates)) {
|
|
|
|
children = <Rates provider={provider} rates={rates} />;
|
|
|
|
} else {
|
|
|
|
// TODO - de-dup
|
|
|
|
children = (
|
2018-04-06 18:59:07 +00:00
|
|
|
<React.Fragment>
|
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="SwapRates-panel-side col-sm-6">
|
2018-04-06 18:59:07 +00:00
|
|
|
<div className="SwapRates-panel-rate is-loading">
|
2018-01-02 18:04:50 +00:00
|
|
|
<Spinner size="x1" light={true} />
|
|
|
|
</div>
|
2018-04-06 18:59:07 +00:00
|
|
|
<div className="SwapRates-panel-rate is-loading">
|
2018-01-02 18:04:50 +00:00
|
|
|
<Spinner size="x1" light={true} />
|
|
|
|
</div>
|
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>
|
|
|
|
|
|
|
|
<div className="SwapRates-panel-side col-sm-6">
|
2018-04-06 18:59:07 +00:00
|
|
|
<div className="SwapRates-panel-rate is-loading">
|
2018-01-02 18:04:50 +00:00
|
|
|
<Spinner size="x1" light={true} />
|
|
|
|
</div>
|
2018-04-06 18:59:07 +00:00
|
|
|
<div className="SwapRates-panel-rate is-loading">
|
2018-01-02 18:04:50 +00:00
|
|
|
<Spinner size="x1" light={true} />
|
|
|
|
</div>
|
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>
|
2018-04-06 18:59:07 +00:00
|
|
|
</React.Fragment>
|
2018-01-02 18:04:50 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return this.swapEl(providerURL, providerLogo, children);
|
2017-06-19 05:39:07 +00:00
|
|
|
}
|
2017-06-12 01:02:39 +00:00
|
|
|
}
|
2018-02-25 01:29:34 +00:00
|
|
|
|
|
|
|
function mapStateToProps(state: AppState): StateProps {
|
|
|
|
return {
|
|
|
|
isOffline: getOffline(state),
|
|
|
|
provider: state.swap.provider,
|
|
|
|
bityRates: state.swap.bityRates,
|
|
|
|
shapeshiftRates: state.swap.shapeshiftRates
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, {
|
|
|
|
loadShapeshiftRatesRequestedSwap,
|
|
|
|
stopLoadShapeshiftRatesSwap
|
|
|
|
})(CurrentRates);
|