Files: 653e7d95783fcb22649a1ce70a47fc0deb571e3f / bookmark / html / tagAction.js
1367 bytesRaw
1 | var { h, computed, when } = require('mutant') |
2 | var nest = require('depnest') |
3 | |
4 | exports.needs = nest({ |
5 | 'bookmark.obs.bookmark': 'first', |
6 | 'bookmark.async.save': 'first', |
7 | 'keys.sync.id': 'first' |
8 | }) |
9 | |
10 | exports.gives = nest('bookmark.html.tagAction') |
11 | |
12 | exports.create = (api) => { |
13 | return nest('bookmark.html.tagAction', tag) |
14 | |
15 | function tag(tagValue, doActionText, undoActionText) { |
16 | return function(msgId) { |
17 | const id = api.keys.sync.id() |
18 | var bookmark = api.bookmark.obs.bookmark(msgId, id) |
19 | var tagged = computed([bookmark.tags, tagValue], isTagged) |
20 | return when(tagged, |
21 | h('button.undoAction', { |
22 | 'ev-click': () => publishAction(msgId, bookmark.recps(), bookmark.tags(), tagValue, false) |
23 | }, undoActionText), |
24 | h('button.doAction', { |
25 | 'ev-click': () => publishAction(msgId, bookmark.recps(), bookmark.tags(), tagValue, true) |
26 | }, doActionText) |
27 | ) |
28 | } |
29 | } |
30 | |
31 | function publishAction(messageId, recps, bookmarkTags, tagValue, status = true) { |
32 | var currentTags = bookmarkTags || [] |
33 | var tags |
34 | if (status) { |
35 | tags = currentTags |
36 | tags.push(tagValue) |
37 | } else { |
38 | tags = currentTags.filter(t => t !== tagValue) |
39 | } |
40 | api.bookmark.async.save({ recps, messageId, tags }, console.log) |
41 | } |
42 | } |
43 | |
44 | function isTagged(tags, tag) { |
45 | if (!tags) return |
46 | return tags.includes(tag) |
47 | } |
Built with git-ssb-web