-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
64 lines (57 loc) · 1.97 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
64
import { h, app } from 'hyperapp'
import debounce from 'debounce-promise'
import './styles/main.sass'
const getUserDataFn = username => {
return fetch(`https://api.github.com/users/${username}`)
.then(res => res.json())
}
const getUserData = debounce(getUserDataFn, 700)
// single global state - one per app
const state = {
username: '',
userData: null,
}
const actions = {
updateUsername: (username) => (state, actions) => {
// perform side effect - fetching the user data from github API
getUserData(username)
.then(actions.setUserData)
// what the action actually changes in state is just username
return { username }
},
// a simplest action, which just updates some part of state
setUserData: userData => state => ({ userData })
}
// here comes the JSX, but remember that it's just syntactic sugar:
// <div className='classy'>hello</div>
// becomes
// h('div', {className: 'classy'}, 'hello')
// the 'h' corresponds to 'React.createElement' in React
const view = (state, actions) =>
<main>
<div>Search github users:</div>
<input
type='text'
className='searchInput'
value={state.username}
oninput={e => actions.updateUsername(e.target.value)}
/>
<br/>
<div className='userCard'>
{state.userData ? (
<div>
<img class='userCard__img' src={state.userData.avatar_url} />
<div class='userCard__name'>{state.userData.name}</div>
<div class='userCard__location'>{state.userData.location}</div>
</div>
) : (
<div>👆 search 'em</div>
)}
</div>
</main>
// This runs the app. The return object (here not used) makes it possible
// to interact with the app from outside of the view
// In a real app, you should not attach the view directly to the body, but
// create some element in the static HTML to attach to.
// more on that: https://medium.com/@dan_abramov/two-weird-tricks-that-fix-react-7cf9bbdef375#486f
app(state, actions, view, document.body)