2020-09-11 14:31:38 +00:00
|
|
|
import React from 'react';
|
2022-03-29 16:09:44 +00:00
|
|
|
import { ScrollView, StyleSheet, Text, View } from 'react-native';
|
|
|
|
import Clipboard from '@react-native-clipboard/clipboard';
|
2022-01-13 18:37:14 +00:00
|
|
|
import { connect } from 'react-redux';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-01-13 18:37:14 +00:00
|
|
|
import { encryptionSetBanner } from '../actions/encryption';
|
|
|
|
import Button from '../containers/Button';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2020-09-11 14:31:38 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
|
|
|
import StatusBar from '../containers/StatusBar';
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
2022-01-13 18:37:14 +00:00
|
|
|
import { IApplicationState, IBaseScreen } from '../definitions';
|
2020-09-11 14:31:38 +00:00
|
|
|
import I18n from '../i18n';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { E2E_RANDOM_PASSWORD_KEY, themes } from '../lib/constants';
|
2022-04-07 13:13:19 +00:00
|
|
|
import UserPreferences from '../lib/methods/userPreferences';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { E2ESaveYourPasswordStackParamList } from '../stacks/types';
|
2022-01-13 18:37:14 +00:00
|
|
|
import { withTheme } from '../theme';
|
2022-06-06 14:17:51 +00:00
|
|
|
import EventEmitter from '../lib/methods/helpers/events';
|
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
|
|
|
import scrollPersistTaps from '../lib/methods/helpers/scrollPersistTaps';
|
2022-01-13 18:37:14 +00:00
|
|
|
import sharedStyles from './Styles';
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
container: {
|
|
|
|
flex: 1,
|
|
|
|
padding: 44,
|
|
|
|
paddingTop: 32
|
|
|
|
},
|
|
|
|
content: {
|
|
|
|
marginVertical: 68,
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
warning: {
|
|
|
|
fontSize: 14,
|
|
|
|
...sharedStyles.textMedium
|
|
|
|
},
|
|
|
|
passwordText: {
|
|
|
|
marginBottom: 8,
|
|
|
|
...sharedStyles.textAlignCenter
|
|
|
|
},
|
|
|
|
password: {
|
|
|
|
fontSize: 24,
|
|
|
|
marginBottom: 24,
|
|
|
|
...sharedStyles.textBold
|
|
|
|
},
|
|
|
|
copyButton: {
|
|
|
|
width: 72,
|
|
|
|
height: 32
|
|
|
|
},
|
|
|
|
info: {
|
|
|
|
fontSize: 14,
|
|
|
|
marginBottom: 64,
|
|
|
|
...sharedStyles.textRegular
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-11-17 20:05:35 +00:00
|
|
|
interface IE2ESaveYourPasswordViewState {
|
2022-05-20 02:42:55 +00:00
|
|
|
password: string | null;
|
2021-11-17 20:05:35 +00:00
|
|
|
}
|
|
|
|
|
2022-01-13 18:37:14 +00:00
|
|
|
interface IE2ESaveYourPasswordViewProps extends IBaseScreen<E2ESaveYourPasswordStackParamList, 'E2ESaveYourPasswordView'> {
|
2021-11-17 20:05:35 +00:00
|
|
|
server: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
class E2ESaveYourPasswordView extends React.Component<IE2ESaveYourPasswordViewProps, IE2ESaveYourPasswordViewState> {
|
|
|
|
private mounted: boolean;
|
|
|
|
|
|
|
|
static navigationOptions = ({ navigation }: Pick<IE2ESaveYourPasswordViewProps, 'navigation'>) => ({
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: () => <HeaderButton.CloseModal navigation={navigation} testID='e2e-save-your-password-view-close' />,
|
2020-09-11 14:31:38 +00:00
|
|
|
title: I18n.t('Save_Your_E2E_Password')
|
2021-09-13 20:41:05 +00:00
|
|
|
});
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2021-11-17 20:05:35 +00:00
|
|
|
constructor(props: IE2ESaveYourPasswordViewProps) {
|
2020-09-11 14:31:38 +00:00
|
|
|
super(props);
|
|
|
|
this.mounted = false;
|
|
|
|
this.state = { password: '' };
|
|
|
|
this.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.mounted = true;
|
|
|
|
}
|
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
init = () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
const { server } = this.props;
|
|
|
|
try {
|
|
|
|
// Set stored password on local state
|
2022-03-09 19:41:26 +00:00
|
|
|
const password = UserPreferences.getString(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
|
2020-09-11 14:31:38 +00:00
|
|
|
if (this.mounted) {
|
2022-05-20 02:42:55 +00:00
|
|
|
this.setState({ password });
|
2020-09-11 14:31:38 +00:00
|
|
|
} else {
|
2021-11-17 20:05:35 +00:00
|
|
|
// @ts-ignore
|
2020-09-11 14:31:38 +00:00
|
|
|
this.state.password = password;
|
|
|
|
}
|
|
|
|
} catch {
|
|
|
|
// Do nothing
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
2022-03-09 19:41:26 +00:00
|
|
|
onSaved = () => {
|
2020-09-11 14:31:38 +00:00
|
|
|
logEvent(events.E2E_SAVE_PW_SAVED);
|
2022-01-13 18:37:14 +00:00
|
|
|
const { navigation, server, dispatch } = this.props;
|
2020-09-11 14:31:38 +00:00
|
|
|
// Remove stored password
|
2022-03-09 19:41:26 +00:00
|
|
|
UserPreferences.removeItem(`${server}-${E2E_RANDOM_PASSWORD_KEY}`);
|
2020-09-11 14:31:38 +00:00
|
|
|
// Hide encryption banner
|
2022-01-13 18:37:14 +00:00
|
|
|
dispatch(encryptionSetBanner());
|
2020-09-11 14:31:38 +00:00
|
|
|
navigation.pop();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
onCopy = () => {
|
|
|
|
logEvent(events.E2E_SAVE_PW_COPY);
|
|
|
|
const { password } = this.state;
|
2022-05-20 02:42:55 +00:00
|
|
|
if (password) {
|
|
|
|
Clipboard.setString(password);
|
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Copied_to_clipboard') });
|
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
onHowItWorks = () => {
|
|
|
|
logEvent(events.E2E_SAVE_PW_HOW_IT_WORKS);
|
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.navigate('E2EHowItWorksView');
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-09-11 14:31:38 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { password } = this.state;
|
|
|
|
const { theme } = this.props;
|
|
|
|
|
|
|
|
return (
|
2020-10-30 18:31:04 +00:00
|
|
|
<SafeAreaView style={{ backgroundColor: themes[theme].backgroundColor }} testID='e2e-save-password-view'>
|
2020-10-30 13:59:44 +00:00
|
|
|
<StatusBar />
|
2021-09-13 20:41:05 +00:00
|
|
|
<ScrollView
|
|
|
|
{...scrollPersistTaps}
|
|
|
|
style={sharedStyles.container}
|
2022-08-08 21:02:08 +00:00
|
|
|
contentContainerStyle={sharedStyles.containerScrollView}
|
|
|
|
>
|
2020-09-11 14:31:38 +00:00
|
|
|
<View style={[styles.container, { backgroundColor: themes[theme].backgroundColor }]}>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Text style={[styles.warning, { color: themes[theme].dangerColor }]}>
|
|
|
|
{I18n.t('Save_Your_Encryption_Password_warning')}
|
|
|
|
</Text>
|
2020-09-11 14:31:38 +00:00
|
|
|
<View style={styles.content}>
|
|
|
|
<Text style={[styles.passwordText, { color: themes[theme].bodyText }]}>{I18n.t('Your_password_is')}</Text>
|
|
|
|
<Text style={[styles.password, { color: themes[theme].bodyText }]}>{password}</Text>
|
|
|
|
<Button
|
|
|
|
onPress={this.onCopy}
|
|
|
|
style={[styles.copyButton, { backgroundColor: themes[theme].auxiliaryBackground }]}
|
|
|
|
title={I18n.t('Copy')}
|
|
|
|
type='secondary'
|
|
|
|
fontSize={12}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
<Text style={[styles.info, { color: themes[theme].bodyText }]}>{I18n.t('Save_Your_Encryption_Password_info')}</Text>
|
|
|
|
<Button
|
|
|
|
onPress={this.onHowItWorks}
|
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
|
|
|
title={I18n.t('How_It_Works')}
|
|
|
|
type='secondary'
|
2020-10-30 18:31:04 +00:00
|
|
|
testID='e2e-save-password-view-how-it-works'
|
2020-09-11 14:31:38 +00:00
|
|
|
/>
|
|
|
|
<Button
|
|
|
|
onPress={this.onSaved}
|
|
|
|
title={I18n.t('I_Saved_My_E2E_Password')}
|
2020-10-30 18:31:04 +00:00
|
|
|
testID='e2e-save-password-view-saved-password'
|
2020-09-11 14:31:38 +00:00
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-13 18:37:14 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-09-11 14:31:38 +00:00
|
|
|
server: state.server.server
|
|
|
|
});
|
2022-01-13 18:37:14 +00:00
|
|
|
|
|
|
|
export default connect(mapStateToProps)(withTheme(E2ESaveYourPasswordView));
|