-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
141 lines (129 loc) · 3.43 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
var hg = require('mercury');
var h = require('mercury').h;
var _ = require('underscore');
var uuid = require('uuid');
var request = require('browser-request');
function getTodos(cb) {
request({
method: 'GET',
url: '/api/todos',
json: true,
}, function(e, r, b) {
if(e || r.statusCode < 200 || r.statusCode >= 400) {
console.log('something went wrong');
cb(e);
}
//b is an array of todo docs from couchdb
cb(null, b);
});
}
getTodos(function(e, todos) {
var initialTodos = hg.array([]);
for(var i = 0; i < todos.length; i++) {
initialTodos.push(hg.struct({
id: hg.value(todos[i]._id),
task: hg.value(todos[i].task)
}));
}
function App() {
var state = hg.state({
addTodo: hg.value(''),
handles: hg.value(null),
todos: initialTodos
});
console.log('foo');
state.handles.set(hg.handles({
change: function(state, data) {
state.addTodo.set(data.addTodo);
},
addToList: function(state) {
request({
method: 'POST',
url: '/api/todos/add',
body: { task: state.addTodo() },
json: true
}, function(e, r, b) {
if(e || (r.statusCode < 200 || r.statusCode >= 400)) {
return console.log('something went wrong');
}
var listObj = hg.struct({
id: b.id,
task: hg.value(state.addTodo())
});
state.todos.push(listObj);
state.addTodo.set('');
});
},
removeFromList: function(state, data) {
var index;
for(var i = 0; i < state.todos.getLength(); i++) {
var item = state.todos.get(i);
if(item.id === data.id) {
index = i;
break;
}
}
request({
method: 'DELETE',
url: '/api/todos/' + data.id
}, function(e, r, b) {
if(e || (r.statusCode < 200 || r.statusCode >= 400)) {
return console.log('something went wrong');
}
console.log(b);
state.todos.splice(index,1);
});
}
}, state));
return state;
}
function inputBox(value, sink) {
return h('input', {
value: value,
name: 'addTodo',
type: 'text',
placeholder: 'Add todo',
'ev-event': hg.changeEvent(sink)
});
}
function addButton(state) {
return h('button.btn.btn-primary', {
style: { 'margin-left': '5px' },
'ev-click': hg.event(state.handles.addToList)
}, 'Add');
}
function deleteButton(state, id) {
return h('button.btn.btn-xs.btn-danger.pull-right', {
'ev-click': hg.event(state.handles.removeFromList, { id: id })
},'Delete');
}
function buildItems(state) {
var items = [];
for(var i = 0; i < state.todos.length; i++) {
var el = h('li.list-group-item', [
state.todos[i].task,
deleteButton(state, state.todos[i].id)
]);
items.push(el);
}
return items;
}
function list(state) {
return h('ul.list-group', buildItems(state));
}
App.render = function render(state) {
console.log('bar');
return h('div.container', [
h('div.row', [
h('div.col-md-3', [
inputBox(state.addTodo, state.handles.change),
addButton(state)
])
]),
h('div.row', [
h('div.col-md-6', list(state))
])
]);
}
hg.app(document.body, App(), App.render);
});