-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
RequestCallPage.js
217 lines (198 loc) · 7.99 KB
/
RequestCallPage.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
214
215
216
217
import React, {Component} from 'react';
import {View, TextInput} from 'react-native';
import _ from 'underscore';
import {withOnyx} from 'react-native-onyx';
import PropTypes from 'prop-types';
import Str from 'expensify-common/lib/str';
import HeaderWithCloseButton from '../components/HeaderWithCloseButton';
import Navigation from '../libs/Navigation/Navigation';
import styles from '../styles/styles';
import ScreenWrapper from '../components/ScreenWrapper';
import withLocalize, {withLocalizePropTypes} from '../components/withLocalize';
import ONYXKEYS from '../ONYXKEYS';
import compose from '../libs/compose';
import FullNameInputRow from '../components/FullNameInputRow';
import Button from '../components/Button';
import FixedFooter from '../components/FixedFooter';
import CONST from '../CONST';
import Growl from '../libs/Growl';
import {requestConciergeDMCall} from '../libs/actions/Inbox';
import {fetchOrCreateChatReport} from '../libs/actions/Report';
import personalDetailsPropType from './personalDetailsPropType';
import Text from '../components/Text';
const propTypes = {
...withLocalizePropTypes,
/** The personal details of the person who is logged in */
myPersonalDetails: personalDetailsPropType.isRequired,
/** Current user session */
session: PropTypes.shape({
email: PropTypes.string.isRequired,
}).isRequired,
/** The details about the user that is signed in */
user: PropTypes.shape({
/** Whether or not the user is subscribed to news updates */
loginList: PropTypes.arrayOf(PropTypes.shape({
/** Phone/Email associated with user */
partnerUserID: PropTypes.string,
})),
}).isRequired,
/** The policies which the user has access to */
policies: PropTypes.shape({
/** ID of the policy */
policyID: PropTypes.string,
/** The type of the policy */
type: PropTypes.string,
}).isRequired,
};
class RequestCallPage extends Component {
constructor(props) {
super(props);
const {firstName, lastName} = this.getFirstAndLastName(props.myPersonalDetails);
this.state = {
firstName,
lastName,
phoneNumber: this.getPhoneNumber(props.user.loginList) ?? '',
isLoading: false,
};
this.onSubmit = this.onSubmit.bind(this);
this.getPhoneNumber = this.getPhoneNumber.bind(this);
this.getFirstAndLastName = this.getFirstAndLastName.bind(this);
}
onSubmit() {
this.setState({isLoading: true});
if (!this.state.firstName.length || !this.state.lastName.length) {
Growl.success(this.props.translate('requestCallPage.growlMessageEmptyName'));
this.setState({isLoading: false});
return;
}
const personalPolicy = _.find(this.props.policies, policy => policy.type === CONST.POLICY.TYPE.PERSONAL);
if (!personalPolicy) {
Growl.error(this.props.translate('requestCallPage.growlMessageNoPersonalPolicy'), 3000);
return;
}
requestConciergeDMCall(personalPolicy.id, this.state.firstName, this.state.lastName, this.state.phoneNumber)
.then((result) => {
this.setState({isLoading: false});
if (result.jsonCode === 200) {
Growl.success(this.props.translate('requestCallPage.growlMessageOnSave'));
fetchOrCreateChatReport([this.props.session.email, CONST.EMAIL.CONCIERGE], true);
return;
}
// Phone number validation is handled by the API
Growl.error(result.message, 3000);
});
}
/**
* Gets the user's phone number from their secondary login.
* Returns null if it doesn't exist.
* @param {Array<Object>} loginList
*
* @returns {String|null}
*/
getPhoneNumber(loginList) {
const secondaryLogin = _.find(loginList, login => Str.isSMSLogin(login.partnerUserID));
return secondaryLogin ? Str.removeSMSDomain(secondaryLogin.partnerUserID) : null;
}
/**
* Gets the first and last name from the user's personal details.
* If the login is the same as the displayName, then they don't exist,
* so we return empty strings instead.
* @param {String} login
* @param {String} displayName
*
* @returns {Object}
*/
getFirstAndLastName({login, displayName}) {
let firstName;
let lastName;
if (login === displayName) {
firstName = '';
lastName = '';
} else {
const firstSpaceIndex = displayName.indexOf(' ');
const lastSpaceIndex = displayName.lastIndexOf(' ');
if (firstSpaceIndex === -1) {
firstName = displayName;
lastName = '';
} else {
firstName = displayName.substring(0, firstSpaceIndex);
lastName = displayName.substring(lastSpaceIndex);
}
}
return {firstName, lastName};
}
render() {
const isButtonDisabled = false;
return (
<ScreenWrapper>
<HeaderWithCloseButton
title={this.props.translate('requestCallPage.requestACall')}
shouldShowBackButton
onBackButtonPress={() => fetchOrCreateChatReport([
this.props.session.email,
CONST.EMAIL.CONCIERGE,
], true)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<View style={[styles.flex1, styles.p5]}>
<Text style={[styles.mb4]}>
{this.props.translate('requestCallPage.description')}
</Text>
<Text style={[styles.mt4, styles.mb4]}>
{this.props.translate('requestCallPage.instructions')}
</Text>
<FullNameInputRow
firstName={this.state.firstName}
lastName={this.state.lastName}
onChangeFirstName={firstName => this.setState({firstName})}
onChangeLastName={lastName => this.setState({lastName})}
style={[styles.mt4, styles.mb4]}
/>
<Text style={[styles.mt4, styles.formLabel]} numberOfLines={1}>
{this.props.translate('common.phoneNumber')}
</Text>
<TextInput
autoCompleteType="off"
autoCorrect={false}
style={[styles.textInput]}
value={this.state.phoneNumber}
placeholder="+14158675309"
onChangeText={phoneNumber => this.setState({phoneNumber})}
/>
<Text style={[styles.mt4, styles.textLabel, styles.colorMuted, styles.mb6]}>
{this.props.translate('requestCallPage.availabilityText')}
</Text>
</View>
<FixedFooter>
<Button
success
isDisabled={isButtonDisabled}
onPress={this.onSubmit}
style={[styles.w100]}
text={this.props.translate('requestCallPage.callMe')}
isLoading={this.state.isLoading}
/>
</FixedFooter>
</ScreenWrapper>
);
}
}
RequestCallPage.displayName = 'RequestCallPage';
RequestCallPage.propTypes = propTypes;
export default compose(
withLocalize,
withOnyx({
myPersonalDetails: {
key: ONYXKEYS.MY_PERSONAL_DETAILS,
},
session: {
key: ONYXKEYS.SESSION,
},
user: {
key: ONYXKEYS.USER,
},
policies: {
key: ONYXKEYS.COLLECTION.POLICY,
},
}),
)(RequestCallPage);