Files: 93aed91e8e392b4b011fbacc5cccc010522e986d / book / html / layout / detail.js
2899 bytesRaw
1 | const nest = require('depnest') |
2 | const { h, when, computed } = require('mutant') |
3 | |
4 | exports.needs = nest({ |
5 | 'book.obs.book': 'first', |
6 | 'about.html.image': 'first', |
7 | 'message.html': { |
8 | 'markdown': 'first' |
9 | }, |
10 | 'book.html': { |
11 | 'title': 'first', |
12 | 'authors': 'first', |
13 | 'description': 'first', |
14 | 'images': 'first' |
15 | } |
16 | }) |
17 | |
18 | exports.gives = nest('book.html.layout') |
19 | |
20 | exports.create = (api) => { |
21 | return nest('book.html.layout', bookLayout) |
22 | |
23 | function simpleEdit(isEditing, name, value) { |
24 | return h('div', [h('span', name), |
25 | when(isEditing, |
26 | h('input', {'ev-input': e => value.set(e.target.value), value: value() }), |
27 | h('span', value))]) |
28 | |
29 | } |
30 | |
31 | function textEdit(isEditing, name, value) { |
32 | const markdown = api.message.html.markdown |
33 | const input = h('textarea', {'ev-input': e => value.set(e.target.value), value: value() }) |
34 | |
35 | return h('div', [h('span', name), |
36 | when(isEditing, input, computed(value, markdown))]) |
37 | } |
38 | |
39 | function bookLayout (msg, opts) { |
40 | if (!(opts.layout === undefined || opts.layout === 'detail')) return |
41 | |
42 | const { obs, isEditing, isCard } = opts |
43 | |
44 | const { title, authors, description, images } = api.book.html |
45 | |
46 | let reviews = [] |
47 | |
48 | return h('Message -book-detail', [ |
49 | title({ title: obs.title, msg, isEditing, onUpdate: obs.title.set }), |
50 | authors({authors: obs.authors, isEditing, onUpdate: obs.authors.set}), |
51 | h('section.content', [ |
52 | images({images: obs.images, isEditing, onUpdate: obs.images.add }), |
53 | h('section.description', |
54 | description({description: obs.description, isEditing, onUpdate: obs.description.set})), |
55 | ]), |
56 | h('section.subjective', [ |
57 | computed(obs.subjective, subjectives => { |
58 | let i = 0; |
59 | Object.keys(subjectives).forEach(user => { |
60 | if (i++ < reviews.length) return |
61 | let subjective = obs.subjective.get(user) |
62 | reviews.push([ |
63 | h('section.avatar', {}, api.about.html.image(user)), |
64 | h('section', [ |
65 | textEdit(isEditing, 'Review', subjective.review), |
66 | simpleEdit(isEditing, 'Rating', subjective.rating), |
67 | simpleEdit(isEditing, 'Rating type', subjective.ratingType), |
68 | simpleEdit(isEditing, 'Shelve', subjective.shelve), |
69 | simpleEdit(isEditing, 'Genre', subjective.genre) |
70 | ]) |
71 | ]) |
72 | }) |
73 | |
74 | return reviews |
75 | }) |
76 | ]), |
77 | h('section.actions', [ |
78 | h('button.edit', { 'ev-click': () => isEditing.set(!isEditing()) }, |
79 | when(isEditing, 'Cancel', 'Edit')), |
80 | when(isEditing, h('button', {'ev-click': () => save(obs)}, 'Update')) |
81 | ]) |
82 | ]) |
83 | |
84 | function save (obs) { |
85 | // FIXME: check if anything changed |
86 | obs.amend() |
87 | |
88 | isEditing.set(false) |
89 | } |
90 | } |
91 | } |
92 |
Built with git-ssb-web