diff --git a/components/settings/password/create.js b/components/settings/password/create.js index f024c3f..eee35e6 100644 --- a/components/settings/password/create.js +++ b/components/settings/password/create.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React, { useState } from 'react' import PropTypes from 'prop-types' import Button from '../../common/button' @@ -8,42 +8,35 @@ import showBackUpReminder from './show-backup-reminder' import settings from '../../../i18n/en/settings' -export default class CreatePassword extends Component { - static propTypes = { - changeEncryptionAndRestart: PropTypes.func, +const CreatePassword = ({ changeEncryptionAndRestart }) => { + const [isSettingPassword, setIsSettingPassword] = useState(false) + + const startSettingPassword = () => { + showBackUpReminder( + () => { + setIsSettingPassword(true) + }, + () => {} + ) } - constructor() { - super() + const labels = settings.passwordSettings - this.state = { isSettingPassword: false } + if (!isSettingPassword) { + return ( + + ) } - toggleSettingPassword = () => { - const { isSettingPassword } = this.state - this.setState({ isSettingPassword: !isSettingPassword }) - } - - startSettingPassword = () => { - showBackUpReminder(this.toggleSettingPassword, () => {}) - } - - render() { - const { isSettingPassword } = this.state - const labels = settings.passwordSettings - - if (!isSettingPassword) { - return ( - - ) - } else { - return ( - - ) - } - } + return ( + + ) } + +CreatePassword.propTypes = { + changeEncryptionAndRestart: PropTypes.func, +} + +export default CreatePassword diff --git a/components/settings/password/delete.js b/components/settings/password/delete.js index d195c99..84a3f0b 100644 --- a/components/settings/password/delete.js +++ b/components/settings/password/delete.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React, { useState } from 'react' import PropTypes from 'prop-types' import Button from '../../common/button' @@ -6,45 +6,43 @@ import ConfirmWithPassword from '../common/confirm-with-password' import labels from '../../../i18n/en/settings' -export default class DeletePassword extends Component { - static propTypes = { - onStartDelete: PropTypes.func, - onCancelDelete: PropTypes.func, - changeEncryptionAndRestart: PropTypes.func, +const DeletePassword = ({ + onStartDelete, + onCancelDelete, + changeEncryptionAndRestart, +}) => { + const [enteringCurrentPassword, setEnteringCurrentPassword] = useState(false) + + const startConfirmWithPassword = () => { + setEnteringCurrentPassword(true) + onStartDelete() } - constructor() { - super() - - this.state = { enteringCurrentPassword: false } + const cancelConfirmationWithPassword = () => { + setEnteringCurrentPassword(false) + onCancelDelete() } - startConfirmWithPassword = () => { - this.setState({ enteringCurrentPassword: true }) - this.props.onStartDelete() - } - - cancelConfirmationWithPassword = () => { - this.setState({ enteringCurrentPassword: false }) - this.props.onCancelDelete() - } - - render() { - const { enteringCurrentPassword } = this.state - - if (enteringCurrentPassword) { - return ( - - ) - } - + if (enteringCurrentPassword) { return ( - + ) } + + return ( + + ) } + +DeletePassword.propTypes = { + onStartDelete: PropTypes.func, + onCancelDelete: PropTypes.func, + changeEncryptionAndRestart: PropTypes.func, +} + +export default DeletePassword diff --git a/components/settings/password/enter-new-password.js b/components/settings/password/enter-new-password.js index a593e13..7f42169 100644 --- a/components/settings/password/enter-new-password.js +++ b/components/settings/password/enter-new-password.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React, { useState, useEffect } from 'react' import { StyleSheet } from 'react-native' import nodejs from 'nodejs-mobile-react-native' import PropTypes from 'prop-types' @@ -13,89 +13,67 @@ import settings from '../../../i18n/en/settings' const LISTENER_TYPE = 'create-or-change-pw' -export default class EnterNewPassword extends Component { - static propTypes = { - changeEncryptionAndRestart: PropTypes.func, - } - constructor(props) { - super() - this.state = { - password: '', - passwordConfirmation: '', - shouldShowErrorMessage: false, - } - nodejs.channel.addListener( +const EnterNewPassword = ({ changeEncryptionAndRestart }) => { + const [password, setPassword] = useState('') + const [passwordConfirmation, setPasswordConfirmation] = useState('') + const [shouldShowErrorMessage, setShouldShowErrorMessage] = useState(false) + + useEffect(() => { + const listener = nodejs.channel.addListener( LISTENER_TYPE, - props.changeEncryptionAndRestart, + changeEncryptionAndRestart, this ) - } + return () => listener.remove() + }, []) - componentWillUnmount() { - nodejs.channel.removeListener( - LISTENER_TYPE, - this.props.changeEncryptionAndRestart - ) - } - - savePassword = () => { - if (this.comparePasswords()) { - requestHash(LISTENER_TYPE, this.state.password) + const savePassword = () => { + if (comparePasswords()) { + requestHash(LISTENER_TYPE, password) } else { - this.setState({ shouldShowErrorMessage: true }) + setShouldShowErrorMessage(true) } } - comparePasswords = () => { - return this.state.password === this.state.passwordConfirmation - } + const comparePasswords = () => password === passwordConfirmation - handlePasswordInput = (password) => { - this.setState({ password }) - } + const labels = settings.passwordSettings + const isButtonActive = password.length > 0 && passwordConfirmation.length > 0 - handleConfirmationInput = (passwordConfirmation) => { - this.setState({ passwordConfirmation }) - } + return ( + <> + + + {shouldShowErrorMessage && ( + {labels.passwordsDontMatch} + )} + + + ) +} - render() { - const { password, passwordConfirmation, shouldShowErrorMessage } = - this.state - const labels = settings.passwordSettings - const isButtonActive = - password.length > 0 && passwordConfirmation.length > 0 - - return ( - - - - {shouldShowErrorMessage && ( - {labels.passwordsDontMatch} - )} - - - ) - } +EnterNewPassword.propTypes = { + changeEncryptionAndRestart: PropTypes.func, } const styles = StyleSheet.create({ @@ -104,3 +82,5 @@ const styles = StyleSheet.create({ marginTop: Spacing.base, }, }) + +export default EnterNewPassword diff --git a/components/settings/password/index.js b/components/settings/password/index.js index 62e9b15..98a3ca7 100644 --- a/components/settings/password/index.js +++ b/components/settings/password/index.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React, { useState } from 'react' import PropTypes from 'prop-types' import { changeDbEncryption } from '../../../db' @@ -14,81 +14,55 @@ import DeletePassword from './delete' import { hasEncryptionObservable } from '../../../local-storage' import labels from '../../../i18n/en/settings' -class PasswordSetting extends Component { - static propTypes = { - navigate: PropTypes.func, - restartApp: PropTypes.func, - } - constructor(props) { - super(props) +const PasswordSetting = ({ restartApp, navigate }) => { + const isPasswordSet = hasEncryptionObservable.value + const [isChangingPassword, setIsChangingPassword] = useState(false) + const [isDeletingPassword, setIsDeletingPassword] = useState(false) - this.state = { - isPasswordSet: hasEncryptionObservable.value, - isChangingPassword: false, - isDeletingPassword: false, - } - } - - onChangingPassword = () => { - this.setState({ isChangingPassword: true }) - } - - onCancelChangingPassword = () => { - this.setState({ isChangingPassword: false }) - } - - onDeletingPassword = () => { - this.setState({ isDeletingPassword: true }) - } - - onCancelDeletingPassword = () => { - this.setState({ isDeletingPassword: false }) - } - - changeEncryptionAndRestart = async (hash) => { + const changeEncryptionAndRestart = async (hash) => { await changeDbEncryption(hash) - await this.props.restartApp() - this.props.navigate('Home') + await restartApp() + navigate('Home') } - render() { - const { isPasswordSet, isChangingPassword, isDeletingPassword } = this.state + const { title, explainerEnabled, explainerDisabled } = labels.passwordSettings - const { title, explainerEnabled, explainerDisabled } = - labels.passwordSettings + return ( + + + + {isPasswordSet ? explainerEnabled : explainerDisabled} + - return ( - - - - {isPasswordSet ? explainerEnabled : explainerDisabled} - + {!isPasswordSet && ( + + )} - {!isPasswordSet && ( - - )} + {isPasswordSet && !isDeletingPassword && ( + setIsChangingPassword(true)} + onCancelChange={() => setIsChangingPassword(false)} + changeEncryptionAndRestart={changeEncryptionAndRestart} + /> + )} - {isPasswordSet && !isDeletingPassword && ( - - )} + {isPasswordSet && !isChangingPassword && ( + setIsDeletingPassword(true)} + onCancelDelete={() => setIsDeletingPassword(false)} + changeEncryptionAndRestart={changeEncryptionAndRestart} + /> + )} + + + ) +} - {isPasswordSet && !isChangingPassword && ( - - )} - - - ) - } +PasswordSetting.propTypes = { + navigate: PropTypes.func, + restartApp: PropTypes.func, } export default PasswordSetting diff --git a/components/settings/password/update.js b/components/settings/password/update.js index a83d701..76d06ec 100644 --- a/components/settings/password/update.js +++ b/components/settings/password/update.js @@ -1,4 +1,4 @@ -import React, { Component } from 'react' +import React, { useState } from 'react' import PropTypes from 'prop-types' import Button from '../../common/button' @@ -9,83 +9,71 @@ import ConfirmWithPassword from '../common/confirm-with-password' import settings from '../../../i18n/en/settings' -export default class ChangePassword extends Component { - static propTypes = { - onStartChange: PropTypes.func, - onCancelChange: PropTypes.func, - changeEncryptionAndRestart: PropTypes.func, - } +const ChangePassword = ({ + changeEncryptionAndRestart, + onStartChange, + onCancelChange, +}) => { + const [currentPassword, setCurrentPassword] = useState(null) + const [enteringCurrentPassword, setEnteringCurrentPassword] = useState(false) + const [enteringNewPassword, setEnteringNewPassword] = useState(false) - constructor() { - super() - - this.state = { - currentPassword: null, - enteringCurrentPassword: false, - enteringNewPassword: false, - } - } - - startChangingPassword = () => { + const startChangingPassword = () => { showBackUpReminder( - this.startEnteringCurrentPassword, - this.cancelConfirmationWithPassword + startEnteringCurrentPassword, + cancelConfirmationWithPassword ) } - startEnteringCurrentPassword = () => { - this.setState({ enteringCurrentPassword: true }) - this.props.onStartChange() + const startEnteringCurrentPassword = () => { + setEnteringCurrentPassword(true) + onStartChange() } - startEnteringNewPassword = () => { - this.setState({ - currentPassword: null, - enteringNewPassword: true, - enteringCurrentPassword: false, - }) + const startEnteringNewPassword = () => { + setCurrentPassword(null) + setEnteringNewPassword(true) + setEnteringCurrentPassword(false) } - cancelConfirmationWithPassword = () => { - this.setState({ - currentPassword: null, - enteringNewPassword: false, - enteringCurrentPassword: false, - }) - this.props.onCancelChange() + const cancelConfirmationWithPassword = () => { + setCurrentPassword(null) + setEnteringNewPassword(false) + setEnteringCurrentPassword(false) + onCancelChange() } - render() { - const { enteringCurrentPassword, enteringNewPassword, currentPassword } = - this.state - const labels = settings.passwordSettings - const isPasswordSet = currentPassword !== null - - if (enteringCurrentPassword) { - return ( - - ) - } - - if (enteringNewPassword) { - return ( - - ) - } + const labels = settings.passwordSettings + const isPasswordSet = currentPassword !== null + if (enteringCurrentPassword) { return ( - + ) } + + if (enteringNewPassword) { + return ( + + ) + } + + return ( + + ) +} + +export default ChangePassword + +ChangePassword.propTypes = { + onStartChange: PropTypes.func, + onCancelChange: PropTypes.func, + changeEncryptionAndRestart: PropTypes.func, }