forked from MicheleBertoli/react-automata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
79 lines (71 loc) · 1.67 KB
/
App.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import React from 'react'
import { hot } from 'react-hot-loader'
import { Action, withStatechart } from '../src'
export const statechart = {
initial: 'idle',
states: {
idle: {
on: {
FETCH: 'fetching',
},
onEntry: 'showButton',
},
fetching: {
on: {
SUCCESS: 'success',
ERROR: 'error',
},
onEntry: 'fetchGists',
},
success: {
onEntry: 'showGists',
},
error: {
on: {
FETCH: 'fetching',
},
onEntry: 'showError',
},
},
}
export class App extends React.Component {
fetchGists() {
fetch('https://api.github.com/users/gaearon/gists')
.then(response => response.json())
.then(gists => this.props.transition('SUCCESS', { gists }))
.catch(() => this.props.transition('ERROR'))
}
handleClick = () => {
this.props.transition('FETCH')
}
render() {
return (
<div>
<h1>Actions</h1>
<Action show="showButton" hide="fetchGists">
<button onClick={this.handleClick}>Fetch</button>
</Action>
<Action show="fetchGists">Loading...</Action>
<Action show="showGists">
<ul>
{this.props.gists
.filter(gist => gist.description)
.map(gist => <li key={gist.id}>{gist.description}</li>)}
</ul>
</Action>
<Action show="showError">
<button onClick={this.handleClick}>Retry</button>
Oh, snap!
</Action>
</div>
)
}
}
App.defaultProps = {
gists: [],
}
const options = {
devTools: true,
}
const StateMachine = withStatechart(statechart, options)(App)
export default hot(module)(StateMachine)