/
Detail.js
213 lines (204 loc) · 5.52 KB
/
Detail.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import { Platform, View } from 'react-native'
import React, { PureComponent } from 'react'
import { Pagination, RelayContext } from '../../../relay'
import { Text, Flex, Screen, Header, Icon } from '../../Library'
import { colors } from '../../../constants'
import { fragments } from '../../../graphql'
import { merge } from '../../../helpers'
import { shadow } from '../../../styles'
import { conversation as utils } from '../../../utils'
import { parseEmbedded } from '../../../helpers/json'
const Message = fragments.Event(props => {
const conversation = props.navigation.getParam('conversation')
const contactId = props.data.senderId
const isMyself =
conversation.members.find(m => m.contactId === contactId).contact.status ===
42
return (
<Flex.Rows
align={isMyself ? 'end' : 'start'}
style={{ marginHorizontal: 10, marginVertical: 10 }}
>
<Text
padding={{
vertical: 6,
horizontal: 10,
}}
multiline
left={!isMyself}
right={isMyself}
background={colors.blue}
color={colors.white}
rounded={14.5}
margin={{
bottom: 4,
[isMyself ? 'left' : 'right']: 42,
}}
>
{parseEmbedded(props.data.attributes).message.text}
</Text>
<Text
left={!isMyself}
right={isMyself}
tiny
color={colors.subtleGrey}
margin={{
top: 6,
bottom: 6,
[isMyself ? 'left' : 'right']: 42,
}}
>
{new Date(props.data.createdAt).toTimeString()}
{' '}
{isMyself
? <Icon name={props.data.ackedAt ? 'check-circle' : 'circle'} />
: null}
</Text>
</Flex.Rows>
)
})
class Input extends PureComponent {
static contextType = RelayContext
state = {
input: '',
height: 16,
}
onSubmit = () => {
const { input } = this.state
this.setState({ input: '' }, async () => {
try {
const conversation = this.props.navigation.getParam('conversation')
await this.props.screenProps.context.mutations.conversationAddMessage({
conversation: {
id: conversation.id,
},
message: {
text: input,
},
})
} catch (err) {
console.error(err)
}
})
}
render () {
return (
<Flex.Cols style={[shadow]} justify='center' align='center'>
<Flex.Cols
style={{
backgroundColor: colors.grey8,
marginLeft: 16,
borderRadius: 16,
}}
>
<Text
left
middle
margin={{
left: 8,
right: Platform.OS === 'web' ? 8 : 0,
top: this.state.height > 20 ? 0 : 8,
bottom: this.state.height > 20 ? 0 : 8,
}}
lineHeight={this.state.height > 20 ? 0 : 16}
background={colors.grey8}
icon='edit-2'
height={this.state.height}
input={{
onChangeText: input => {
this.setState({ input })
},
onContentSizeChange: ({
nativeEvent: {
contentSize: { height },
},
}) => {
this.setState({
height: height > 80 ? 80 : height,
})
},
placeholder: 'Write a secure message...',
autoFocus: true,
value: this.state.input,
}}
multiline
color={colors.grey5}
/>
</Flex.Cols>
<Text
right
size={0}
middle
margin
height={this.state.height}
icon='send'
color={colors.grey5}
onPress={this.onSubmit}
/>
</Flex.Cols>
)
}
}
export default class Detail extends PureComponent {
static navigationOptions = ({ navigation }) => ({
header: (
<Header
navigation={navigation}
title={utils.getTitle(navigation.getParam('conversation'))}
backBtn
rightBtnIcon='settings'
onPressRightBtn={() =>
navigation.push('chats/settings', {
conversation: navigation.getParam('conversation'),
})
}
/>
),
})
render () {
const conversation = this.props.navigation.getParam('conversation')
const {
navigation,
screenProps: {
context: { queries, subscriptions },
},
} = this.props
return (
<Screen style={{ backgroundColor: colors.white, paddingTop: 0 }}>
<Pagination
direction='forward'
query={queries.EventList.graphql}
variables={merge([
queries.EventList.defaultVariables,
{
filter: {
kind: 302,
conversationId: conversation.id,
},
},
])}
subscriptions={[subscriptions.conversationNewMessage]}
fragment={fragments.EventList}
alias='EventList'
renderItem={props => <Message {...props} navigation={navigation} />}
inverted
style={{ paddingTop: 48 }}
/>
<View
style={{
left: 0,
right: 0,
bottom: 0,
position: 'absolute',
backgroundColor: colors.white,
}}
>
<Input
navigation={this.props.navigation}
screenProps={this.props.screenProps}
/>
</View>
</Screen>
)
}
}