git ssb

3+

arj / patchbook



Commit 1d5d3beb765e9f7bb08097a0896eb8ea83eeccca

Refactor code and move edit rating to self

Anders Rune Jensen committed on 12/28/2017, 10:28:39 AM
Parent: 7a8f0c745fefe4f62fc12bf825ad78827680a21d

Files changed

book/html/layout/detail.jschanged
book/html/layout/detail.mcsschanged
book/html/layout/detail.jsView
@@ -90,21 +90,81 @@
9090 )
9191 ])
9292 }
9393
94 + function saveSubjective(obs, isEditingSubjective) {
95 + obs.updateSubjective()
96 + isEditingSubjective.set(false)
97 + }
98 +
99 + function handleSubjective(user, i, obs) {
100 + let originalSubjective = {}
101 + let isEditingSubjective = Value(false)
102 + let subjective = obs.subjective.get(user)
103 + let isMe = Value(api.keys.sync.id() == user)
104 + let isOwnEditingSubj = computed([isEditingSubjective, isMe],
105 + (e, me) => { return e && me })
106 + let showRating = computed([subjective.rating, isEditingSubjective, isMe],
107 + (v, e, me) => { return v || (e && me) })
108 +
109 + function editRatingClick() {
110 + if (isEditingSubjective()) { // cancel
111 + if (obs.subjective.has(api.keys.sync.id())) {
112 + let subj = obs.subjective.get(api.keys.sync.id())
113 + Object.keys(originalSubjective).forEach((v) => {
114 + subj[v].set(originalSubjective[v])
115 + })
116 + }
117 + } else {
118 + if (obs.subjective.has(api.keys.sync.id()))
119 + originalSubjective = JSON.parse(JSON.stringify(obs.subjective.get(api.keys.sync.id())()))
120 + }
121 +
122 + isEditingSubjective.set(!isEditingSubjective())
123 + }
124 +
125 + return [
126 + h('section',
127 + [api.about.html.image(user),
128 + h('span.text', [api.about.obs.name(user), when(showRating, ' rated ')]),
129 + ratingEdit(isOwnEditingSubj, subjective.rating),
130 + ratingTypeEdit(isOwnEditingSubj, subjective.ratingType)]),
131 + simpleEdit(isOwnEditingSubj, 'Shelve', subjective.shelve),
132 + simpleEdit(isOwnEditingSubj, 'Genre', subjective.genre),
133 + textEdit(isOwnEditingSubj, 'Review', subjective.review),
134 + h('section.actions',
135 + when(isMe, [
136 + h('button.subjective', { 'ev-click': editRatingClick },
137 + when(isEditingSubjective, 'Cancel', 'Edit my rating')),
138 + when(isEditingSubjective,
139 + h('button', { 'ev-click': () => saveSubjective(obs, isEditingSubjective) },
140 + 'Update rating'))
141 + ]))
142 + ]
143 + }
144 +
94145 function bookLayout(msg, opts) {
95146 const { layout, obs, isEditing, isCard } = opts
96147
97148 if (layout !== undefined && layout !== 'detail') return
98149
99150 const { title, authors, description,
100151 series, seriesNo, images } = api.book.html
101152
102- let isEditingSubjective = Value(false)
103- let originalSubjective = {}
104153 let originalBook = {}
105154 let reviews = []
106155
156 + function editClick() {
157 + if (isEditing()) { // cancel
158 + Object.keys(originalBook).forEach((v) => {
159 + obs[v].set(originalBook[v])
160 + })
161 + } else
162 + originalBook = JSON.parse(JSON.stringify(obs()))
163 +
164 + isEditing.set(!isEditing())
165 + }
166 +
107167 return [h('Message -book-detail', [
108168 title({ title: obs.title, msg, isEditing, onUpdate: obs.title.set }),
109169 series({ series: obs.series, seriesNo: obs.seriesNo, msg, isEditing,
110170 onUpdate: obs.series.set, onUpdateNo: obs.seriesNo.set }),
@@ -114,79 +174,28 @@
114174 h('section.description',
115175 description({description: obs.description, isEditing, onUpdate: obs.description.set})),
116176 ]),
117177 h('section.actions', [
118- h('button.edit', { 'ev-click': () => {
119- if (isEditing()) { // cancel
120- Object.keys(originalBook).forEach((v) => {
121- obs[v].set(originalBook[v])
122- })
123- } else
124- originalBook = JSON.parse(JSON.stringify(obs()))
125-
126- isEditing.set(!isEditing())
127- } },
178 + h('button.edit', { 'ev-click': editClick },
128179 when(isEditing, 'Cancel', 'Edit book')),
129180 when(isEditing, h('button', {'ev-click': () => saveBook(obs)}, 'Update book'))
130181 ]),
131182 h('section.subjective', [
132183 computed(obs.subjective, subjectives => {
133184 let i = 0;
134185 Object.keys(subjectives).forEach(user => {
135186 if (i++ < reviews.length) return
136- let subjective = obs.subjective.get(user)
137- let isMe = Value(api.keys.sync.id() == user)
138- let isOwnEditingSubj = computed([isEditingSubjective, isMe],
139- (e, me) => { return e && me })
140- let showRating = computed([subjective.rating, isEditingSubjective, isMe],
141- (v, e, me) => { return v || (e && me) })
142- reviews.push([
143- h('section',
144- [api.about.html.image(user),
145- h('span.text', [api.about.obs.name(user), when(showRating, ' rated ')]),
146- ratingEdit(isOwnEditingSubj, subjective.rating),
147- ratingTypeEdit(isOwnEditingSubj, subjective.ratingType)]),
148- simpleEdit(isOwnEditingSubj, 'Shelve', subjective.shelve),
149- simpleEdit(isOwnEditingSubj, 'Genre', subjective.genre),
150- textEdit(isOwnEditingSubj, 'Review', subjective.review)
151- ])
187 + reviews.push(handleSubjective(user, i, obs))
152188 })
153189
154190 return reviews
155191 })
156- ]),
157- h('section.actions', [
158- h('button.subjective', {
159- 'ev-click': () => {
160- if (isEditingSubjective()) { // cancel
161- if (obs.subjective.has(api.keys.sync.id())) {
162- let subj = obs.subjective.get(api.keys.sync.id())
163- Object.keys(originalSubjective).forEach((v) => {
164- subj[v].set(originalSubjective[v])
165- })
166- }
167- } else {
168- if (obs.subjective.has(api.keys.sync.id()))
169- originalSubjective = JSON.parse(JSON.stringify(obs.subjective.get(api.keys.sync.id())()))
170- }
171-
172- isEditingSubjective.set(!isEditingSubjective())
173- }
174- },
175- when(isEditingSubjective, 'Cancel', 'Edit my rating')),
176- when(isEditingSubjective, h('button', { 'ev-click': () => saveSubjective(obs) }, 'Update rating'))
177- ]),
192 + ])
178193 ])]
179194
180195 function saveBook(obs) {
181196 obs.amend()
182197
183198 isEditing.set(false)
184199 }
185-
186- function saveSubjective(obs) {
187- obs.updateSubjective()
188-
189- isEditingSubjective.set(false)
190- }
191200 }
192201 }
book/html/layout/detail.mcssView
@@ -27,8 +27,14 @@
2727 }
2828 }
2929
3030 section.subjective {
31 + section.actions {
32 + flex-basis: 100%
33 + display: flex
34 + justify-content: flex-end
35 + }
36 +
3137 div {
3238 -contracted {
3339 display: none
3440 }

Built with git-ssb-web