-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (49 loc) · 1.22 KB
/
main.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
import {h, text, app} from "https://cdn.skypack.dev/hyperapp"
const API_DOMAIN = `http://localhost:5000/`
const putJson = (dispatch, options) => {
fetch(options.url, {
method: 'PUT',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(options.data)
})
.then(response => response.json())
.then(data => dispatch(options.action, data))
}
const fetchJson = (dispatch, options) => {
fetch(options.url)
.then(response => response.json())
.then(data => dispatch(options.action, data))
}
const GotCounter = (state, data) => ({...state, count: data.counter})
const jsonFetcher = (url, action) => [fetchJson, {url, action}]
const updateCounter = (state) => [
{...state},
[
putJson,
{
url: API_DOMAIN+"counter",
action: GotCounter,
data: {}
}
]
]
const initState = (state) => [
{ count: null },
jsonFetcher(API_DOMAIN+"counter", GotCounter)
]
app({
init: initState,
view: state => h("main", {}, [
h("div", {class: "person"}, [
h("p", {}, text(`Count ${state.count}`)),
h("input", {
type: "button",
value: "increment",
onclick: updateCounter,
}),
]),
]),
node: document.getElementById("app"),
})