2019-06-11 14:01:40 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { FlatList } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
2020-11-30 21:47:05 +00:00
|
|
|
import RNRestart from 'react-native-restart';
|
2021-11-10 14:35:01 +00:00
|
|
|
import { Dispatch } from 'redux';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2020-11-30 21:47:05 +00:00
|
|
|
import I18n, { LANGUAGES, isRTL } from '../../i18n';
|
2019-06-11 14:01:40 +00:00
|
|
|
import { showErrorAlert } from '../../utils/info';
|
2021-09-13 20:41:05 +00:00
|
|
|
import log, { events, logEvent } from '../../utils/log';
|
2019-06-11 14:01:40 +00:00
|
|
|
import { setUser as setUserAction } from '../../actions/login';
|
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../../containers/List';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../../constants/colors';
|
|
|
|
import { withTheme } from '../../theme';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ROOT_INSIDE, ROOT_LOADING, appStart as appStartAction } from '../../actions/app';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2020-02-11 20:28:40 +00:00
|
|
|
import database from '../../lib/database';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
interface ILanguageViewProps {
|
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
language: string;
|
|
|
|
};
|
|
|
|
setUser(user: object): void;
|
|
|
|
appStart(params: any): void;
|
|
|
|
theme: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ILanguageViewState {
|
|
|
|
language: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class LanguageView extends React.Component<ILanguageViewProps, ILanguageViewState> {
|
2020-07-31 18:30:36 +00:00
|
|
|
static navigationOptions = () => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
title: I18n.t('Change_Language')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
constructor(props: ILanguageViewProps) {
|
2019-06-11 14:01:40 +00:00
|
|
|
super(props);
|
|
|
|
this.state = {
|
2020-02-28 20:11:08 +00:00
|
|
|
language: props.user ? props.user.language : 'en'
|
2019-06-11 14:01:40 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
shouldComponentUpdate(nextProps: ILanguageViewProps, nextState: ILanguageViewState) {
|
2020-02-28 20:11:08 +00:00
|
|
|
const { language } = this.state;
|
2020-02-11 20:28:40 +00:00
|
|
|
const { user, theme } = this.props;
|
2019-12-04 16:39:53 +00:00
|
|
|
if (nextProps.theme !== theme) {
|
|
|
|
return true;
|
|
|
|
}
|
2019-06-11 14:01:40 +00:00
|
|
|
if (nextState.language !== language) {
|
|
|
|
return true;
|
|
|
|
}
|
2020-02-11 20:28:40 +00:00
|
|
|
if (nextProps.user.language !== user.language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
formIsChanged = (language: string) => {
|
2020-02-11 20:28:40 +00:00
|
|
|
const { user } = this.props;
|
2021-09-13 20:41:05 +00:00
|
|
|
return user.language !== language;
|
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
submit = async (language: string) => {
|
2019-06-11 14:01:40 +00:00
|
|
|
if (!this.formIsChanged(language)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-30 21:47:05 +00:00
|
|
|
const { appStart, user } = this.props;
|
|
|
|
|
|
|
|
const shouldRestart = isRTL(language) || isRTL(user.language);
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
await appStart({ root: ROOT_LOADING, text: I18n.t('Change_language_loading') });
|
2020-02-28 20:11:08 +00:00
|
|
|
|
|
|
|
// shows loading for at least 300ms
|
|
|
|
await Promise.all([this.changeLanguage(language), new Promise(resolve => setTimeout(resolve, 300))]);
|
|
|
|
|
2020-11-30 21:47:05 +00:00
|
|
|
if (shouldRestart) {
|
|
|
|
await RNRestart.Restart();
|
|
|
|
} else {
|
|
|
|
await appStart({ root: ROOT_INSIDE });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-02-28 20:11:08 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
changeLanguage = async (language: string) => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE);
|
2020-02-28 20:11:08 +00:00
|
|
|
const { user, setUser } = this.props;
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
const params: { language?: string } = {};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
// language
|
2020-02-11 20:28:40 +00:00
|
|
|
if (user.language !== language) {
|
2019-06-11 14:01:40 +00:00
|
|
|
params.language = language;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
await RocketChat.saveUserPreferences(params);
|
|
|
|
setUser({ language: params.language });
|
|
|
|
|
2020-02-11 20:28:40 +00:00
|
|
|
const serversDB = database.servers;
|
2021-02-26 16:25:51 +00:00
|
|
|
const usersCollection = serversDB.get('users');
|
2021-11-10 14:35:01 +00:00
|
|
|
await serversDB.write(async () => {
|
2020-02-11 20:28:40 +00:00
|
|
|
try {
|
|
|
|
const userRecord = await usersCollection.find(user.id);
|
2021-11-10 14:35:01 +00:00
|
|
|
await userRecord.update((record: any) => {
|
2020-02-11 20:28:40 +00:00
|
|
|
record.language = params.language;
|
|
|
|
});
|
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
}
|
|
|
|
});
|
2019-06-11 14:01:40 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.LANG_SET_LANGUAGE_F);
|
2020-02-11 20:28:40 +00:00
|
|
|
showErrorAlert(I18n.t('There_was_an_error_while_action', { action: I18n.t('saving_preferences') }));
|
|
|
|
log(e);
|
2019-06-11 14:01:40 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderIcon = () => {
|
|
|
|
const { theme } = this.props;
|
2020-10-30 13:59:44 +00:00
|
|
|
return <List.Icon name='check' color={themes[theme].tintColor} />;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
renderItem = ({ item }: { item: { value: string; label: string } }) => {
|
2019-06-11 14:01:40 +00:00
|
|
|
const { value, label } = item;
|
|
|
|
const { language } = this.state;
|
|
|
|
const isSelected = language === value;
|
|
|
|
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
2019-06-11 14:01:40 +00:00
|
|
|
title={label}
|
|
|
|
onPress={() => this.submit(value)}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`language-view-${value}`}
|
2022-01-17 16:10:39 +00:00
|
|
|
right={() => (isSelected ? this.renderIcon() : null)}
|
2020-10-30 13:59:44 +00:00
|
|
|
translateTitle={false}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2019-06-11 14:01:40 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='language-view'>
|
|
|
|
<StatusBar />
|
2019-06-11 14:01:40 +00:00
|
|
|
<FlatList
|
|
|
|
data={LANGUAGES}
|
|
|
|
keyExtractor={item => item.value}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListHeaderComponent={List.Separator}
|
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
contentContainerStyle={List.styles.contentContainerStyleFlatList}
|
2019-06-11 14:01:40 +00:00
|
|
|
renderItem={this.renderItem}
|
2020-10-30 13:59:44 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2019-06-11 14:01:40 +00:00
|
|
|
/>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-02-11 20:28:40 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2021-11-10 14:35:01 +00:00
|
|
|
const mapDispatchToProps = (dispatch: Dispatch) => ({
|
|
|
|
setUser: (params: any) => dispatch(setUserAction(params)),
|
|
|
|
appStart: (params: any) => dispatch(appStartAction(params))
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(LanguageView));
|