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