Commit 1d5d3beb765e9f7bb08097a0896eb8ea83eeccca
Refactor code and move edit rating to self
Anders Rune Jensen committed on 12/28/2017, 10:28:39 AMParent: 7a8f0c745fefe4f62fc12bf825ad78827680a21d
Files changed
book/html/layout/detail.js | changed |
book/html/layout/detail.mcss | changed |
book/html/layout/detail.js | ||
---|---|---|
@@ -90,21 +90,81 @@ | ||
90 | 90 … | ) |
91 | 91 … | ]) |
92 | 92 … | } |
93 | 93 … | |
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 … | + | |
94 | 145 … | function bookLayout(msg, opts) { |
95 | 146 … | const { layout, obs, isEditing, isCard } = opts |
96 | 147 … | |
97 | 148 … | if (layout !== undefined && layout !== 'detail') return |
98 | 149 … | |
99 | 150 … | const { title, authors, description, |
100 | 151 … | series, seriesNo, images } = api.book.html |
101 | 152 … | |
102 | - let isEditingSubjective = Value(false) | |
103 | - let originalSubjective = {} | |
104 | 153 … | let originalBook = {} |
105 | 154 … | let reviews = [] |
106 | 155 … | |
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 … | + | |
107 | 167 … | return [h('Message -book-detail', [ |
108 | 168 … | title({ title: obs.title, msg, isEditing, onUpdate: obs.title.set }), |
109 | 169 … | series({ series: obs.series, seriesNo: obs.seriesNo, msg, isEditing, |
110 | 170 … | onUpdate: obs.series.set, onUpdateNo: obs.seriesNo.set }), |
@@ -114,79 +174,28 @@ | ||
114 | 174 … | h('section.description', |
115 | 175 … | description({description: obs.description, isEditing, onUpdate: obs.description.set})), |
116 | 176 … | ]), |
117 | 177 … | 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 }, | |
128 | 179 … | when(isEditing, 'Cancel', 'Edit book')), |
129 | 180 … | when(isEditing, h('button', {'ev-click': () => saveBook(obs)}, 'Update book')) |
130 | 181 … | ]), |
131 | 182 … | h('section.subjective', [ |
132 | 183 … | computed(obs.subjective, subjectives => { |
133 | 184 … | let i = 0; |
134 | 185 … | Object.keys(subjectives).forEach(user => { |
135 | 186 … | 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)) | |
152 | 188 … | }) |
153 | 189 … | |
154 | 190 … | return reviews |
155 | 191 … | }) |
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 … | + ]) | |
178 | 193 … | ])] |
179 | 194 … | |
180 | 195 … | function saveBook(obs) { |
181 | 196 … | obs.amend() |
182 | 197 … | |
183 | 198 … | isEditing.set(false) |
184 | 199 … | } |
185 | - | |
186 | - function saveSubjective(obs) { | |
187 | - obs.updateSubjective() | |
188 | - | |
189 | - isEditingSubjective.set(false) | |
190 | - } | |
191 | 200 … | } |
192 | 201 … | } |
Built with git-ssb-web