-
Notifications
You must be signed in to change notification settings - Fork 3k
/
TimezoneSelectPage.js
135 lines (119 loc) · 4.96 KB
/
TimezoneSelectPage.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
import lodashGet from 'lodash/get';
import React, {Component} from 'react';
import _ from 'underscore';
import moment from 'moment-timezone';
import withCurrentUserPersonalDetails, {withCurrentUserPersonalDetailsPropTypes, withCurrentUserPersonalDetailsDefaultProps} from '../../../components/withCurrentUserPersonalDetails';
import ScreenWrapper from '../../../components/ScreenWrapper';
import HeaderWithBackButton from '../../../components/HeaderWithBackButton';
import withLocalize, {withLocalizePropTypes} from '../../../components/withLocalize';
import CONST from '../../../CONST';
import * as PersonalDetails from '../../../libs/actions/PersonalDetails';
import compose from '../../../libs/compose';
import Navigation from '../../../libs/Navigation/Navigation';
import ROUTES from '../../../ROUTES';
import SelectionListRadio from '../../../components/SelectionListRadio';
const propTypes = {
...withLocalizePropTypes,
...withCurrentUserPersonalDetailsPropTypes,
};
const defaultProps = {
...withCurrentUserPersonalDetailsDefaultProps,
};
class TimezoneSelectPage extends Component {
constructor(props) {
super(props);
this.saveSelectedTimezone = this.saveSelectedTimezone.bind(this);
this.filterShownTimezones = this.filterShownTimezones.bind(this);
this.getTimezoneOption = this.getTimezoneOption.bind(this);
this.timezone = this.getUserTimezone(props.currentUserPersonalDetails);
this.allTimezones = _.chain(moment.tz.names())
.filter((timezone) => !timezone.startsWith('Etc/GMT'))
.map(this.getTimezoneOption)
.value();
this.state = {
timezoneInputText: this.timezone.selected,
timezoneOptions: this.allTimezones,
};
}
componentDidUpdate() {
// componentDidUpdate is added in order to update the timezone options when automatic is toggled on/off as
// navigating back doesn't unmount the page, thus it won't update the timezone options & stay disabled without this.
const newTimezone = this.getUserTimezone(this.props.currentUserPersonalDetails);
if (_.isEqual(this.timezone, newTimezone)) {
return;
}
this.timezone = newTimezone;
this.allTimezones = _.map(this.allTimezones, (timezone) => {
const text = timezone.text.split('-')[0];
return this.getTimezoneOption(text);
});
this.setState({
timezoneInputText: this.timezone.selected,
timezoneOptions: this.allTimezones,
});
}
/**
* We add the current time to the key to fix a bug where the list options don't update unless the key is updated.
* @param {String} text
* @return {string} key for list item
*/
getKey(text) {
return `${text}-${new Date().getTime()}`;
}
/**
* Get timezone option object for the list.
* @param {String} text
* @return {Object} Timezone list option
*/
getTimezoneOption(text) {
return {
text,
keyForList: this.getKey(text),
isSelected: text === this.timezone.selected,
};
}
/**
* @param {Object} currentUserPersonalDetails
* @return {Object} user's timezone data
*/
getUserTimezone(currentUserPersonalDetails) {
return lodashGet(currentUserPersonalDetails, 'timezone', CONST.DEFAULT_TIME_ZONE);
}
/**
* @param {Object} timezone
* @param {String} timezone.text
*/
saveSelectedTimezone({text}) {
PersonalDetails.updateSelectedTimezone(text);
}
/**
* @param {String} searchText
*/
filterShownTimezones(searchText) {
this.setState({
timezoneInputText: searchText,
timezoneOptions: _.filter(this.allTimezones, (tz) => tz.text.toLowerCase().includes(searchText.trim().toLowerCase())),
});
}
render() {
return (
<ScreenWrapper includeSafeAreaPaddingBottom={false}>
<HeaderWithBackButton
title={this.props.translate('timezonePage.timezone')}
onBackButtonPress={() => Navigation.goBack(ROUTES.SETTINGS_TIMEZONE)}
/>
<SelectionListRadio
textInputLabel={this.props.translate('timezonePage.timezone')}
textInputValue={this.state.timezoneInputText}
onChangeText={this.filterShownTimezones}
onSelectRow={this.saveSelectedTimezone}
sections={[{data: this.state.timezoneOptions, indexOffset: 0, isDisabled: this.timezone.automatic}]}
initiallyFocusedOptionKey={_.get(_.filter(this.state.timezoneOptions, (tz) => tz.text === this.timezone.selected)[0], 'keyForList')}
/>
</ScreenWrapper>
);
}
}
TimezoneSelectPage.propTypes = propTypes;
TimezoneSelectPage.defaultProps = defaultProps;
export default compose(withLocalize, withCurrentUserPersonalDetails)(TimezoneSelectPage);