2020-08-21 13:30:11 +00:00
|
|
|
import React from 'react';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { StyleSheet, Text } from 'react-native';
|
2020-08-21 13:30:11 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2020-08-21 13:30:11 +00:00
|
|
|
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';
|
2020-10-30 13:59:44 +00:00
|
|
|
import sharedStyles from '../Styles';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { OPTIONS } from './options';
|
2020-10-30 13:59:44 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
pickerText: {
|
|
|
|
...sharedStyles.textRegular,
|
|
|
|
fontSize: 16
|
|
|
|
}
|
|
|
|
});
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
class UserNotificationPreferencesView extends React.Component {
|
|
|
|
static navigationOptions = () => ({
|
|
|
|
title: I18n.t('Notification_Preferences')
|
2021-10-01 18:12:19 +00:00
|
|
|
});
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
theme: PropTypes.string,
|
|
|
|
user: PropTypes.shape({
|
|
|
|
id: PropTypes.string
|
|
|
|
})
|
|
|
|
};
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
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 });
|
|
|
|
}
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
findDefaultOption = key => {
|
2020-08-21 13:30:11 +00:00
|
|
|
const { preferences } = this.state;
|
|
|
|
const option = preferences[key] ? OPTIONS[key].find(item => item.value === preferences[key]) : OPTIONS[key][0];
|
|
|
|
return option;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
renderPickerOption = key => {
|
2020-08-21 13:30:11 +00:00
|
|
|
const { theme } = this.props;
|
2020-11-06 14:26:43 +00:00
|
|
|
const text = this.findDefaultOption(key);
|
2021-10-01 18:12:19 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>
|
|
|
|
{I18n.t(text?.label, { defaultValue: text?.label, second: text?.second })}
|
|
|
|
</Text>
|
|
|
|
);
|
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
pickerSelection = (title, key) => {
|
|
|
|
const { preferences } = this.state;
|
|
|
|
const { navigation } = this.props;
|
|
|
|
let values = OPTIONS[key];
|
2020-11-06 14:26:43 +00:00
|
|
|
|
|
|
|
const defaultOption = this.findDefaultOption(key);
|
2020-08-21 13:30:11 +00:00
|
|
|
if (OPTIONS[key][0]?.value !== 'default') {
|
2021-10-01 18:12:19 +00:00
|
|
|
values = [{ label: `${I18n.t('Default')} (${I18n.t(defaultOption.label)})` }, ...OPTIONS[key]];
|
2020-08-21 13:30:11 +00:00
|
|
|
}
|
2020-11-06 14:26:43 +00:00
|
|
|
|
2020-08-21 13:30:11 +00:00
|
|
|
navigation.navigate('PickerView', {
|
|
|
|
title,
|
|
|
|
data: values,
|
|
|
|
value: preferences[key],
|
2020-11-06 14:26:43 +00:00
|
|
|
onChangeValue: value => this.onValueChangePicker(key, value ?? defaultOption.value)
|
2020-08-21 13:30:11 +00:00
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
onValueChangePicker = (key, value) => this.saveNotificationPreferences({ [key]: value.toString() });
|
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
saveNotificationPreferences = async params => {
|
2020-08-21 13:30:11 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
const { id } = user;
|
|
|
|
const result = await RocketChat.setUserPreferences(id, params);
|
2021-10-01 18:12:19 +00:00
|
|
|
const {
|
|
|
|
user: { settings }
|
|
|
|
} = result;
|
2020-08-21 13:30:11 +00:00
|
|
|
this.setState({ preferences: settings.preferences });
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { theme } = this.props;
|
|
|
|
const { loading } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='user-notification-preference-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
2021-10-01 18:12:19 +00:00
|
|
|
{loading ? (
|
|
|
|
<>
|
|
|
|
<List.Section title='Desktop_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Alert'
|
|
|
|
testID='user-notification-preference-view-alert'
|
|
|
|
onPress={title => this.pickerSelection(title, 'desktopNotifications')}
|
|
|
|
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'
|
|
|
|
onPress={title => this.pickerSelection(title, 'mobileNotifications')}
|
|
|
|
right={() => this.renderPickerOption('mobileNotifications')}
|
|
|
|
/>
|
|
|
|
<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'
|
|
|
|
onPress={title => this.pickerSelection(title, 'emailNotificationMode')}
|
|
|
|
right={() => this.renderPickerOption('emailNotificationMode')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='You_need_to_verifiy_your_email_address_to_get_notications' />
|
|
|
|
</List.Section>
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<ActivityIndicator theme={theme} />
|
|
|
|
)}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Container>
|
2020-08-21 13:30:11 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
user: getUserSelector(state)
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(UserNotificationPreferencesView));
|