2020-08-21 13:30:11 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { StyleSheet, Text } from 'react-native';
|
2021-11-16 16:16:29 +00:00
|
|
|
import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
|
2020-08-21 13:30:11 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../../lib/constants';
|
2020-08-21 13:30:11 +00:00
|
|
|
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';
|
2022-04-12 16:27:05 +00:00
|
|
|
import { TSupportedThemes, withTheme } from '../../theme';
|
2020-08-21 13:30:11 +00:00
|
|
|
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-09-13 20:41:05 +00:00
|
|
|
import { OPTIONS } from './options';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { ProfileStackParamList } from '../../stacks/types';
|
2022-04-07 16:27:45 +00:00
|
|
|
import { INotificationPreferences, IUser } from '../../definitions';
|
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
|
|
|
|
2021-11-18 17:46:11 +00:00
|
|
|
type TKey = 'desktopNotifications' | 'pushNotifications' | 'emailNotificationMode';
|
2021-11-16 16:16:29 +00:00
|
|
|
|
|
|
|
interface IUserNotificationPreferencesViewState {
|
2022-03-10 00:16:20 +00:00
|
|
|
preferences: INotificationPreferences;
|
2021-11-16 16:16:29 +00:00
|
|
|
loading: boolean;
|
|
|
|
}
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
interface IUserNotificationPreferencesViewProps {
|
2021-12-03 19:27:57 +00:00
|
|
|
navigation: StackNavigationProp<ProfileStackParamList, 'UserNotificationPrefView'>;
|
2022-04-12 16:27:05 +00:00
|
|
|
theme: TSupportedThemes;
|
2022-04-07 16:27:45 +00:00
|
|
|
user: IUser;
|
2021-11-16 16:16:29 +00:00
|
|
|
}
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
class UserNotificationPreferencesView extends React.Component<
|
|
|
|
IUserNotificationPreferencesViewProps,
|
|
|
|
IUserNotificationPreferencesViewState
|
|
|
|
> {
|
|
|
|
static navigationOptions = (): StackNavigationOptions => ({
|
|
|
|
title: I18n.t('Notification_Preferences')
|
|
|
|
});
|
|
|
|
|
|
|
|
constructor(props: IUserNotificationPreferencesViewProps) {
|
2020-08-21 13:30:11 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2022-03-10 00:16:20 +00:00
|
|
|
preferences: {} as INotificationPreferences,
|
2020-08-21 13:30:11 +00:00
|
|
|
loading: false
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
const { user } = this.props;
|
|
|
|
const { id } = user;
|
|
|
|
const result = await RocketChat.getUserPreferences(id);
|
2022-03-10 00:16:20 +00:00
|
|
|
if (result.success) {
|
|
|
|
const { preferences } = result;
|
|
|
|
this.setState({ preferences, loading: true });
|
|
|
|
}
|
2020-08-21 13:30:11 +00:00
|
|
|
}
|
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
findDefaultOption = (key: TKey) => {
|
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-09-13 20:41:05 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
renderPickerOption = (key: TKey) => {
|
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);
|
2022-04-14 20:30:41 +00:00
|
|
|
return (
|
|
|
|
<Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>
|
|
|
|
{text?.label ? I18n.t(text?.label) : text?.label}
|
|
|
|
</Text>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
pickerSelection = (title: string, key: TKey) => {
|
2020-08-21 13:30:11 +00:00
|
|
|
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') {
|
2022-04-14 20:30:41 +00:00
|
|
|
const defaultValue = {
|
|
|
|
label: `${I18n.t('Default')} (${defaultOption?.label ? I18n.t(defaultOption?.label) : defaultOption?.label})`
|
|
|
|
} as {
|
2021-11-16 16:16:29 +00:00
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
};
|
|
|
|
values = [defaultValue, ...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],
|
2021-11-16 16:16:29 +00:00
|
|
|
onChangeValue: (value: string) => this.onValueChangePicker(key, value ?? defaultOption?.value)
|
2020-08-21 13:30:11 +00:00
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
onValueChangePicker = (key: TKey, value: string) => this.saveNotificationPreferences({ [key]: value.toString() });
|
2020-08-21 13:30:11 +00:00
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
saveNotificationPreferences = async (params: { [key: string]: string }) => {
|
2020-08-21 13:30:11 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
const { id } = user;
|
|
|
|
const result = await RocketChat.setUserPreferences(id, params);
|
2022-03-03 01:53:44 +00:00
|
|
|
if (result.success) {
|
|
|
|
const {
|
|
|
|
user: { settings }
|
|
|
|
} = result;
|
|
|
|
this.setState({ preferences: settings.preferences });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-08-21 13:30:11 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { loading } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='user-notification-preference-view'>
|
|
|
|
<StatusBar />
|
|
|
|
<List.Container>
|
2021-09-13 20:41:05 +00:00
|
|
|
{loading ? (
|
|
|
|
<>
|
|
|
|
<List.Section title='Desktop_Notifications'>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Item
|
|
|
|
title='Alert'
|
|
|
|
testID='user-notification-preference-view-alert'
|
2021-11-16 16:16:29 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'desktopNotifications')}
|
2021-09-13 20:41:05 +00:00
|
|
|
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'
|
2021-11-18 17:46:11 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'pushNotifications')}
|
|
|
|
right={() => this.renderPickerOption('pushNotifications')}
|
2021-09-13 20:41:05 +00:00
|
|
|
/>
|
|
|
|
<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'
|
2021-11-16 16:16:29 +00:00
|
|
|
onPress={(title: string) => this.pickerSelection(title, 'emailNotificationMode')}
|
2021-09-13 20:41:05 +00:00
|
|
|
right={() => this.renderPickerOption('emailNotificationMode')}
|
|
|
|
/>
|
|
|
|
<List.Separator />
|
|
|
|
<List.Info info='You_need_to_verifiy_your_email_address_to_get_notications' />
|
|
|
|
</List.Section>
|
|
|
|
</>
|
|
|
|
) : (
|
2022-03-18 02:37:10 +00:00
|
|
|
<ActivityIndicator />
|
2021-09-13 20:41:05 +00:00
|
|
|
)}
|
2020-10-30 13:59:44 +00:00
|
|
|
</List.Container>
|
2020-08-21 13:30:11 +00:00
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-16 16:16:29 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-08-21 13:30:11 +00:00
|
|
|
user: getUserSelector(state)
|
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(UserNotificationPreferencesView));
|