-
Notifications
You must be signed in to change notification settings - Fork 295
/
helpers.tsx
73 lines (69 loc) · 2.13 KB
/
helpers.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
// @flow
import { get } from 'lodash';
import React from 'react';
import SVGInline from 'react-svg-inline';
import styles from './DelegationCenterHeader.scss';
import delimeterIcon from '../../../assets/images/delimeter.inline.svg';
import delimeterSlashIcon from '../../../assets/images/delimeter-slash.inline.svg';
const EPOCH_MAX_LENGTH = 5;
export const with2Decimals = (value: number) => {
const formattedValue = value.toString().match(/^-?\d+(?:\.\d{0,2})?/);
const result = get(formattedValue, 0, 0);
return result;
};
export const generateFieldPanel = (labels: any, values: any, index: number) => {
const value = values[index];
const includeSlashDelimeter = index === values.length - 2;
const includeDotsDelimeter =
!includeSlashDelimeter && index !== values.length - 1;
const labelStr = labels[index];
const valueStr = value.toString();
let zeroValues = '';
if (index === 1 && valueStr.length < values[index + 1].toString().length) {
const zerosToAdd =
index === 1
? parseInt(values[index + 1].toString().length, 10) -
parseInt(valueStr.length, 10)
: parseInt(EPOCH_MAX_LENGTH, 10) - parseInt(valueStr.length, 10);
switch (zerosToAdd) {
case 1:
zeroValues = '0';
break;
case 2:
zeroValues = '00';
break;
case 3:
zeroValues = '000';
break;
case 4:
zeroValues = '0000';
break;
default:
break;
}
}
return (
<div className={styles.fieldPanel}>
<div className={styles.left}>
<div className={styles.fieldLabel}>{labelStr}</div>
<div className={styles.fieldValue}>
{zeroValues && <span>{zeroValues}</span>}
{valueStr}
</div>
</div>
{includeDotsDelimeter && (
<div className={styles.right}>
<SVGInline svg={delimeterIcon} className={styles.delimeterIcon} />
</div>
)}
{includeSlashDelimeter && (
<div className={styles.right}>
<SVGInline
svg={delimeterSlashIcon}
className={styles.delimeterSlashIcon}
/>
</div>
)}
</div>
);
};