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