-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinbox.go
430 lines (370 loc) · 11.3 KB
/
inbox.go
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package main
import (
"github.com/maxence-charriere/go-app/v10/pkg/app"
"log"
"sort"
"strconv"
"encoding/json"
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
shell "github.com/stateless-minds/go-ipfs-api"
)
const (
topicUpdateIssue = "update-issue"
)
type Inbox struct{
app.Compo
sh *shell.Shell
sub *shell.PubSubSubscription
issues []Issue
alert *Alert
veto bool
}
type Issue struct {
ID string `mapstructure:"_id" json:"_id" validate:"uuid_rfc4122"`
Title string `mapstructure:"title" json:"title" validate:"uuid_rfc4122"`
Description string `mapstructure:"description" json:"description" validate:"uuid_rfc4122"`
Solutions []Solution `mapstructure:"solutions" json:"solutions" validate:"uuid_rfc4122"`
Veto bool `mapstructure:"veto" json:"veto" validate:"uuid_rfc4122"`
}
type Solution struct {
ID string `mapstructure:"_id" json:"_id" validate:"uuid_rfc4122"`
Body string `mapstructure:"body" json:"body" validate:"uuid_rfc4122"`
}
func newInbox() *Inbox {
return &Inbox{}
}
func (c *Inbox) OnMount(ctx app.Context) {
sh := shell.NewShell("localhost:5001")
c.sh = sh
c.subscribeToCreateIssueTopic(ctx)
c.subscribeToUpdateIssueTopic(ctx)
// c.DeleteIssues(ctx)
c.FetchIssues(ctx)
c.alert = newAlert()
ctx.Handle("refresh", c.handleRefresh) // Registering action handler.
}
func (c *Inbox) handleRefresh(ctx app.Context, a app.Action) {
ctx.Update()
}
func (c *Inbox) onChangeTab(ctx app.Context, e app.Event) {
id := ctx.JSSrc().Get("id").String()
app.Window().Get("document").Call("querySelector", ".email-item-unread").Get("classList").Call("remove", "email-item-unread")
app.Window().Get("document").Call("querySelector", ".email-item-selected").Get("classList").Call("remove", "email-item-selected")
app.Window().GetElementByID(id).Get("parentElement").Get("parentElement").Get("classList").Call("add", "email-item-unread")
app.Window().GetElementByID(id).Get("parentElement").Get("parentElement").Get("classList").Call("add", "email-item-selected")
app.Window().Get("document").Call("querySelector", ".inbox.active").Get("classList").Call("remove", "active")
mainElem := app.Window().
Get("document").
Call("getElementById","main-"+id)
mainElem.Get("classList").Call("add", "active")
}
func (c *Inbox) DeleteIssues(ctx app.Context) {
ctx.Async(func() {
err := c.sh.OrbitDocsDelete(dbNameIssue, "all")
if err != nil {
c.alert.send(ctx, AlertError, "Could not delete issues")
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
c.issues = []Issue{}
})
})
}
func (c *Inbox) FetchIssues(ctx app.Context) {
ctx.Async(func() {
v, err := c.sh.OrbitDocsQuery(dbNameIssue, "all", "")
if err != nil {
c.alert.send(ctx, AlertError, "Could not fetch issues")
log.Fatal(err)
}
var vv []interface{}
err = json.Unmarshal(v, &vv)
if err != nil {
log.Fatal(err)
}
var issues []Issue
for _, ii := range vv {
issue := Issue{}
err = mapstructure.Decode(ii, &issue)
if err != nil {
log.Fatal(err)
}
issues = append(issues, issue)
sort.SliceStable(issues, func(i, j int) bool {
return issues[i].ID < issues[j].ID
})
if len(issue.Solutions) > 0 {
sort.SliceStable(issue.Solutions, func(i, j int) bool {
return issue.Solutions[i].ID < issue.Solutions[j].ID
})
}
}
ctx.Dispatch(func(ctx app.Context) {
c.issues = issues
log.Println(c.issues)
})
})
}
func (c *Inbox) subscribeToCreateIssueTopic(ctx app.Context) {
ctx.Async(func() {
topic := topicCreateIssue
subscription, err := c.sh.PubSubSubscribe(topic)
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
c.sub = subscription
c.subscriptionCreateIssue(ctx)
})
}
func (c *Inbox) subscriptionCreateIssue(ctx app.Context) {
ctx.Async(func() {
defer c.sub.Cancel()
// wait on pubsub
res, err := c.sub.Next()
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
// Decode the string data.
str := string(res.Data)
log.Println("Subscriber of topic create-issue received message: " + str)
ctx.Async(func() {
c.subscribeToCreateIssueTopic(ctx)
})
i := Issue{}
err = json.Unmarshal([]byte(str), &i)
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
c.issues = append(c.issues, i)
})
})
}
func (c *Inbox) subscribeToUpdateIssueTopic(ctx app.Context) {
ctx.Async(func() {
topic := topicUpdateIssue
subscription, err := c.sh.PubSubSubscribe(topic)
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
c.sub = subscription
c.subscriptionUpdateIssue(ctx)
})
}
func (c *Inbox) subscriptionUpdateIssue(ctx app.Context) {
ctx.Async(func() {
defer c.sub.Cancel()
// wait on pubsub
res, err := c.sub.Next()
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
// Decode the string data.
str := string(res.Data)
log.Println("Subscriber of topic update-issue received message: " + str)
ctx.Async(func() {
c.subscribeToUpdateIssueTopic(ctx)
})
i := Issue{}
err = json.Unmarshal([]byte(str), &i)
if err != nil {
c.alert.send(ctx, AlertError, "Could not subscribe to topic")
log.Fatal(err)
}
ctx.Dispatch(func(ctx app.Context) {
for _, iss := range c.issues {
if iss.ID == i.ID {
iss = i
}
}
})
})
}
func (c *Inbox) Render() app.UI {
// log.Println(c.alert)
return app.Div().Class("layout content pure-g").Body(
app.If(c.alert != nil && c.alert.message != "", func() app.UI {
return c.alert.Render()
}),
newNav(),
app.Div().ID("list").Class("pure-u-1 inbox-list").Body(
app.Range(c.issues).Slice(func(i int) app.UI {
return app.If(i == 0, func() app.UI {
return app.Div().ID("inbox-item-" + c.issues[i].ID).Class("email-item email-item-unread email-item-selected pure-g").Body(
app.Div().Class("pure-u-1").Body(
app.A().ID("inbox-" + c.issues[i].ID).Href("").Body(
app.H4().Class("email-subject").Text(c.issues[i].Title),
).OnClick(c.onChangeTab),
),
)
}).Else(func() app.UI {
return app.Div().ID("inbox-item-" + c.issues[i].ID).Class("email-item pure-g").Body(
app.Div().Class("pure-u-1").Body(
app.A().ID("inbox-" + c.issues[i].ID).Href("").Body(
app.H4().Class("email-subject").Text(c.issues[i].Title),
).OnClick(c.onChangeTab),
),
)
})
}),
),
// Iterate over issues to display them...
app.Range(c.issues).Slice(func(i int) app.UI {
return app.If(i == 0, func() app.UI {
return c.constructInboxItem(i, "active")
}).Else(func() app.UI {
return c.constructInboxItem(i, "")
})
}),
)
}
func (c *Inbox) constructInboxItem(index int, activeClass string) app.UI {
return app.Div().ID("main-inbox-" + c.issues[index].ID).Class("pure-u-1 inbox " + activeClass).Body(
app.Div().Class("email-content").Body(
app.Div().Class("email-content-header pure-g").Body(
app.Div().Class("pure-u-1").Body(
app.H1().Class("email-content-title").Text(c.issues[index].Title),
),
),
app.Div().Class("email-content-body").Body(
app.P().Text(c.issues[index].Description),
),
),
app.Div().Class("email-content").Body(
app.Div().Class("email-content-header pure-g").Body(
app.Div().Class("pure-u-2-3").Body(
app.H2().Class("email-content-title").Text("Solutions"),
),
app.Div().Class("email-content-controls pure-u-1-3").Body(
app.If(c.issues[index].Veto && len(c.issues[index].Solutions) > 0, func() app.UI {
return app.Button().ID(strconv.Itoa(index)).Class("secondary-button pure-button").Text("Consensus").OnClick(c.onClickConsensus)
}),
app.If(!c.issues[index].Veto, func() app.UI {
return app.Button().ID(strconv.Itoa(index)).Class("secondary-button pure-button").Text("Veto").OnClick(c.onClickVeto)
}),
),
),
),
app.Range(c.issues[index].Solutions).Slice(func(n int) app.UI {
return app.Div().Class("email-content").Body(
app.Div().Class("email-content-body").Body(
app.P().Text(c.issues[index].Solutions[n].Body),
),
)
}),
app.If(c.issues[index].Veto, func() app.UI {
return c.constructSolutionForm(index)
}),
)
}
// func (c *Inbox) onClickVeto(ctx app.Context, e app.Event) {
// e.PreventDefault()
// c.veto = true
// }
// func (c *Inbox) onClickConsensus(ctx app.Context, e app.Event) {
// e.PreventDefault()
// c.veto = false
// }
func (c *Inbox) constructSolutionForm(id int) app.UI {
return app.Div().Class("pure-u-1 form-box").Body(
app.H1().Text("Suggest solution"),
app.Form().ID(strconv.Itoa(id)).Class("pure-form").Body(
app.FieldSet().Class("pure-group").Body(
app.Textarea().ID("reply").Class("pure-input-1").Placeholder("How would you solve the issue?").Cols(50).Rows(10).Required(true),
),
app.Div().Class("email-content-controls pure-u-1").Body(
app.Button().Class("pure-button pure-button-primary").Type("submit").Text("Reply"),
),
).OnSubmit(c.onSubmitSolution),
)
}
func (c *Inbox) onSubmitSolution(ctx app.Context, e app.Event) {
e.PreventDefault()
index := ctx.JSSrc().Get("id").String()
indexInt, err := strconv.Atoi(index)
if err != nil {
log.Fatal(err)
}
reply := app.Window().GetElementByID("reply").Get("value").String()
id := uuid.New()
s := Solution{
ID: id.String(),
Body: reply,
}
c.issues[indexInt].Solutions = append(c.issues[indexInt].Solutions, s)
issue, err := json.Marshal(c.issues[indexInt])
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
ctx.Async(func() {
err = c.sh.OrbitDocsPut(dbNameIssue, issue)
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
err = c.sh.PubSubPublish(topicUpdateIssue, string(issue))
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
})
}
func (c *Inbox) onClickConsensus(ctx app.Context, e app.Event) {
e.PreventDefault()
index := ctx.JSSrc().Get("id").String()
indexInt, err := strconv.Atoi(index)
if err != nil {
log.Fatal(err)
}
c.issues[indexInt].Veto = false
issue, err := json.Marshal(c.issues[indexInt])
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
ctx.Async(func() {
err = c.sh.OrbitDocsPut(dbNameIssue, issue)
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
err = c.sh.PubSubPublish(topicUpdateIssue, string(issue))
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
})
}
func (c *Inbox) onClickVeto(ctx app.Context, e app.Event) {
e.PreventDefault()
index := ctx.JSSrc().Get("id").String()
indexInt, err := strconv.Atoi(index)
if err != nil {
log.Fatal(err)
}
c.issues[indexInt].Veto = true
issue, err := json.Marshal(c.issues[indexInt])
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
ctx.Async(func() {
err = c.sh.OrbitDocsPut(dbNameIssue, issue)
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
err = c.sh.PubSubPublish(topicUpdateIssue, string(issue))
if err != nil {
c.alert.send(ctx, AlertError, "Could not save solution")
log.Fatal(err)
}
})
}