-
Notifications
You must be signed in to change notification settings - Fork 80
/
Copy pathedit-profile-window.js
117 lines (102 loc) · 2.75 KB
/
edit-profile-window.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
var h = require('./lib/h')
var computed = require('@mmckegg/mutant/computed')
var when = require('@mmckegg/mutant/when')
var Value = require('@mmckegg/mutant/value')
var electron = require('electron')
module.exports = function (client, config, data) {
var context = {
config,
api: require('./api')(client, config)
}
var imagePath = Value()
var publishing = Value(false)
var imageInput = h('input', {type: 'file', accept: 'image/*'})
var displayName = h('input -name', {
placeholder: 'Choose a display name',
value: data.profile.displayName || ''
})
var description = h('textarea -description', {
rows: 9,
placeholder: 'Anything you want to say about yourself?',
value: data.profile.description || ''
})
var defaultImage = data.profile.image && context.api.getBlobUrl(data.profile.image)
imageInput.onchange = function () {
var file = imageInput.files[0]
if (file) {
imagePath.set(file.path)
}
}
setTimeout(() => {
displayName.focus()
displayName.select()
}, 50)
return h('Dialog', [
h('section EditProfile', [
h('div.image', {
style: {
'background-image': url(computed(imagePath, p => p ? `file://${p}` : defaultImage))
}
}, [
h('span', ['🖼 Choose Profile Image...']), imageInput
]),
h('div.main', [
h('div.info', [
displayName, description
])
])
]),
h('footer', [
when(publishing,
h('button', {'disabled': true}, ['Publishing...']),
[ h('button -save', {'ev-click': save}, ['Save Profile']),
h('button -cancel', {'ev-click': cancel}, ['Cancel'])
]
)
])
])
// scoped
function save () {
publishing.set(true)
var item = {}
if (displayName.value !== (data.profile.displayName || '')) {
item.name = displayName.value
}
if (description.value !== (data.profile.description || '')) {
item.description = description.value
}
if (imagePath()) {
context.api.addBlob(imagePath(), (err, hash) => {
if (err) throw err
console.log('added blob', hash)
item.image = {
link: hash
}
publish(item)
})
} else {
publish(item)
}
}
function publish (item) {
console.log('publishing', item)
if (Object.keys(item)) {
item.type = 'about'
item.about = data.id
context.api.publish(item, function (err) {
if (err) throw err
var window = electron.remote.getCurrentWindow()
window.close()
})
} else {
cancel()
}
}
function cancel () {
var window = electron.remote.getCurrentWindow()
window.close()
}
}
function url (value) {
return computed(value, (v) => v ? `url('${v}')` : '')
}