-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
AddressPage.js
226 lines (208 loc) · 8.35 KB
/
AddressPage.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
218
219
220
221
222
223
224
225
226
import lodashGet from 'lodash/get';
import _ from 'underscore';
import React, {Component} from 'react';
import PropTypes from 'prop-types';
import {View} from 'react-native';
import {CONST as COMMON_CONST} from 'expensify-common/lib/CONST';
import {withOnyx} from 'react-native-onyx';
import ScreenWrapper from '../../../../components/ScreenWrapper';
import HeaderWithCloseButton from '../../../../components/HeaderWithCloseButton';
import withLocalize, {withLocalizePropTypes} from '../../../../components/withLocalize';
import ROUTES from '../../../../ROUTES';
import Form from '../../../../components/Form';
import ONYXKEYS from '../../../../ONYXKEYS';
import CONST from '../../../../CONST';
import TextInput from '../../../../components/TextInput';
import styles from '../../../../styles/styles';
import Navigation from '../../../../libs/Navigation/Navigation';
import * as PersonalDetails from '../../../../libs/actions/PersonalDetails';
import compose from '../../../../libs/compose';
import AddressSearch from '../../../../components/AddressSearch';
import CountryPicker from '../../../../components/CountryPicker';
import StatePicker from '../../../../components/StatePicker';
const propTypes = {
/* Onyx Props */
/** User's private personal details */
privatePersonalDetails: PropTypes.shape({
/** User's home address */
address: PropTypes.shape({
street: PropTypes.string,
city: PropTypes.string,
state: PropTypes.string,
zip: PropTypes.string,
country: PropTypes.string,
}),
}),
...withLocalizePropTypes,
};
const defaultProps = {
privatePersonalDetails: {
address: {
street: '',
city: '',
state: '',
zip: '',
country: '',
},
},
};
class AddressPage extends Component {
constructor(props) {
super(props);
this.validate = this.validate.bind(this);
this.updateAddress = this.updateAddress.bind(this);
this.onCountryUpdate = this.onCountryUpdate.bind(this);
const currentCountry = lodashGet(props.privatePersonalDetails, 'address.country') || '';
this.state = {
isUsaForm: currentCountry === CONST.USA_COUNTRY_NAME,
};
}
/**
* @param {String} newCountry - new country selected in form
*/
onCountryUpdate(newCountry) {
if (newCountry === CONST.USA_COUNTRY_NAME) {
this.setState({isUsaForm: true});
} else {
this.setState({isUsaForm: false});
}
}
/**
* Submit form to update user's first and last legal name
* @param {Object} values - form input values
*/
updateAddress(values) {
PersonalDetails.updateAddress(
values.addressLine1.trim(),
values.addressLine2.trim(),
values.city.trim(),
values.state.trim(),
values.zipPostCode,
values.country,
);
}
/**
* @param {Object} values - form input values
* @returns {Object} - An object containing the errors for each inputID
*/
validate(values) {
const errors = {};
const requiredFields = [
'addressLine1',
'city',
'zipPostCode',
'country',
'state',
];
// Check "State" dropdown is a valid state if selected Country is USA.
if (this.state.isUsaForm && !COMMON_CONST.STATES[values.state]) {
errors.state = this.props.translate('common.error.fieldRequired');
}
// Add "Field required" errors if any required field is empty
_.each(requiredFields, (fieldKey) => {
if (!_.isEmpty(values[fieldKey])) {
return;
}
errors[fieldKey] = this.props.translate('common.error.fieldRequired');
});
return errors;
}
render() {
const address = lodashGet(this.props.privatePersonalDetails, 'address') || {};
const [street1, street2] = (address.street || '').split('\n');
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithCloseButton
title={this.props.translate('privatePersonalDetails.homeAddress')}
shouldShowBackButton
onBackButtonPress={() => Navigation.navigate(ROUTES.SETTINGS_PERSONAL_DETAILS)}
onCloseButtonPress={() => Navigation.dismissModal(true)}
/>
<Form
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.HOME_ADDRESS_FORM}
validate={this.validate}
onSubmit={this.updateAddress}
submitButtonText={this.props.translate('common.save')}
enabledWhenOffline
>
<View style={styles.mb4}>
<AddressSearch
inputID="addressLine1"
label={this.props.translate('common.addressLine', {lineNumber: 1})}
defaultValue={street1 || ''}
isLimitedToUSA={false}
renamedInputKeys={{
street: 'addressLine1',
city: 'city',
state: 'state',
zipCode: 'zipPostCode',
country: 'country',
}}
maxInputLength={CONST.FORM_CHARACTER_LIMIT}
/>
</View>
<View style={styles.mb4}>
<TextInput
inputID="addressLine2"
label={this.props.translate('common.addressLine', {lineNumber: 2})}
defaultValue={street2 || ''}
maxLength={CONST.FORM_CHARACTER_LIMIT}
/>
</View>
<View style={styles.mb4}>
<TextInput
inputID="city"
label={this.props.translate('common.city')}
defaultValue={address.city || ''}
maxLength={CONST.FORM_CHARACTER_LIMIT}
/>
</View>
<View style={[styles.flexRow, styles.mb4]}>
<View style={[styles.flex1, styles.mr2]}>
{this.state.isUsaForm ? (
<StatePicker
inputID="state"
defaultValue={address.state || ''}
/>
) : (
<TextInput
inputID="state"
label={this.props.translate('common.stateOrProvince')}
defaultValue={address.state || ''}
maxLength={CONST.FORM_CHARACTER_LIMIT}
/>
)}
</View>
<View style={[styles.flex1]}>
<TextInput
inputID="zipPostCode"
label={this.props.translate('common.zipPostCode')}
keyboardType={CONST.KEYBOARD_TYPE.NUMBER_PAD}
defaultValue={address.zip || ''}
maxLength={CONST.BANK_ACCOUNT.MAX_LENGTH.ZIP_CODE}
/>
</View>
</View>
<View>
<CountryPicker
inputID="country"
onValueChange={this.onCountryUpdate}
defaultValue={address.country || ''}
/>
</View>
</Form>
</ScreenWrapper>
);
}
}
AddressPage.propTypes = propTypes;
AddressPage.defaultProps = defaultProps;
export default compose(
withLocalize,
withOnyx({
privatePersonalDetails: {
key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS,
},
}),
)(AddressPage);