Skip to content
This repository was archived by the owner on Nov 19, 2018. It is now read-only.

Commit 0c369c8

Browse files
committed
feat(<Filesize />): initial version of component
TODO: - unit tests - docs - manual tests closes #18
1 parent 57775cc commit 0c369c8

File tree

2 files changed

+119
-1
lines changed

2 files changed

+119
-1
lines changed

.eslintrc.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,8 @@
3030
"rules": {
3131
"indent": [
3232
"error",
33-
4
33+
4,
34+
{ "SwitchCase": 1 }
3435
],
3536
"linebreak-style": [
3637
"error",

src/components/filesize.jsx

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
import React, { Component, PropTypes } from 'react'
2+
3+
class Filesize extends Component {
4+
static propTypes = {
5+
id: PropTypes.number.isRequired,
6+
units: PropTypes.shape({
7+
byte: PropTypes.string,
8+
kilobyte: PropTypes.string,
9+
megabyte: PropTypes.string,
10+
gigabyte: PropTypes.string,
11+
terabyte: PropTypes.string
12+
}),
13+
uploader: PropTypes.object.isRequired
14+
};
15+
16+
static defaultProps = {
17+
units: {
18+
byte: 'B',
19+
kilobyte: 'KB',
20+
megabyte: 'MB',
21+
gigabyte: 'GB',
22+
terabyte: 'TB'
23+
}
24+
}
25+
26+
constructor(props) {
27+
super(props)
28+
29+
this.state = {
30+
size: props.uploader.methods.getSize(props.id)
31+
}
32+
33+
// If this is a scaled image, the size won't be known until upload time.
34+
this._onUploadHandler = id => {
35+
if (id === this.props.id) {
36+
this.setState({
37+
size: this.props.uploader.getSize(id)
38+
})
39+
}
40+
}
41+
}
42+
43+
componentDidMount() {
44+
this.props.uploader.on('upload', this._onUploadHandler)
45+
}
46+
47+
componentWillUnmount() {
48+
this.props.uploader.off('upload', this._onUploadHandler)
49+
}
50+
51+
shouldComponentUpdate(nextProps, nextState) {
52+
return nextState.size !== this.state.size || !areUnitsEqual(nextProps.units, this.props.units)
53+
}
54+
55+
render() {
56+
const size = this.state.size
57+
const units = this.props.units
58+
const { formattedSize, formattedUnits } = formatSizeAndUnits({ size, units })
59+
60+
return (
61+
<span className='react-fine-uploader-filesize'>
62+
<span className='react-fine-uploader-filesize-value'>
63+
{ formattedSize }
64+
</span>
65+
<span className='react-fine-uploader-filesize-unit'>
66+
{ formattedUnits }
67+
</span>
68+
</span>
69+
)
70+
}
71+
}
72+
73+
const formatSizeAndUnits = ({ size, units }) => {
74+
let formattedSize,
75+
formattedUnits
76+
77+
switch(size) {
78+
case size < 1e+3: {
79+
formattedSize = size
80+
formattedUnits = units.byte
81+
break
82+
}
83+
case size >= 1e+3 && size < 1e+6: {
84+
formattedSize = (1e+3 / size).toFixed(2)
85+
formattedUnits = units.kilobyte
86+
break
87+
}
88+
case size >= 1e+6 && size < 1e+9: {
89+
formattedSize = (1e+6 / size).toFixed(2)
90+
formattedUnits = units.megabyte
91+
break
92+
}
93+
case size >= 1e+9 && size < 1e+12: {
94+
formattedSize = (1e+9 / size).toFixed(2)
95+
formattedUnits = units.gigabyte
96+
break
97+
}
98+
default: {
99+
formattedSize = (1e+12 / size).toFixed(2)
100+
formattedUnits = units.terabyte
101+
}
102+
}
103+
104+
return { formattedSize, formattedUnits }
105+
}
106+
107+
const areUnitsEqual = (units1, units2) => {
108+
const keys1 = Object.keys(units1)
109+
110+
if (keys1.length === Object.keys(units2).length) {
111+
return keys1.every(key1 => units1[key1] === units2[key1])
112+
}
113+
114+
return false
115+
}
116+
117+
export default Filesize

0 commit comments

Comments
 (0)