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