From ae6a7ee8243663d14f7a1f834adb0ba98f0c7ce2 Mon Sep 17 00:00:00 2001 From: Cezar Augusto Date: Wed, 2 Jan 2019 06:36:11 -0200 Subject: [PATCH] add extra dialog to prevent user from aborting a new sync chain fix https://github.com/brave/brave-browser/issues/2564 --- browser/ui/webui/brave_webui_source.cc | 2 + components/brave_sync/ui/BUILD.gn | 1 + .../commonDialogs/cancelDeviceSyncing.tsx | 36 ++++++++++++++++ .../ui/components/modals/deviceType.tsx | 2 +- .../ui/components/modals/scanCode.tsx | 42 ++++++++++++++++--- .../ui/components/modals/viewSyncCode.tsx | 41 +++++++++++++++--- .../resources/brave_components_strings.grd | 2 + 7 files changed, 115 insertions(+), 11 deletions(-) create mode 100644 components/brave_sync/ui/components/commonDialogs/cancelDeviceSyncing.tsx diff --git a/browser/ui/webui/brave_webui_source.cc b/browser/ui/webui/brave_webui_source.cc index de1d8e46f90c..109745969a7a 100644 --- a/browser/ui/webui/brave_webui_source.cc +++ b/browser/ui/webui/brave_webui_source.cc @@ -445,6 +445,8 @@ void CustomizeWebUIHTMLSource(const std::string &name, content::WebUIDataSource* { "wordCount", IDS_BRAVE_SYNC_SHARED_WORD_COUNT_TEXT }, { "ok", IDS_BRAVE_SYNC_SHARED_OK_BUTTON }, { "cancel", IDS_BRAVE_SYNC_SHARED_CANCEL_BUTTON }, + { "cancelDeviceSyncing", IDS_BRAVE_SYNC_SHARED_CANCEL_SYNCING_TITLE }, + { "cancelDeviceSyncingButton", IDS_BRAVE_SYNC_SHARED_CANCEL_SYNCING_BUTTON }, { "thisSyncChain", IDS_BRAVE_SYNC_SHARED_FROM_THIS_CHAIN_PARTIAL }, { "lookingForDevice", IDS_BRAVE_SYNC_SCAN_CODE_LOOKING_FOR_DEVICE_BUTTON }, { "viewSyncCode", IDS_BRAVE_SYNC_ENABLED_VIEW_CODE_BUTTON }, diff --git a/components/brave_sync/ui/BUILD.gn b/components/brave_sync/ui/BUILD.gn index d1efc97c0fad..96df9f047c63 100644 --- a/components/brave_sync/ui/BUILD.gn +++ b/components/brave_sync/ui/BUILD.gn @@ -4,6 +4,7 @@ transpile_web_ui("ui") { inputs = [ "actions/sync_actions.ts", "components/commonDialogs/areYouSure.tsx", + "components/commonDialogs/cancelDeviceSyncing.tsx", "components/modals/deviceType.tsx", "components/modals/enterSyncCode.tsx", "components/modals/removeDevice.tsx", diff --git a/components/brave_sync/ui/components/commonDialogs/cancelDeviceSyncing.tsx b/components/brave_sync/ui/components/commonDialogs/cancelDeviceSyncing.tsx new file mode 100644 index 000000000000..bc8fd9d13f4b --- /dev/null +++ b/components/brave_sync/ui/components/commonDialogs/cancelDeviceSyncing.tsx @@ -0,0 +1,36 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this file, + * You can obtain one at http://mozilla.org/MPL/2.0/. */ + +import * as React from 'react' + +// Components +import { AlertBox } from 'brave-ui' + +// Feature-specific components +import { Title, Paragraph } from 'brave-ui/features/sync' + +// Utils +import { getLocale } from '../../../../common/locale' + +interface Props { + onClickOk: () => void + onClickCancel: () => void +} + +export default class AreYouSure extends React.PureComponent { + render () { + const { onClickOk, onClickCancel } = this.props + return ( + + {getLocale('areYouSure')} + {getLocale('cancelDeviceSyncing')} + + ) + } +} diff --git a/components/brave_sync/ui/components/modals/deviceType.tsx b/components/brave_sync/ui/components/modals/deviceType.tsx index d86dedfcbbb6..75a6c97a4210 100644 --- a/components/brave_sync/ui/components/modals/deviceType.tsx +++ b/components/brave_sync/ui/components/modals/deviceType.tsx @@ -48,7 +48,7 @@ export default class DeviceTypeModal extends React.PureComponent { } } - componentWillMount () { + componentDidMount () { // once the screen is rendered, create a sync chain. // this allow us to request the qr code and sync words immediately const { thisDeviceName } = this.props.syncData diff --git a/components/brave_sync/ui/components/modals/scanCode.tsx b/components/brave_sync/ui/components/modals/scanCode.tsx index cc5fd22460bc..2350fd48da0d 100644 --- a/components/brave_sync/ui/components/modals/scanCode.tsx +++ b/components/brave_sync/ui/components/modals/scanCode.tsx @@ -22,6 +22,9 @@ import { // Modals import ViewSyncCode from './viewSyncCode' +// Dialogs +import CancelDeviceSyncingDialog from '../commonDialogs/cancelDeviceSyncing' + // Utils import { getLocale } from '../../../../common/locale' @@ -33,15 +36,18 @@ interface Props { actions: any onClose: () => void } + interface State { viewSyncCode: boolean + willCancelScanCode: boolean } export default class ScanCodeModal extends React.PureComponent { constructor (props: Props) { super(props) this.state = { - viewSyncCode: false + viewSyncCode: false, + willCancelScanCode: false } } @@ -49,14 +55,35 @@ export default class ScanCodeModal extends React.PureComponent { this.setState({ viewSyncCode: !this.state.viewSyncCode }) } - onCancel = (event: React.MouseEvent) => { - event.preventDefault() + onDismissModal = () => { + const { devices, isSyncConfigured } = this.props.syncData + // if user is still trying to build a sync chain, + // open the confirmation modal. otherwise close it + isSyncConfigured && devices.length < 2 + ? this.setState({ willCancelScanCode: true }) + : this.props.onClose() + } + + onDismissDialog = () => { + this.setState({ willCancelScanCode: false }) + } + + onConfirmDismissModal = () => { + const { devices, isSyncConfigured } = this.props.syncData + // sync is enabled when at least 2 devices are in the chain. + // this modal works both with sync enabled and disabled states. + // in case user opens it in the enabled content screen, + // check there are 2 devices in chain before reset + if (isSyncConfigured && devices.length < 2) { + this.props.actions.onSyncReset() + } + this.setState({ willCancelScanCode: false }) this.props.onClose() } render () { const { onClose, syncData, actions } = this.props - const { viewSyncCode } = this.state + const { viewSyncCode, willCancelScanCode } = this.state return ( @@ -65,6 +92,11 @@ export default class ScanCodeModal extends React.PureComponent { ? : null } + { + willCancelScanCode + ? + : null + }
{getLocale('scanThisCode')} @@ -83,7 +115,7 @@ export default class ScanCodeModal extends React.PureComponent {
- {getLocale('cancel')} + {getLocale('cancel')}