-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathclient.js
218 lines (195 loc) · 5.6 KB
/
client.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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
FBP.debug(true)
const choo = require('choo')
const app = choo()
const config = require('./config.js')
// import the main template
const main = require('./main.js')
const host = location.hostname
const protocol = location.protocol
const port = config.port
console.log('config: ', config);
app.use(function (state, emitter) {
// initialize default state (if you see this, something is wrong)
state.filterString = ''
state.filterDone = 0
state.todos = [
{task: 'default task 1', status: 0, id: 1},
{task: 'default task 2', status: 1, id: 2},
{task: 'default task 3', status: 0, id: 3}
]
state.errors = []
// delete a todo
emitter.on('deleteTodo', function deleteTodo (id) {
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/todo/${id}`,
'ajax.method': 'DELETE',
'ajax.data': null,
'storeTodos.key': 'todos', // parameterization of storage component (needs localstorage key)
}
)
})
// toggle a todo's status
emitter.on('toggleStatus', function toggleStatus (id, status) {
console.log('toggling status with: ', id, status)
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/todo/${id}`,
'ajax.method': 'PUT',
'ajax.data': { status: status },
'storeTodos.key': 'todos', // parameterization of storage component (needs localstorage key)
}
)
})
// get the most recent todo list from the server
emitter.on('getAll', function getAll () {
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/todos`,
'ajax.method': 'GET',
'ajax.data': null,
'storeTodos.key': 'todos'
}
)
})
emitter.on('search', function search (query) {
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/search?search=${query}`,
'ajax.method': 'GET',
'ajax.data': null,
'storeTodos.key': 'todos' // parameterization of storage component (needs localstorage key)
}
)
})
// NOTE: filtering here is dumb. try to make it more like a real query
emitter.on('filter', function filter (type, filterOn) {
console.log('filtering on: ', type, filterOn)
if (type === 'task') {
state.filterString = filterOn
} else {
state.filterDone = filterOn
}
FBP.go(
'filter',
{
'getTodos.key': 'todos',
'filter.key': type === 'task' ? 'task' : 'status',
'filter.filterOn': filterOn
}
)
})
emitter.on('add', function add (text) {
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/todos`,
'ajax.method': 'POST',
'ajax.data': {task: text, status: 'in progress'},
'storeTodos.key': 'todos' // parameterization of storage component (needs localstorage key)
}
)
})
emitter.on('testError', function testError () {
FBP.go(
'ajax',
{
'ajax.url': `${protocol}//${host}:${port}/testError`,
'ajax.method': 'GET',
'ajax.data': null,
'storeTodos.key': 'todos',
'storeErr.key': 'errors'
}
)
})
/**
* NOTE: defining component and networks here under app.use because we need the state and emitter
*/
/**
* Define specialized components for this app
*/
/**
* Update the ToDo list data store
* @param {ip} todos - A list of ToDos
* @param {port} output - The output port
*/
FBP.registerComponent({
name: 'updateToDoList',
inPorts: ['todos'],
outPorts: ['output'],
body: function updateToDoList (todos, output) {
state.todos = todos.data
emitter.emit('render')
output(todos)
}
})
/**
* Add an error to the Errors list data store
* @param {ip} error - An error to append to the errors list data store
* @param {port} output - The output port
*/
FBP.registerComponent({
name: 'updateErrors',
inPorts: ['error'],
outPorts: ['output'],
body: function updateErrors (error, output) {
state.errors.push(error.data)
setTimeout(function removeError () {
FBP.log('removing error: ', error.data)
state.errors.splice(state.errors.indexOf(error.data), 1)
emitter.emit('render')
}, 3000)
emitter.emit('render')
output(error)
}
})
/*
* Define the networks
*/
FBP.registerNetwork({
name: 'ajax',
processes: [
{name: 'ajax', component: 'ajax'},
{name: 'storeTodos', component: 'localStorageStore'},
{name: 'storeErr', component: 'localStorageStore'},
{name: 'updateToDoList', component: 'updateToDoList'},
{name: 'log', component: 'log'},
{name: 'trash', component: 'trash'},
{name: 'updateErrors', component: 'updateErrors'}
],
connections: {
'ajax.output': 'log.input',
'log.output': 'storeTodos.data',
'storeTodos.output': 'updateToDoList.todos',
'updateToDoList.output': 'trash.data',
// error branch
'ajax.outputErr': 'storeErr.data',
'storeErr.output': 'updateErrors.error',
'updateErrors.output': 'trash.data'
}
})
FBP.registerNetwork({
name: 'filter',
processes: [
{name: 'getTodos', component: 'localStorageRetrieve'},
{name: 'filter', component: 'filter'},
{name: 'updateToDoList', component: 'updateToDoList'},
{name: 'trash', component: 'trash'}
],
connections: {
'getTodos.output': 'filter.coll',
'filter.output': 'updateToDoList.todos',
'updateToDoList.output': 'trash.data'
}
})
emitter.emit('getAll')
emitter.emit('testError')
})
// create a route
app.route('/', main)
// start app
app.mount('div')