This repository has been archived by the owner on Dec 23, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
63 lines (56 loc) · 1.75 KB
/
index.js
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
import React, { Fragment, Component } from 'react';
import logo from '../../images/logo.svg';
import AvatarFromCDN from '../../components/AvatarFromCDN';
import AvatarFromFiles from '../../components/AvatarFromFiles';
import AvatarFromLazyFiles from '../../components/AvatarFromLazyFiles';
import AvatarFromLocalServer from '../../components/AvatarFromLocalServer';
import { app, loading, logoContainer, title } from './styles.scss';
const data = {
head: 'head2',
lazyHead: 'head3',
eyes: 'eyes2',
lazyEyes: 'eyes3',
mouth: 'mouth2',
lazyMouth: 'mouth3',
cdn: 'https://i.imgur.com/foiVXpi.png',
};
const fetchAvatarFromAPI = status =>
new Promise((resolve, reject) => {
setTimeout(() => {
if (status === 200) {
resolve({ data });
} else {
reject('Unable to locate avatar!');
}
}, 1500);
});
class Home extends Component {
state = { isLoading: true, head: '', eyes: '', mouth: '' };
componentDidMount = () => {
this.fetchAvatar();
};
fetchAvatar = () => {
fetchAvatarFromAPI(200)
.then(({ data }) => this.setState({ isLoading: false, ...data }))
.catch(err => this.setState({ err: err.toString() }));
};
render = () => (
<div className={app}>
<div className={logoContainer}>
<h1 className={title}>React Starter Kit</h1>
<AvatarFromFiles />
{this.state.isLoading ? (
<img className={loading} src={logo} alt="" width={100} />
) : (
<Fragment>
<AvatarFromLocalServer {...this.state} />
<AvatarFromLazyFiles {...this.state} />
<AvatarFromCDN src={this.state.cdn} />
</Fragment>
)}
</div>
{this.state.err && <p>{this.state.err}</p>}
</div>
);
}
export default Home;