import React from 'react';
import {
View, ScrollView, Switch, Text
} from 'react-native';
import PropTypes from 'prop-types';
import RNPickerSelect from 'react-native-picker-select';
import { SafeAreaView } from 'react-navigation';
import { SWITCH_TRACK_COLOR } from '../../constants/colors';
import StatusBar from '../../containers/StatusBar';
import ListItem from '../../containers/ListItem';
import Separator from '../../containers/Separator';
import I18n from '../../i18n';
import scrollPersistTaps from '../../utils/scrollPersistTaps';
import styles from './styles';
import sharedStyles from '../Styles';
import RocketChat from '../../lib/rocketchat';
import log from '../../utils/log';
const SectionTitle = React.memo(({ title }) => {title});
const SectionSeparator = React.memo(() => );
const Info = React.memo(({ info }) => {info});
SectionTitle.propTypes = {
title: PropTypes.string
};
Info.propTypes = {
info: PropTypes.string
};
const OPTIONS = {
desktopNotifications: [{
label: I18n.t('Default'), value: 'default'
}, {
label: I18n.t('All_Messages'), value: 'all'
}, {
label: I18n.t('Mentions'), value: 'mentions'
}, {
label: I18n.t('Nothing'), value: 'nothing'
}],
audioNotifications: [{
label: I18n.t('Default'), value: 'default'
}, {
label: I18n.t('All_Messages'), value: 'all'
}, {
label: I18n.t('Mentions'), value: 'mentions'
}, {
label: I18n.t('Nothing'), value: 'nothing'
}],
mobilePushNotifications: [{
label: I18n.t('Default'), value: 'default'
}, {
label: I18n.t('All_Messages'), value: 'all'
}, {
label: I18n.t('Mentions'), value: 'mentions'
}, {
label: I18n.t('Nothing'), value: 'nothing'
}],
emailNotifications: [{
label: I18n.t('Default'), value: 'default'
}, {
label: I18n.t('All_Messages'), value: 'all'
}, {
label: I18n.t('Mentions'), value: 'mentions'
}, {
label: I18n.t('Nothing'), value: 'nothing'
}],
desktopNotificationDuration: [{
label: I18n.t('Default'), value: 0
}, {
label: I18n.t('Seconds', { second: 1 }), value: 1
}, {
label: I18n.t('Seconds', { second: 2 }), value: 2
}, {
label: I18n.t('Seconds', { second: 3 }), value: 3
}, {
label: I18n.t('Seconds', { second: 4 }), value: 4
}, {
label: I18n.t('Seconds', { second: 5 }), value: 5
}],
audioNotificationValue: [{
label: 'None', value: 'none None'
}, {
label: I18n.t('Default'), value: '0 Default'
}, {
label: 'Beep', value: 'beep Beep'
}, {
label: 'Ding', value: 'ding Ding'
}, {
label: 'Chelle', value: 'chelle Chelle'
}, {
label: 'Droplet', value: 'droplet Droplet'
}, {
label: 'Highbell', value: 'highbell Highbell'
}, {
label: 'Seasons', value: 'seasons Seasons'
}]
};
export default class NotificationPreferencesView extends React.Component {
static navigationOptions = () => ({
title: I18n.t('Notification_Preferences')
})
static propTypes = {
navigation: PropTypes.object
}
constructor(props) {
super(props);
this.mounted = false;
this.rid = props.navigation.getParam('rid');
const room = props.navigation.getParam('room');
this.state = {
room: room || {}
};
if (room && room.observe) {
this.roomObservable = room.observe();
this.subscription = this.roomObservable
.subscribe((changes) => {
if (this.mounted) {
this.setState({ room: changes });
} else {
this.state.room = changes;
}
});
}
}
componentDidMount() {
this.mounted = true;
}
componentWillUnmount() {
if (this.subscription && this.subscription.unsubscribe) {
this.subscription.unsubscribe();
}
}
onValueChangeSwitch = async(key, value) => {
const params = {
[key]: value ? '1' : '0'
};
try {
await RocketChat.saveNotificationSettings(this.rid, params);
} catch (e) {
log(e);
}
}
onValueChangePicker = async(key, value) => {
const params = {
[key]: value.toString()
};
try {
await RocketChat.saveNotificationSettings(this.rid, params);
} catch (e) {
log(e);
}
}
renderPicker = (key) => {
const { room } = this.state;
return (
this.onValueChangePicker(key, value)}
items={OPTIONS[key]}
/>
);
}
renderSwitch = (key) => {
const { room } = this.state;
return (
this.onValueChangeSwitch(key, !value)}
/>
);
}
render() {
const { room } = this.state;
return (
this.renderSwitch('disableNotifications')}
/>
this.renderSwitch('muteGroupMentions')}
/>
this.renderSwitch('hideUnreadStatus')}
/>
this.renderPicker('desktopNotifications')}
/>
this.renderPicker('mobilePushNotifications')}
/>
this.renderPicker('audioNotifications')}
/>
this.renderPicker('audioNotificationValue')}
/>
this.renderPicker('desktopNotificationDuration')}
/>
this.renderPicker('emailNotifications')}
/>
);
}
}