git ssb

16+

Dominic / patchbay



Commit 1c20aa10f87f7b23dd262d5570c1bb0ee166a0d2

refactor profile edit to be all mutant, no loose components

mix irving committed on 1/25/2017, 8:07:55 AM
Parent: b1da73e22bda6761baeced6c02888d038c2f9ace

Files changed

modules_basic/avatar/edit.jschanged
modules_basic/avatar/edit.mcsschanged
modules_basic/avatar/edit.jsView
@@ -5,9 +5,9 @@
55 const hypercrop = require('hypercrop')
66 const hyperlightbox = require('hyperlightbox')
77 const h = require('../../h')
88 const {
9- Value, Array: MutantArray, Dict: MutantObject,
9 + Value, Array: MutantArray, Dict: MutantObject, Struct,
1010 map, computed, when, dictToCollection
1111 } = require('@mmckegg/mutant')
1212 const pull = require('pull-stream')
1313 const getAvatar = require('ssb-avatar')
@@ -47,19 +47,26 @@
4747 mcss: () => fs.readFileSync(__filename.replace(/js$/, 'mcss'), 'utf8')
4848 }
4949
5050 function avatar_edit (id) {
51- var img = visualize(new Buffer(id.substring(1), 'base64'), 256)
5251
53- var proposedAvatar = MutantObject()
54- getAvatar({links: api.sbot_links}, self_id, id, (err, avatar) => {
52 + var avatar = Struct({
53 + original: Value(visualize(new Buffer(id.substring(1), 'base64'), 256).src),
54 + new: MutantObject()
55 + })
56 +
57 + getAvatar({links: api.sbot_links}, self_id, id, (err, _avatar) => {
5558 if (err) return console.error(err)
5659 //don't show user has already selected an avatar.
57- if(proposedAvatar.keys().length) return
58- if(ref.isBlob(avatar.image))
59- img.src = api.blob_url(avatar.image)
60 + if(ref.isBlob(_avatar.image))
61 + avatar.original.set(api.blob_url(_avatar.image))
6062 })
6163
64 + var name = Struct({
65 + original: Value(api.avatar_name(id)),
66 + new: Value()
67 + })
68 +
6269 var images = MutantArray()
6370 pull(
6471 api.sbot_links({dest: id, rel: 'about', values: true}),
6572 pull.map(e => e.value.content.image),
@@ -80,70 +87,79 @@
8087 )
8188 var names = dictToCollection(namesRecord)
8289
8390 var lb = hyperlightbox()
84- var name = Value(api.avatar_name(id))
85- var proposedName = Value()
86- var name_input = h('input', {placeholder: ' + another name', 'ev-keyup': (e) => proposedName.set(e.target.value) })
91 +
92 +
8793 var description = '' //TODO load this in, make this editable
8894
89- var isPossibleUpdate = computed([proposedName, proposedAvatar], (name, image) => {
90- return name || Object.keys(image).length
95 + var isPossibleUpdate = computed([name.new, avatar.new], (name, avatar) => {
96 + return name || avatar.link
9197 })
9298
99 + var avatarSrc = computed([avatar], avatar => {
100 + if (avatar.new.link) return api.blob_url(avatar.new.link)
101 + else return avatar.original
102 + })
103 +
104 + var displayedName = computed([name], name => {
105 + if (name.new) return '@'+name.new
106 + else return name.original
107 + })
108 +
93109 return h('ProfileEdit', [
94110 h('section.lightbox', lb),
95111 h('section.avatar', [
96- h('section', img),
97- h('footer', name),
112 + h('section', [
113 + h('img', { src: avatarSrc }),
114 + ]),
115 + h('footer', displayedName),
98116 ]),
99117 h('section.description', description),
100118 h('section.aliases', [
101119 h('header', 'Aliases'),
102120 h('section.avatars', [
103121 h('header', 'Avatars'),
104122 map(images, image => h('img', {
105123 'src': api.blob_url(image),
106- 'ev-click': changeSelectedImage(image)
124 + 'ev-click': () => avatar.new.set(image)
107125 })),
108126 h('div.file-upload', [
109127 hyperfile.asDataURL(dataUrlCallback)
110128 ])
111129 ]),
112130 h('section.names', [
113131 h('header', 'Names'),
114132 h('section', [
115- map(names, name => h('div', [
116- h('div.name', name.key),
117- h('div.count', name.value)
133 + map(names, n => h('div', { 'ev-click': () => name.new.set(n.key()) }, [
134 + h('div.name', n.key),
135 + h('div.count', n.value)
118136 ])),
119- name_input
137 + h('input', {
138 + placeholder: ' + another name',
139 + 'ev-keyup': e => name.new.set(e.target.value)
140 + })
120141 ])
121142 ]),
122- when(isPossibleUpdate, h('button.confirm', { 'ev-click': handleUpdateClick }, 'Confirm changes'))
143 + when(isPossibleUpdate, h('section.action', [
144 + h('button.cancel', { 'ev-click': handleCancelClick }, 'cancel'),
145 + h('button.confirm', { 'ev-click': handleUpdateClick }, 'confirm changes')
146 + ]))
123147 ])
124148 ])
125149
126- function changeSelectedImage (image) {
127- return () => {
128- proposedAvatar.set(image)
129- img.src = api.blob_url(image.link || image)
130- }
131- }
132-
133150 function dataUrlCallback (data) {
134151 var el = crop(data, (err, data) => {
135152 if(data) {
136- img.src = data
137153 var _data = dataurl.parse(data)
138154 pull(
139155 pull.once(_data.data),
140156 api.sbot_blobs_add((err, hash) => {
141157 //TODO. Alerts are EVIL.
142158 //I use them only in a moment of weakness.
143159
144160 if(err) return alert(err.stack)
145- proposedAvatar.set({
161 + avatar.new.set({
146162 link: hash,
147163 size: _data.data.length,
148164 type: _data.mimetype,
149165 width: 512,
@@ -156,23 +172,26 @@
156172 })
157173 lb.show(el)
158174 }
159175
176 + function handleCancelClick () {
177 + name.new.set(null)
178 + avatar.new.set({})
179 + }
180 +
160181 function handleUpdateClick () {
161- const name = proposedName()
162- const avatar = proposedAvatar()
182 + const newName = name.new()
183 + const newAvatar = avatar.new()
163184
164185 const msg = {
165186 type: 'about',
166187 about: id
167188 }
168189
169- if (name) msg.name = name
170- if (Object.keys(avatar).length) msg.image = avatar
190 + if (newName) msg.name = newName
191 + if (newAvatar.link) msg.image = newAvatar
171192
172193 api.message_confirm(msg)
173-
174- if (name) name.set('@'+name)
175194 }
176195 }
177196
178197 }
modules_basic/avatar/edit.mcssView
@@ -117,8 +117,9 @@
117117 flex-wrap: wrap
118118
119119 div {
120120 display: flex
121 + cursor: pointer
121122
122123 border: 1px gainsboro solid
123124 margin: 0 .4rem .5rem 0
124125
@@ -142,14 +143,18 @@
142143 }
143144
144145 }
145146
146- button.confirm {
147- margin-left: 0
148-
149- color: #fff
150- $backgroundPrimary
151- border: none
147 + section.action {
148 + button.cancel {
149 + margin-left: 0
150 + }
151 +
152 + button.confirm {
153 + color: #fff
154 + $backgroundPrimary
155 + border: none
156 + }
152157 }
153158 }
154159 }
155160

Built with git-ssb-web