-
Notifications
You must be signed in to change notification settings - Fork 375
/
PublicKeyWithActions.js
193 lines (180 loc) · 4.79 KB
/
PublicKeyWithActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { ScrollView, TextInput, Platform, Clipboard } from 'react-native'
import { btoa } from 'b64-lite'
import React, { PureComponent } from 'react'
import { Button, Flex, TextInputMultilineFix, Text } from './index'
import { RelayContext } from '../../relay'
import { colors } from '../../constants'
import {
extractPublicKeyFromId,
shareLinkOther,
shareLinkSelf,
} from '../../helpers/contacts'
import { marginTop, padding, rounded, textTiny } from '../../styles'
const CopyKeyButton = ({ id }) => (
<ActionButton
icon='copy'
onPress={() => Clipboard.setString(id)}
label={'Copy the key'}
/>
)
const ShareKeyButton = ({ id, displayName, self }) => (
<ActionButton
icon='share'
onPress={() =>
self
? shareLinkSelf({ id, displayName })
: shareLinkOther({ id, displayName })
}
label={'Share the key'}
/>
)
const ActionButton = props => (
<Button
icon={props.icon}
background={colors.blue}
margin
padding
rounded={23}
height={24}
medium
middle
center
self='stretch'
onPress={props.onPress}
>
{props.label}
</Button>
)
const AddButton = ({ onPress }) => (
<ActionButton icon='plus' onPress={onPress} label={'Add this key'} />
)
export default class PublicKeyWithActions extends PureComponent {
static contextType = RelayContext
constructor (props) {
super(props)
const [initialKey, initialName] = props.navigation
? [
props.navigation.getParam('initialKey'),
props.navigation.getParam('initialName'),
]
: [props.initialKey, props.initialName]
const missingInitialData = props.initialKey === undefined
this.state = {
err: null,
contact: {
id: initialKey || '',
displayName: initialName || '',
displayStatus: '',
overrideDisplayName: '',
overrideDisplayStatus: '',
},
}
if (missingInitialData && props.data !== undefined) {
try {
this.state.contact = {
...props.data,
id: extractPublicKeyFromId(props.data.id),
}
} catch (e) {
console.error(e)
}
}
}
onSubmit = async () => {
const input = {
contact: {
...this.state.contact,
id: btoa(`contact:${this.state.contact.id}`),
displayName: this.state.contact.displayName,
},
introText: '',
}
try {
await this.props.screenProps.context.mutations.contactRequest(input)
this.props.navigation.goBack(null)
} catch (err) {
this.setState({ err })
}
}
render () {
const {
navigation,
shareButton,
addButton,
copyButton,
readOnly,
self,
} = this.props
const {
contact: { id, displayName },
} = this.state
let errors = []
try {
errors = this.state.err.res.errors
} catch (e) {
// noop
}
return (
<ScrollView>
<Flex.Rows style={[padding]} align='center'>
<TextInput
placeholder={'Contact name (optional)'}
onChangeText={displayName =>
this.setState({ contact: { ...this.state.contact, displayName } })
}
value={displayName}
style={[
{
backgroundColor: colors.grey7,
color: colors.black,
textAlign: 'left',
width: 330,
flex: 0,
...(Platform.OS === 'web' ? { outline: 'none' } : {}),
},
padding,
rounded,
]}
/>
<TextInputMultilineFix
style={[
{
width: 330,
height: 165,
backgroundColor: colors.grey7,
color: colors.black,
flexWrap: 'wrap',
fontFamily: Platform.OS === 'ios' ? 'Courier' : 'monospace',
},
textTiny,
padding,
marginTop,
rounded,
]}
multiline
placeholder='Type or copy/paste a berty user public key here'
value={id}
onChangeText={id =>
this.setState({ contact: { ...this.state.contact, id } })
}
editable={!readOnly}
selectTextOnFocus
/>
{shareButton ? (
<ShareKeyButton id={id} displayName={displayName} self={self} />
) : null}
{copyButton ? <CopyKeyButton id={id} /> : null}
{addButton ? (
<AddButton
id={id}
displayName={displayName}
navigation={navigation}
onPress={this.onSubmit}
/>
) : null}
{errors.map((err, i) => <Text multiline key={i}>{err.message}</Text>)}
</Flex.Rows>
</ScrollView>
)
}
}