index.tsx 5.19 KB
Newer Older
1
import React from 'react';
2
import { StyleSheet, Text } from 'react-native';
3
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
4
5
6
7
import { connect } from 'react-redux';

import { themes } from '../../constants/colors';
import StatusBar from '../../containers/StatusBar';
8
import * as List from '../../containers/List';
9
10
11
12
13
14
import I18n from '../../i18n';
import RocketChat from '../../lib/rocketchat';
import { withTheme } from '../../theme';
import SafeAreaView from '../../containers/SafeAreaView';
import ActivityIndicator from '../../containers/ActivityIndicator';
import { getUserSelector } from '../../selectors/login';
15
import sharedStyles from '../Styles';
16
import { OPTIONS } from './options';
17
18
19
20
21
22
23

const styles = StyleSheet.create({
	pickerText: {
		...sharedStyles.textRegular,
		fontSize: 16
	}
});
24

25
type TKey = 'desktopNotifications' | 'pushNotifications' | 'emailNotificationMode';
26
27
28
29

interface IUserNotificationPreferencesViewState {
	preferences: {
		desktopNotifications?: string;
30
		pushNotifications?: string;
31
32
33
34
		emailNotificationMode?: string;
	};
	loading: boolean;
}
35

36
37
38
39
40
interface IUserNotificationPreferencesViewProps {
	navigation: StackNavigationProp<any, 'UserNotificationPreferencesView'>;
	theme: string;
	user: {
		id: string;
41
	};
42
}
43

44
45
46
47
48
49
50
51
52
class UserNotificationPreferencesView extends React.Component<
	IUserNotificationPreferencesViewProps,
	IUserNotificationPreferencesViewState
> {
	static navigationOptions = (): StackNavigationOptions => ({
		title: I18n.t('Notification_Preferences')
	});

	constructor(props: IUserNotificationPreferencesViewProps) {
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
		super(props);
		this.state = {
			preferences: {},
			loading: false
		};
	}

	async componentDidMount() {
		const { user } = this.props;
		const { id } = user;
		const result = await RocketChat.getUserPreferences(id);
		const { preferences } = result;
		this.setState({ preferences, loading: true });
	}

68
	findDefaultOption = (key: TKey) => {
69
70
71
		const { preferences } = this.state;
		const option = preferences[key] ? OPTIONS[key].find(item => item.value === preferences[key]) : OPTIONS[key][0];
		return option;
72
	};
73

74
	renderPickerOption = (key: TKey) => {
75
		const { theme } = this.props;
76
		const text = this.findDefaultOption(key);
77
		return <Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>{I18n.t(text?.label)}</Text>;
78
	};
79

80
	pickerSelection = (title: string, key: TKey) => {
81
82
83
		const { preferences } = this.state;
		const { navigation } = this.props;
		let values = OPTIONS[key];
84
85

		const defaultOption = this.findDefaultOption(key);
86
		if (OPTIONS[key][0]?.value !== 'default') {
87
88
89
90
91
			const defaultValue = { label: `${I18n.t('Default')} (${I18n.t(defaultOption?.label)})` } as {
				label: string;
				value: string;
			};
			values = [defaultValue, ...OPTIONS[key]];
92
		}
93

94
95
96
97
		navigation.navigate('PickerView', {
			title,
			data: values,
			value: preferences[key],
98
			onChangeValue: (value: string) => this.onValueChangePicker(key, value ?? defaultOption?.value)
99
		});
100
	};
101

102
	onValueChangePicker = (key: TKey, value: string) => this.saveNotificationPreferences({ [key]: value.toString() });
103

104
	saveNotificationPreferences = async (params: { [key: string]: string }) => {
105
106
107
		const { user } = this.props;
		const { id } = user;
		const result = await RocketChat.setUserPreferences(id, params);
108
109
110
		const {
			user: { settings }
		} = result;
111
		this.setState({ preferences: settings.preferences });
112
	};
113
114
115
116
117

	render() {
		const { theme } = this.props;
		const { loading } = this.state;
		return (
118
119
120
			<SafeAreaView testID='user-notification-preference-view'>
				<StatusBar />
				<List.Container>
121
122
123
124
125
126
127
					{loading ? (
						<>
							<List.Section title='Desktop_Notifications'>
								<List.Separator />
								<List.Item
									title='Alert'
									testID='user-notification-preference-view-alert'
128
									onPress={(title: string) => this.pickerSelection(title, 'desktopNotifications')}
129
130
131
132
133
134
135
136
137
138
139
									right={() => this.renderPickerOption('desktopNotifications')}
								/>
								<List.Separator />
								<List.Info info='Desktop_Alert_info' />
							</List.Section>

							<List.Section title='Push_Notifications'>
								<List.Separator />
								<List.Item
									title='Alert'
									testID='user-notification-preference-view-push-notification'
140
141
									onPress={(title: string) => this.pickerSelection(title, 'pushNotifications')}
									right={() => this.renderPickerOption('pushNotifications')}
142
143
144
145
146
147
148
149
150
151
								/>
								<List.Separator />
								<List.Info info='Push_Notifications_Alert_Info' />
							</List.Section>

							<List.Section title='Email'>
								<List.Separator />
								<List.Item
									title='Alert'
									testID='user-notification-preference-view-email-alert'
152
									onPress={(title: string) => this.pickerSelection(title, 'emailNotificationMode')}
153
154
155
156
157
158
159
160
161
									right={() => this.renderPickerOption('emailNotificationMode')}
								/>
								<List.Separator />
								<List.Info info='You_need_to_verifiy_your_email_address_to_get_notications' />
							</List.Section>
						</>
					) : (
						<ActivityIndicator theme={theme} />
					)}
162
				</List.Container>
163
164
165
166
167
			</SafeAreaView>
		);
	}
}

168
const mapStateToProps = (state: any) => ({
169
170
171
172
	user: getUserSelector(state)
});

export default connect(mapStateToProps)(withTheme(UserNotificationPreferencesView));