Chore: Migrate UserNotificationPreferencesView to Typescript (#3469)

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-11-16 13:16:29 -03:00 committed by GitHub
parent b223a711a2
commit 8e55032118
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 28 deletions

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
import { StyleSheet, Text } from 'react-native'; import { StyleSheet, Text } from 'react-native';
import PropTypes from 'prop-types'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { themes } from '../../constants/colors'; import { themes } from '../../constants/colors';
@ -22,20 +22,34 @@ const styles = StyleSheet.create({
} }
}); });
class UserNotificationPreferencesView extends React.Component { type TKey = 'desktopNotifications' | 'mobileNotifications' | 'emailNotificationMode';
static navigationOptions = () => ({
interface IUserNotificationPreferencesViewState {
preferences: {
desktopNotifications?: string;
mobileNotifications?: string;
emailNotificationMode?: string;
};
loading: boolean;
}
interface IUserNotificationPreferencesViewProps {
navigation: StackNavigationProp<any, 'UserNotificationPreferencesView'>;
theme: string;
user: {
id: string;
};
}
class UserNotificationPreferencesView extends React.Component<
IUserNotificationPreferencesViewProps,
IUserNotificationPreferencesViewState
> {
static navigationOptions = (): StackNavigationOptions => ({
title: I18n.t('Notification_Preferences') title: I18n.t('Notification_Preferences')
}); });
static propTypes = { constructor(props: IUserNotificationPreferencesViewProps) {
navigation: PropTypes.object,
theme: PropTypes.string,
user: PropTypes.shape({
id: PropTypes.string
})
};
constructor(props) {
super(props); super(props);
this.state = { this.state = {
preferences: {}, preferences: {},
@ -51,43 +65,43 @@ class UserNotificationPreferencesView extends React.Component {
this.setState({ preferences, loading: true }); this.setState({ preferences, loading: true });
} }
findDefaultOption = key => { findDefaultOption = (key: TKey) => {
const { preferences } = this.state; const { preferences } = this.state;
const option = preferences[key] ? OPTIONS[key].find(item => item.value === preferences[key]) : OPTIONS[key][0]; const option = preferences[key] ? OPTIONS[key].find(item => item.value === preferences[key]) : OPTIONS[key][0];
return option; return option;
}; };
renderPickerOption = key => { renderPickerOption = (key: TKey) => {
const { theme } = this.props; const { theme } = this.props;
const text = this.findDefaultOption(key); const text = this.findDefaultOption(key);
return ( return <Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>{I18n.t(text?.label)}</Text>;
<Text style={[styles.pickerText, { color: themes[theme].actionTintColor }]}>
{I18n.t(text?.label, { defaultValue: text?.label, second: text?.second })}
</Text>
);
}; };
pickerSelection = (title, key) => { pickerSelection = (title: string, key: TKey) => {
const { preferences } = this.state; const { preferences } = this.state;
const { navigation } = this.props; const { navigation } = this.props;
let values = OPTIONS[key]; let values = OPTIONS[key];
const defaultOption = this.findDefaultOption(key); const defaultOption = this.findDefaultOption(key);
if (OPTIONS[key][0]?.value !== 'default') { if (OPTIONS[key][0]?.value !== 'default') {
values = [{ label: `${I18n.t('Default')} (${I18n.t(defaultOption.label)})` }, ...OPTIONS[key]]; const defaultValue = { label: `${I18n.t('Default')} (${I18n.t(defaultOption?.label)})` } as {
label: string;
value: string;
};
values = [defaultValue, ...OPTIONS[key]];
} }
navigation.navigate('PickerView', { navigation.navigate('PickerView', {
title, title,
data: values, data: values,
value: preferences[key], value: preferences[key],
onChangeValue: value => this.onValueChangePicker(key, value ?? defaultOption.value) onChangeValue: (value: string) => this.onValueChangePicker(key, value ?? defaultOption?.value)
}); });
}; };
onValueChangePicker = (key, value) => this.saveNotificationPreferences({ [key]: value.toString() }); onValueChangePicker = (key: TKey, value: string) => this.saveNotificationPreferences({ [key]: value.toString() });
saveNotificationPreferences = async params => { saveNotificationPreferences = async (params: { [key: string]: string }) => {
const { user } = this.props; const { user } = this.props;
const { id } = user; const { id } = user;
const result = await RocketChat.setUserPreferences(id, params); const result = await RocketChat.setUserPreferences(id, params);
@ -111,7 +125,7 @@ class UserNotificationPreferencesView extends React.Component {
<List.Item <List.Item
title='Alert' title='Alert'
testID='user-notification-preference-view-alert' testID='user-notification-preference-view-alert'
onPress={title => this.pickerSelection(title, 'desktopNotifications')} onPress={(title: string) => this.pickerSelection(title, 'desktopNotifications')}
right={() => this.renderPickerOption('desktopNotifications')} right={() => this.renderPickerOption('desktopNotifications')}
/> />
<List.Separator /> <List.Separator />
@ -123,7 +137,7 @@ class UserNotificationPreferencesView extends React.Component {
<List.Item <List.Item
title='Alert' title='Alert'
testID='user-notification-preference-view-push-notification' testID='user-notification-preference-view-push-notification'
onPress={title => this.pickerSelection(title, 'mobileNotifications')} onPress={(title: string) => this.pickerSelection(title, 'mobileNotifications')}
right={() => this.renderPickerOption('mobileNotifications')} right={() => this.renderPickerOption('mobileNotifications')}
/> />
<List.Separator /> <List.Separator />
@ -135,7 +149,7 @@ class UserNotificationPreferencesView extends React.Component {
<List.Item <List.Item
title='Alert' title='Alert'
testID='user-notification-preference-view-email-alert' testID='user-notification-preference-view-email-alert'
onPress={title => this.pickerSelection(title, 'emailNotificationMode')} onPress={(title: string) => this.pickerSelection(title, 'emailNotificationMode')}
right={() => this.renderPickerOption('emailNotificationMode')} right={() => this.renderPickerOption('emailNotificationMode')}
/> />
<List.Separator /> <List.Separator />
@ -151,7 +165,7 @@ class UserNotificationPreferencesView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
user: getUserSelector(state) user: getUserSelector(state)
}); });