git ssb

10+

Matt McKegg / patchwork



Tree: e5ad753b8414d998113f9305e0826f4cf1035fe4

Files: e5ad753b8414d998113f9305e0826f4cf1035fe4 / modules / profile / sheet / edit.js

4363 bytesRaw
1var nest = require('depnest')
2var extend = require('xtend')
3var {Value, h, computed, when} = require('mutant')
4var appRoot = require('app-root-path')
5var i18n = require(appRoot + '/lib/i18n').i18n
6var fallbackImageUrl = 'data:image/gif;base64,R0lGODlhAQABAPAAAP///wAAACH5BAEAAAAALAAAAAABAAEAAAICRAEAOw=='
7
8exports.gives = nest('profile.sheet.edit')
9
10exports.needs = nest({
11 'sheet.display': 'first',
12 'keys.sync.id': 'first',
13 'sbot.async.publish': 'first',
14 'about.obs': {
15 name: 'first',
16 description: 'first',
17 image: 'first',
18 color: 'first'
19 },
20 'blob.html.input': 'first',
21 'blob.sync.url': 'first'
22})
23
24exports.create = function (api) {
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
126function FocusHook () {
127 return function (element) {
128 setTimeout(() => {
129 element.focus()
130 element.select()
131 }, 5)
132 }
133}
134
135function ValueHook (obs) {
136 return function (element) {
137 element.value = obs()
138 element.oninput = function () {
139 obs.set(element.value.trim())
140 }
141 }
142}
143
144function showDialog (opts) {
145 var electron = require('electron')
146 electron.remote.dialog.showMessageBox(electron.remote.getCurrentWindow(), opts)
147}
148

Built with git-ssb-web