vn-verdnaturachat/app/views/SettingsView/index.js

272 lines
7.4 KiB
JavaScript
Raw Normal View History

2018-06-05 01:17:02 +00:00
import React from 'react';
import {
View, Linking, ScrollView, AsyncStorage, SafeAreaView, Switch, Text, Share
} from 'react-native';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2018-06-05 01:17:02 +00:00
2019-11-25 20:01:17 +00:00
import { logout as logoutAction } from '../../actions/login';
import { toggleMarkdown as toggleMarkdownAction } from '../../actions/markdown';
import { toggleCrashReport as toggleCrashReportAction } from '../../actions/crashReport';
2019-11-25 20:01:17 +00:00
import { SWITCH_TRACK_COLOR, COLOR_DANGER } from '../../constants/colors';
import { DrawerButton, CloseModalButton } from '../../containers/HeaderButton';
2019-03-12 16:23:06 +00:00
import StatusBar from '../../containers/StatusBar';
import ListItem from '../../containers/ListItem';
import { DisclosureImage } from '../../containers/DisclosureIndicator';
import Separator from '../../containers/Separator';
import I18n from '../../i18n';
import { MARKDOWN_KEY, CRASH_REPORT_KEY } from '../../lib/rocketchat';
2019-11-25 20:01:17 +00:00
import {
getReadableVersion, getDeviceModel, isAndroid
} from '../../utils/deviceInfo';
import openLink from '../../utils/openLink';
import scrollPersistTaps from '../../utils/scrollPersistTaps';
import { showErrorAlert } from '../../utils/info';
import styles from './styles';
import sharedStyles from '../Styles';
import { loggerConfig, analytics } from '../../utils/log';
2019-08-08 18:28:51 +00:00
import { PLAY_MARKET_LINK, APP_STORE_LINK, LICENSE_LINK } from '../../constants/links';
2019-11-25 20:01:17 +00:00
import SidebarView from '../SidebarView';
import { withSplit } from '../../split';
import Navigation from '../../lib/Navigation';
const SectionSeparator = React.memo(() => <View style={styles.sectionSeparatorBorder} />);
const ItemInfo = React.memo(({ info }) => (
<View style={styles.infoContainer}>
<Text style={styles.infoText}>{info}</Text>
</View>
));
ItemInfo.propTypes = {
info: PropTypes.string
};
2018-06-05 01:17:02 +00:00
class SettingsView extends React.Component {
2019-11-25 20:01:17 +00:00
static navigationOptions = ({ navigation, screenProps }) => ({
headerLeft: screenProps.split ? (
<CloseModalButton navigation={navigation} testID='settings-view-close' />
) : (
<DrawerButton navigation={navigation} />
),
2019-03-12 16:23:06 +00:00
title: I18n.t('Settings')
});
static propTypes = {
navigation: PropTypes.object,
server: PropTypes.object,
useMarkdown: PropTypes.bool,
allowCrashReport: PropTypes.bool,
toggleMarkdown: PropTypes.func,
2019-11-25 20:01:17 +00:00
toggleCrashReport: PropTypes.func,
split: PropTypes.bool,
logout: PropTypes.func.isRequired
}
logout = () => {
const { logout, split } = this.props;
if (split) {
Navigation.navigate('RoomView');
}
logout();
}
toggleMarkdown = (value) => {
AsyncStorage.setItem(MARKDOWN_KEY, JSON.stringify(value));
const { toggleMarkdown } = this.props;
toggleMarkdown(value);
}
toggleCrashReport = (value) => {
AsyncStorage.setItem(CRASH_REPORT_KEY, JSON.stringify(value));
const { toggleCrashReport } = this.props;
toggleCrashReport(value);
loggerConfig.autoNotify = value;
analytics().setAnalyticsCollectionEnabled(value);
if (value) {
loggerConfig.clearBeforeSendCallbacks();
} else {
loggerConfig.registerBeforeSendCallback(() => false);
}
}
navigateToRoom = (room) => {
const { navigation } = this.props;
navigation.navigate(room);
}
sendEmail = async() => {
const subject = encodeURI('React Native App Support');
const email = encodeURI('support@rocket.chat');
const description = encodeURI(`
version: ${ getReadableVersion }
device: ${ getDeviceModel }
`);
try {
await Linking.openURL(`mailto:${ email }?subject=${ subject }&body=${ description }`);
} catch (e) {
showErrorAlert(I18n.t('error-email-send-failed', { message: 'support@rocket.chat' }));
}
2018-06-05 01:17:02 +00:00
}
2019-08-08 18:28:51 +00:00
shareApp = () => {
Share.share({ message: isAndroid ? PLAY_MARKET_LINK : APP_STORE_LINK });
}
onPressLicense = () => openLink(LICENSE_LINK)
renderDisclosure = () => <DisclosureImage />
2019-11-25 20:01:17 +00:00
renderLogout = () => (
<>
<Separator />
<ListItem
title={I18n.t('Logout')}
testID='settings-logout'
onPress={this.logout}
right={this.renderDisclosure}
color={COLOR_DANGER}
/>
<Separator />
<ItemInfo />
</>
);
renderMarkdownSwitch = () => {
const { useMarkdown } = this.props;
return (
<Switch
value={useMarkdown}
trackColor={SWITCH_TRACK_COLOR}
onValueChange={this.toggleMarkdown}
/>
);
}
renderCrashReportSwitch = () => {
const { allowCrashReport } = this.props;
return (
<Switch
value={allowCrashReport}
trackColor={SWITCH_TRACK_COLOR}
onValueChange={this.toggleCrashReport}
/>
);
}
2018-06-05 01:17:02 +00:00
render() {
2019-11-25 20:01:17 +00:00
const { server, split } = this.props;
2018-06-05 01:17:02 +00:00
return (
<SafeAreaView style={sharedStyles.listSafeArea} testID='settings-view'>
2019-03-12 16:23:06 +00:00
<StatusBar />
<ScrollView
{...scrollPersistTaps}
contentContainerStyle={[sharedStyles.listContentContainer, styles.listWithoutBorderBottom]}
showsVerticalScrollIndicator={false}
testID='settings-view-list'
>
2019-11-25 20:01:17 +00:00
{split ? (
<>
<SidebarView />
<SectionSeparator />
<ListItem
title={I18n.t('Profile')}
onPress={() => this.navigateToRoom('ProfileView')}
showActionIndicator
testID='settings-profile'
right={this.renderDisclosure}
/>
<Separator />
</>
) : null}
<ListItem
title={I18n.t('Contact_us')}
onPress={this.sendEmail}
showActionIndicator
testID='settings-view-contact'
right={this.renderDisclosure}
/>
<Separator />
<ListItem
title={I18n.t('Language')}
onPress={() => this.navigateToRoom('LanguageView')}
showActionIndicator
testID='settings-view-language'
right={this.renderDisclosure}
/>
<Separator />
<ListItem
2019-09-24 20:15:13 +00:00
title={I18n.t('Share_this_app')}
showActionIndicator
2019-09-24 20:15:13 +00:00
onPress={this.shareApp}
testID='settings-view-share-app'
right={this.renderDisclosure}
/>
<Separator />
<ListItem
2019-09-24 20:15:13 +00:00
title={I18n.t('Theme')}
showActionIndicator
disabled
2019-09-24 20:15:13 +00:00
testID='settings-view-theme'
/>
2019-08-08 18:28:51 +00:00
<Separator />
<SectionSeparator />
<ListItem
title={I18n.t('License')}
onPress={this.onPressLicense}
showActionIndicator
testID='settings-view-license'
right={this.renderDisclosure}
/>
<Separator />
<ListItem title={I18n.t('Version_no', { version: getReadableVersion })} testID='settings-view-version' />
<Separator />
<ListItem
title={I18n.t('Server_version', { version: server.version })}
subtitle={`${ server.server.split('//')[1] }`}
testID='settings-view-server-version'
/>
<SectionSeparator />
<ListItem
title={I18n.t('Enable_markdown')}
testID='settings-view-markdown'
right={() => this.renderMarkdownSwitch()}
/>
<SectionSeparator />
<ListItem
title={I18n.t('Send_crash_report')}
testID='settings-view-crash-report'
right={() => this.renderCrashReportSwitch()}
/>
<Separator />
<ItemInfo
info={I18n.t('Crash_report_disclaimer')}
/>
2019-11-25 20:01:17 +00:00
{ split ? this.renderLogout() : null }
</ScrollView>
</SafeAreaView>
2018-06-05 01:17:02 +00:00
);
}
}
const mapStateToProps = state => ({
server: state.server,
useMarkdown: state.markdown.useMarkdown,
allowCrashReport: state.crashReport.allowCrashReport
});
const mapDispatchToProps = dispatch => ({
2019-11-25 20:01:17 +00:00
logout: () => dispatch(logoutAction()),
toggleMarkdown: params => dispatch(toggleMarkdownAction(params)),
toggleCrashReport: params => dispatch(toggleCrashReportAction(params))
});
2019-11-25 20:01:17 +00:00
export default connect(mapStateToProps, mapDispatchToProps)(withSplit(SettingsView));