MyCrypto/common/components/ui/UnitDisplay.tsx
Daniel Kmak 985ea0fb89 Ethereum Alarm Clock Integration (#1343)
* [FEATURE] Initial EAC integration.

* Title and explanation

* [FEATURE] Move the Schedule Payment to the main tab.

* [FEATURE] TimeBounty slider.

* [FEATURE] Move to main menu.

* [FEATURE] Redirection to the DApp for details.

* [FEATURE] Timestamp scheduling

* Scheduling: Basic date and time widget

* Linting fixes

* Moved the datetime field to new tab

* Fixed push errors

* Added missing specs

* Undid unintentional UI change

* Fixed some failing tests

* Ignore datetime parameter when checking if a transaction is full

* Added a date selector widget and renamed ScheduleTimestamp to ScheduleDate

* Marked componentDidMount

* Initialized Pikaday

* Revert "Initialized Pikaday"

This reverts commit 4e5bf5b2b882f236f5977400abf9b7092cbd1592.

* Revert "Marked componentDidMount"

This reverts commit 85d52192ac58f4b6ca9219e702f7390cd27e582f.

* Revert "Added a date selector widget and renamed ScheduleTimestamp to ScheduleDate"

This reverts commit aaad0ac9b565a78d1bfc631754160919fd38a59b.

* Converted the date picker into a datetime picker

* Added decent styling to the datetimepicker

* Added validation to the datetime picker

* Fixed prepush errors for scheduling timestamp

* Adjusted validation logic scheduling timestamp

* [FEATURE] Move scheduling to main tab.

* [FEATURE] Timezone selector

* [FEATURE] Scheduling: Timezone selector

* Removed zombie files

* [FEATURE] Reimplement Time Bounty.

* [FEATURE] Time/block selector

* [FEATURE] Add Window Size field.

* [FEATURE] Time/block switch functionality

* Implemented time/block switcher fuctionality

* [FEATURE] Add Schedule Gas Price field.

* [FEATURE] Scheduling toggle

* [FEATURE] Add basic styling and network check.

* [FEATURE] Add Schedule Gas Limit field

* [FEATURE] "Scheduled" button styling

* Reordered, renamed and centered scheduling elements

* Added the toggle button styling

* Class -> ClassName

* [FEATURE] Add Deposit field

*  [FEATURE] Move scheduling code into one directory

* [FIX] Scheduling responsiveness

* [FIX] Datetime picker not working on md screens

* [FEATURE] Timestamp Scheduling basic functionality

* [FIX] Fix data serialization.

* [FEATURE] Timezone inclusion

* [FEATURE] Add ChronoLogic logo.

* [FEATURE] Add link to image.

* [FIX] Update CSS of logo.

* [FEATURE] Default Window Size

* [FEATURE] Modified Help component to enable acting as a tooltip

* [FEATURE] Call contract to validate scheduling params

* [FIX] Change moment import to fix tests

* [FEATURE] Gas estimation for scheduling

* [FEATURE] Additional validation

* [FEATURE] UI changes and descriptions

* [FEATURE] Add tooltip to window and fix fee display.

* [FIX] Fix ethereumjs-abi dependency.

* [FEATURE] Hide scheduling when sending tokens.

* [FIX] Improved styling datetime picker

* [FEATURE] Add Redux state for scheduling

* [FEATURE] Create Toggle component, Share code between components

* [FEATURE] Use Tooltip component for help.

* [FEATURE] Better datetime picker

* [FEATURE] Remove fee

* Trigger mycryptobuild

* [FIX] Timestamp scheduling - Validation match

* [FIX] EAC integration touchups

* [FIX] Code review fixes

* [FIX] Window Size type

* [FIX] Type fixes.

* [FIX] Make tooltips only show on icons + resposiveness fixes

* [FIX] Break tooltips into more lines

* [FIX] Remove unnecessary code.

* [FIX] Remove unnecessary code.

* [FIX] Remove unnecessary types declaration.

* [FIX] Fee class names
2018-04-14 17:21:33 -05:00

109 lines
3.3 KiB
TypeScript

import React from 'react';
import { connect } from 'react-redux';
import { fromTokenBase, getDecimalFromEtherUnit, UnitKey, Wei, TokenValue } from 'libs/units';
import { formatNumber as format } from 'utils/formatters';
import Spinner from 'components/ui/Spinner';
import { getOffline } from 'selectors/config';
import { AppState } from 'reducers';
interface Props {
/**
* @description base value of the token / ether, incase of waiting for API calls, we can return '???'
* @type {TokenValue | Wei}
* @memberof Props
*/
value?: TokenValue | Wei | null;
/**
* @description Symbol to display to the right of the value, such as 'ETH'
* @type {string}
* @memberof Props
*/
symbol?: string | null;
/**
* @description display the long balance, if false, trims it to 3 decimal places, if a number is specified then that number is the number of digits to be displayed.
* @type {boolean}
* @memberof Props
*/
displayShortBalance?: boolean | number;
displayTrailingZeroes?: boolean;
checkOffline?: boolean;
}
interface EthProps extends Props {
unit: UnitKey;
}
interface TokenProps extends Props {
decimal: number;
}
const isEthereumUnit = (param: EthProps | TokenProps): param is EthProps =>
!!(param as EthProps).unit;
const UnitDisplay: React.SFC<EthProps | TokenProps> = params => {
const { value, symbol, displayShortBalance, displayTrailingZeroes, checkOffline } = params;
let element;
if (!value) {
element = <Spinner size="x1" />;
} else {
const convertedValue = isEthereumUnit(params)
? fromTokenBase(value, getDecimalFromEtherUnit(params.unit))
: fromTokenBase(value, params.decimal);
let formattedValue;
if (displayShortBalance) {
const digits = typeof displayShortBalance === 'number' ? displayShortBalance : 4;
formattedValue = format(convertedValue, digits);
// If the formatted value was too low, display something like < 0.01
if (parseFloat(formattedValue) === 0 && parseFloat(convertedValue) !== 0) {
const padding = digits !== 0 ? `.${'0'.repeat(digits - 1)}1` : '';
formattedValue = `< 0${padding}`;
} else if (displayTrailingZeroes) {
const [whole, deci] = formattedValue.split('.');
formattedValue = `${whole}.${(deci || '').padEnd(digits, '0')}`;
}
} else {
formattedValue = convertedValue;
}
element = (
<span>
{formattedValue}
<span>{symbol ? <>&nbsp;{symbol}</> : ''}</span>
</span>
);
}
return checkOffline ? <ConnectedOfflineDisplay>{element}</ConnectedOfflineDisplay> : element;
};
export default UnitDisplay;
/**
* @description Helper component for displaying alternate text when offline.
* Circumvents typescript issue with union props on connected components.
*/
interface OfflineProps {
offline: AppState['config']['meta']['offline'];
children: React.ReactElement<string>;
}
class OfflineDisplay extends React.Component<OfflineProps> {
public render() {
if (this.props.offline) {
return <span>Balance isn't available offline</span>;
} else {
return this.props.children;
}
}
}
function mapStateToOfflineProps(state: AppState) {
return {
offline: getOffline(state)
};
}
const ConnectedOfflineDisplay = connect(mapStateToOfflineProps)(OfflineDisplay);