Files: ffb7bedc3c585728df51f065d1d5d2f2d826319e / modules / profile / sheet / edit.js
4183 bytesRaw
1 | var nest = require('depnest') |
2 | var extend = require('xtend') |
3 | var {Value, h, computed, when} = require('mutant') |
4 | var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw==' |
5 | |
6 | exports.gives = nest('profile.sheet.edit') |
7 | |
8 | exports.needs = nest({ |
9 | 'sheet.display': 'first', |
10 | 'keys.sync.id': 'first', |
11 | 'sbot.async.publish': 'first', |
12 | 'about.obs': { |
13 | name: 'first', |
14 | description: 'first', |
15 | image: 'first', |
16 | color: 'first' |
17 | }, |
18 | 'blob.html.input': 'first', |
19 | 'blob.sync.url': 'first' |
20 | }) |
21 | |
22 | exports.create = function (api) { |
23 | return nest('profile.sheet.edit', function () { |
24 | var id = api.keys.sync.id() |
25 | api.sheet.display(close => { |
26 | var currentName = api.about.obs.name(id) |
27 | var currentImage = api.about.obs.image(id) |
28 | var currentDescription = api.about.obs.description(id) |
29 | |
30 | var publishing = Value(false) |
31 | var chosenImage = Value(currentImage()) |
32 | |
33 | // don't display if name is default |
34 | var chosenName = Value(currentName() === id.slice(1, 10) ? '' : currentName()) |
35 | var chosenDescription = Value(currentDescription()) |
36 | |
37 | return { |
38 | content: h('div', { |
39 | style: { |
40 | padding: '20px', |
41 | 'text-align': 'center' |
42 | } |
43 | }, [ |
44 | h('h2', { |
45 | style: { |
46 | 'font-weight': 'normal' |
47 | } |
48 | }, ['Your Profile']), |
49 | h('ProfileEditor', [ |
50 | h('div.side', [ |
51 | h('ImageInput', [ |
52 | h('img', { |
53 | style: { 'background-color': api.about.obs.color(id) }, |
54 | src: computed(chosenImage, (id) => id ? api.blob.sync.url(id) : fallbackImageUrl) |
55 | }), |
56 | h('span', ['🖼 Choose Profile Image...']), |
57 | api.blob.html.input(file => { |
58 | chosenImage.set(file.link) |
59 | }, { |
60 | accept: 'image/*', |
61 | resize: { width: 500, height: 500 } |
62 | }) |
63 | ]) |
64 | ]), |
65 | h('div.main', [ |
66 | h('input.name', { |
67 | placeholder: 'Choose a name', |
68 | hooks: [ValueHook(chosenName), FocusHook()] |
69 | }), |
70 | h('textarea.description', { |
71 | placeholder: 'Describe yourself (if you want)', |
72 | hooks: [ValueHook(chosenDescription)] |
73 | }) |
74 | ]) |
75 | ]) |
76 | ]), |
77 | footer: [ |
78 | h('button -save', { |
79 | 'ev-click': save, |
80 | 'disabled': publishing |
81 | }, when(publishing, 'Publishing...', 'Publish')), |
82 | h('button -cancel', { |
83 | 'ev-click': close |
84 | }, 'Cancel') |
85 | ] |
86 | } |
87 | |
88 | function save () { |
89 | // no confirm |
90 | var update = {} |
91 | var newName = chosenName().trim() || currentName() |
92 | |
93 | if (chosenImage() !== currentImage()) update.image = chosenImage() |
94 | if (newName !== currentName()) update.name = newName |
95 | if (chosenDescription() !== currentDescription()) update.description = chosenDescription() |
96 | |
97 | if (Object.keys(update).length) { |
98 | publishing.set(true) |
99 | api.sbot.async.publish(extend({ |
100 | type: 'about', |
101 | about: id |
102 | }, update), (err) => { |
103 | if (err) { |
104 | publishing.set(false) |
105 | showDialog({ |
106 | type: 'error', |
107 | title: 'Error', |
108 | buttons: ['OK'], |
109 | message: 'An error occurred while attempting to publish about message.', |
110 | detail: err.message |
111 | }) |
112 | } else { |
113 | close() |
114 | } |
115 | }) |
116 | } else { |
117 | close() |
118 | } |
119 | } |
120 | }) |
121 | }) |
122 | } |
123 | |
124 | function FocusHook () { |
125 | return function (element) { |
126 | setTimeout(() => { |
127 | element.focus() |
128 | element.select() |
129 | }, 5) |
130 | } |
131 | } |
132 | |
133 | function ValueHook (obs) { |
134 | return function (element) { |
135 | element.value = obs() |
136 | element.oninput = function () { |
137 | obs.set(element.value.trim()) |
138 | } |
139 | } |
140 | } |
141 | |
142 | function showDialog (opts) { |
143 | var electron = require('electron') |
144 | electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), opts) |
145 | } |
146 |
Built with git-ssb-web