Introduces PasswordPrompt component redesign

This commit is contained in:
mashazyu
2020-04-24 14:28:27 +02:00
committed by Sofiya Tepikin
parent cbe9f3947d
commit 3d2d659b54
+75 -64
View File
@@ -1,13 +1,19 @@
import React, { Component } from 'react' import React, { Component } from 'react'
import PropTypes from 'prop-types' import PropTypes from 'prop-types'
import { View, TextInput, TouchableOpacity, Alert } from 'react-native' import { Alert, StyleSheet, View } from 'react-native'
import nodejs from 'nodejs-mobile-react-native' import nodejs from 'nodejs-mobile-react-native'
import { saveEncryptionFlag } from '../local-storage'
import AppText from './common/app-text' import AppPage from './common/app-page'
import AppTextInput from './common/app-text-input'
import Button from './common/button'
import Header from './header' import Header from './header'
import styles from '../styles'
import { passwordPrompt as labels, shared, menuTitles } from '../i18n/en/labels' import { saveEncryptionFlag } from '../local-storage'
import { requestHash, deleteDbAndOpenNew, openDb } from '../db' import { requestHash, deleteDbAndOpenNew, openDb } from '../db'
import { passwordPrompt as labels, shared } from '../i18n/en/labels'
import { Containers, Spacing } from '../styles/redesign'
const cancelButton = { text: shared.cancel, style: 'cancel' }
export default class PasswordPrompt extends Component { export default class PasswordPrompt extends Component {
static propTypes = { static propTypes = {
@@ -16,17 +22,40 @@ export default class PasswordPrompt extends Component {
constructor(props) { constructor(props) {
super(props) super(props)
this.state = { this.state = { password: null }
password: null
nodejs.channel.addListener('check-pw', this.passHashToDb, this)
} }
nodejs.channel.addListener( componentWillUnmount() {
'check-pw', nodejs.channel.removeListener('check-pw', this.passHashToDb)
this.passHashToDb, }
this
onConfirmDeletion = async () => {
Alert.alert(
labels.deleteDatabaseTitle,
labels.deleteDatabaseExplainer,
[cancelButton, { text: labels.deleteData, onPress: this.onDeleteData}]
) )
} }
onDeleteData = () => {
Alert.alert(
labels.areYouSureTitle,
labels.areYouSure,
[cancelButton, {
text: labels.reallyDeleteData,
onPress: this.onDeleteDataConfirmation
}]
)
}
onDeleteDataConfirmation = async () => {
await deleteDbAndOpenNew()
await saveEncryptionFlag(false)
this.props.enableShowApp()
}
passHashToDb = async hash => { passHashToDb = async hash => {
const connected = await openDb(hash) const connected = await openDb(hash)
if (!connected) { if (!connected) {
@@ -35,7 +64,7 @@ export default class PasswordPrompt extends Component {
shared.incorrectPasswordMessage, shared.incorrectPasswordMessage,
[{ [{
text: shared.tryAgain, text: shared.tryAgain,
onPress: () => this.setState({ password: null }) onPress: this.setPassword(null)
}] }]
) )
return return
@@ -43,71 +72,53 @@ export default class PasswordPrompt extends Component {
this.props.enableShowApp() this.props.enableShowApp()
} }
confirmDeletion = async () => { setPassword = (password) => {
Alert.alert( this.setState({ password })
labels.deleteDatabaseTitle,
labels.deleteDatabaseExplainer,
[{
text: shared.cancel,
style: 'cancel'
}, {
text: labels.deleteData,
onPress: () => {
Alert.alert(
labels.areYouSureTitle,
labels.areYouSure,
[{
text: shared.cancel,
style: 'cancel'
}, {
text: labels.reallyDeleteData,
onPress: async () => {
await deleteDbAndOpenNew()
await saveEncryptionFlag(false)
this.props.enableShowApp()
}
}]
)
}
}]
)
} }
componentWillUnmount() { unlockApp = () => {
nodejs.channel.removeListener('check-pw', this.passHashToDb) requestHash('check-pw', this.state.password)
} }
render() { render() {
const { password } = this.state
const isPasswordEntered = password && password.length > 0
return ( return (
<View flex={1}> <React.Fragment>
<Header title={menuTitles.PasswordPrompt.toLowerCase()} /> <Header isSideMenuEnabled={false} />
<View style={styles.passwordPromptPage}> <AppPage contentContainerStyle={styles.contentContainer}>
<TextInput <AppTextInput
onChangeText={val => this.setState({ password: val })} onChangeText={this.setPassword}
style={styles.passwordPromptField}
secureTextEntry={true} secureTextEntry={true}
placeholder={labels.enterPassword} placeholder={labels.enterPassword}
/> />
<TouchableOpacity <View style={styles.containerButtons}>
style={styles.passwordPromptButton} <Button
onPress={() => { disabled={!isPasswordEntered}
requestHash('check-pw', this.state.password) isCTA={isPasswordEntered}
}} onPress={this.unlockApp}
disabled={!this.state.password}
> >
<AppText style={styles.passwordPromptButtonText}>
{labels.title} {labels.title}
</AppText> </Button>
</TouchableOpacity> <Button onPress={this.onConfirmDeletion}>
<TouchableOpacity
onPress={this.confirmDeletion}
>
<AppText style={styles.passwordPromptForgotPasswordText}>
{labels.forgotPassword} {labels.forgotPassword}
</AppText> </Button>
</TouchableOpacity>
</View>
</View> </View>
</AppPage>
</React.Fragment>
) )
} }
} }
const styles = StyleSheet.create({
contentContainer: {
flex: 1,
justifyContent: 'center',
marginHorizontal: Spacing.base
},
containerButtons: {
...Containers.rowContainer,
justifyContent: 'space-around'
}
})