MyCrypto/common/containers/Tabs/Swap/components/Rates.tsx

130 lines
3.0 KiB
TypeScript
Raw Normal View History

Shapeshift Integration (#564) * progress * Normalize bity api response * Filter api response * Track swap information in component state * Update dropdown onchange * remove dead code * Update Min Max Validation * Update minmax err msg && fix onChangeOriginKind * Add origin & destination to redux state * Update types & Update tests * Update types * Update swap.spec.ts test * Remove commented out code * Remove hardcoded coin array * Create types.ts for swap reducer * Update swapinput type * Update bityRates in localStorage & Replace all instances of ...Kind / ...Amount props * Add shapeshift banner * initial work for sagas * Update Types * Update swap reducer initial state * Update Types & Store empty obj for bityRates / options * Update more types * added shapeshift file and rates comments * action reducers and prop mapping to components * add typings and swap icon * more actions reducers and sagas * debugging shapeshift service * add Headers * Fix content type * add order reset saga and ui fixes * remove console log and swap b/w Bity and Shapeshift * working state for Shapeshift and Bity - tested with mainnet * add icon component * UI improvements and fix select bug * fix timer bug * add bity fallback options and toFixed floats * tslint errors * add arrow to dropdown and add support footer * Add service provider * fix minor $ bug and stop timer on order complete * better load UX and dropdown UX * fixed single test * currRate prop bugs and reduce LS bloat * takeEvery on timer saga and don't clear state.options to restartSwap reducer * export tx sagas and fix minor type * Add ShapeShift Rates functionality when selecting a ShapeShift pair. * type fixes * BugFix: Don't change displayed ShapeShift Rate Inputs on every dropdown change Also contains some caching / performance improvements * BugFix: Don't remote rate inputs when falsy amount * fix type error * Progress commit * Implement saga logic * Make address field factory component * Shorten debounce time * Make new actions / sagas for handling single token lookup * Implement working version of litesend * Change saga into selector * Add failing spec * fix broken test * add debounce to error message * fix tests * update snapshots * test coverage * move setState disabled property from debounce so we instantly can go to next step on valid amounts * much deeper test coverage, fix debounce ux, and fix bity flashing at swap page load * fix minor failing test * seperate shapeshift erc20 token whitelist * fix saveState store bug * break orderTimeRemaining saga up and rewrite tests * add new swap icon * remove unused allowReadOnly prop * change offlineaware to walletdecrypt for litesend * fix LiteSend changewallet bug * fix error message UX * fix button styling to match develop * fix liteSend test * Fix LiteSend UX on unavl tokens, dropdown null value, and don't show decrypt in litesend after successful wallet decrypt. * add litesend network check
2018-01-02 18:04:50 +00:00
import { NormalizedRates } from 'reducers/swap/types';
import React, { Component } from 'react';
import { toFixedIfLarger } from 'utils/formatters';
import './CurrentRates.scss';
import { ProviderName } from 'actions/swap';
import { objectContainsObjectKeys } from 'utils/helpers';
interface RateInputProps {
rate: number;
amount: number | string;
pair: string;
origin: string;
destination: string;
onChange: any;
}
export const RateInput: React.SFC<RateInputProps> = ({
rate,
amount,
pair,
origin,
destination,
onChange
}) => {
return amount || amount === 0 || amount === '' ? (
<div className="SwapRates-panel-rate">
<input
className="SwapRates-panel-rate-input"
onChange={onChange}
value={amount}
name={pair}
/>
<span className="SwapRates-panel-rate-amount">
{` ${origin} = ${toFixedIfLarger(+amount * rate, 6)} ${destination}`}
</span>
</div>
) : null;
};
interface Props {
provider: ProviderName;
rates: NormalizedRates;
}
interface State {
pairs: { [pair: string]: number };
}
export default class Rates extends Component<Props, State> {
public state = {
pairs: {}
};
public componentDidMount() {
this.setState({ pairs: this.getPairs() });
}
public componentDidUpdate() {
const newPairs = this.getPairs();
// prevents endless loop. if state already contains new pairs, don't set state
if (!objectContainsObjectKeys(newPairs, this.state.pairs)) {
const pairs = {
...this.state.pairs,
...newPairs
};
this.setState({
pairs
});
}
}
public getPairs = () => {
const { rates } = this.props;
const { allIds } = rates;
return allIds.reduce((acc, cur) => {
acc[cur] = 1;
return acc;
}, {});
};
public onChange = (event: any) => {
const { value } = event.target;
const { name } = event.target;
this.setState({
pairs: {
...this.state.pairs,
[name]: value
}
});
};
public buildRateInputs = () => {
const { rates } = this.props;
const { pairs } = this.state;
const fullData: RateInputProps[] = [];
rates.allIds.forEach(each => {
fullData.push({
rate: rates.byId[each].rate,
amount: pairs[each],
pair: each,
origin: rates.byId[each].options[0],
destination: rates.byId[each].options[1],
onChange: this.onChange
});
});
// TODO - don't hardcode only first 4 elements of array.
// not likely to change until significant UI revamp, so not worth spending time on now
return (
<div>
<div className="SwapRates-panel-side col-sm-6">
<RateInput {...fullData[0]} />
<RateInput {...fullData[1]} />
</div>
<div className="SwapRates-panel-side col-sm-6">
<RateInput {...fullData[2]} />
<RateInput {...fullData[3]} />
</div>
</div>
);
};
public render() {
return this.buildRateInputs();
}
}