From 558eb7d717cce2a34c66c399bd3b73c27060ae36 Mon Sep 17 00:00:00 2001 From: mashazyu Date: Sat, 5 Jan 2019 12:46:48 +0100 Subject: [PATCH] Adds new reusable component for confirmation with password --- .../data-management/confirm-with-password.js | 88 +++++++++++++ .../data-management/delete-data-dialog.js | 19 --- .../settings/data-management/delete-data.js | 117 ++++++------------ 3 files changed, 128 insertions(+), 96 deletions(-) create mode 100644 components/settings/data-management/confirm-with-password.js delete mode 100644 components/settings/data-management/delete-data-dialog.js diff --git a/components/settings/data-management/confirm-with-password.js b/components/settings/data-management/confirm-with-password.js new file mode 100644 index 0000000..fee296a --- /dev/null +++ b/components/settings/data-management/confirm-with-password.js @@ -0,0 +1,88 @@ +import React, { Component } from 'react' +import { View, Alert } from 'react-native' + +import nodejs from 'nodejs-mobile-react-native' +import { requestHash, openDb } from '../../../db' + +import PasswordField from '../password/password-field' +import SettingsButton from '../settings-button' + +import settings from '../../../i18n/en/settings' +import { shared } from '../../../i18n/en/labels' + +export default class ConfirmWithPassword extends Component { + constructor() { + super() + this.state = { + password: null + } + nodejs.channel.addListener( + 'password-check', + this.checkPassword, + this + ) + } + + componentWillUnmount() { + nodejs.channel.removeListener('password-check', this.checkPassword) + } + + resetPasswordInput = () => { + this.setState({ password: null }) + } + + + onIncorrectPassword = () => { + Alert.alert( + shared.incorrectPassword, + shared.incorrectPasswordMessage, + [{ + text: shared.cancel, + onPress: this.props.onCancel + }, { + text: shared.tryAgain, + onPress: this.resetPasswordInput + }] + ) + } + + checkPassword = async hash => { + try { + await openDb(hash) + this.props.onSuccess() + } catch (err) { + this.onIncorrectPassword() + } + } + + handlePasswordInput = (password) => { + this.setState({ password }) + } + + initPasswordCheck = () => { + requestHash('password-check', this.state.password) + } + + render() { + + const { password } = this.state + const labels = settings.passwordSettings + + return ( + + + + {settings.deleteSegment.title} + + + ) + + } +} \ No newline at end of file diff --git a/components/settings/data-management/delete-data-dialog.js b/components/settings/data-management/delete-data-dialog.js deleted file mode 100644 index 0122576..0000000 --- a/components/settings/data-management/delete-data-dialog.js +++ /dev/null @@ -1,19 +0,0 @@ -import { Alert } from 'react-native' -import { shared as sharedLabels } from '../../../i18n/en/labels' -import labels from '../../../i18n/en/settings' - -export default function showDeleteDialog(okHandler) { - - const { question, message, confirmation } = labels.deleteSegment - - Alert.alert( - question, - message, - [{ - text: confirmation, - onPress: okHandler - }, { - text: sharedLabels.cancel, style: 'cancel', onPress: () => { } - }] - ) -} \ No newline at end of file diff --git a/components/settings/data-management/delete-data.js b/components/settings/data-management/delete-data.js index 3bd154d..d04d291 100644 --- a/components/settings/data-management/delete-data.js +++ b/components/settings/data-management/delete-data.js @@ -1,63 +1,50 @@ import React, { Component } from 'react' -import { View } from 'react-native' -import nodejs from 'nodejs-mobile-react-native' import RNFS from 'react-native-fs' +import { Alert } from 'react-native' -import settings from '../../../i18n/en/settings' -import { requestHash, clearDb } from '../../../db' +import { clearDb } from '../../../db' import { hasEncryptionObservable } from '../../../local-storage' -import PasswordField from '../password/password-field' import SettingsButton from '../settings-button' -import showDeleteDialog from './delete-data-dialog' -import checkCurrentPassword from '../password/check-current-password' +import ConfirmWithPassword from './confirm-with-password' import alertError from '../alert-error' +import settings from '../../../i18n/en/settings' +import { shared as sharedLabels } from '../../../i18n/en/labels' export default class DeleteData extends Component { constructor() { super() this.state = { isPasswordSet: hasEncryptionObservable.value, - currentPassword: null, - enteringCurrentPassword: false + isConfirmingWithPassword: false } + } - nodejs.channel.addListener( - 'pre-change-pw-check', - this.openNewPasswordField, - this + onAlertConfirmation = () => { + if (this.state.isPasswordSet) { + this.setState({ isConfirmingWithPassword: true }) + } else { + this.deleteAppData() + } + } + + alertBeforeDeletion = () => { + const { question, message, confirmation } = settings.deleteSegment + + Alert.alert( + question, + message, + [{ + text: confirmation, + onPress: this.onAlertConfirmation + }, { + text: sharedLabels.cancel, + style: 'cancel', + onPress: this.cancelConfirmationWithPassword + }] ) } - componentWillUnmount() { - nodejs.channel.removeListener('pre-change-pw-check', this.openNewPasswordField) - } - - openNewPasswordField = async hash => { - const passwordCorrect = await checkCurrentPassword({ - hash, - onTryAgain: () => this.setState({ currentPassword: null }), - onCancel: () => this.setState({ - enteringCurrentPassword: false, - currentPassword: null - }) - }) - - if (passwordCorrect) { - await this.deleteAppData() - } - } - - startDataDeletion = () => { - showDeleteDialog(() => { - if (this.state.isPasswordSet) { - this.setState({ enteringCurrentPassword: true }) - } else { - this.deleteAppData() - } - }) - } - deleteExportedFile = async () => { const path = RNFS.DocumentDirectoryPath + '/data.csv' const isFileExist = await RNFS.exists(path) @@ -77,50 +64,26 @@ export default class DeleteData extends Component { } } - handleCurrentPasswordInput = (currentPassword) => { - this.setState({ currentPassword }) - } - - checkCurrentPassword = () => { - requestHash('pre-change-pw-check', this.state.currentPassword) + cancelConfirmationWithPassword = () => { + this.setState({ isConfirmingWithPassword: false }) } render() { + const { isConfirmingWithPassword } = this.state - const { - enteringCurrentPassword, - currentPassword - } = this.state - - const labels = settings.passwordSettings - - if (enteringCurrentPassword) { + if (isConfirmingWithPassword) { return ( - - - - {settings.deleteSegment.title} - - + ) } return ( - - - {settings.deleteSegment.title} - - + + {settings.deleteSegment.title} + ) } } \ No newline at end of file