diff --git a/app/assets/javascripts/directives/views/accountMenu.ts b/app/assets/javascripts/directives/views/accountMenu.ts index 1654f7027..27c3f6026 100644 --- a/app/assets/javascripts/directives/views/accountMenu.ts +++ b/app/assets/javascripts/directives/views/accountMenu.ts @@ -1,5 +1,5 @@ import { WebDirective } from './../../types'; -import { isDesktopApplication, isNullOrUndefined } from '@/utils'; +import { isDesktopApplication, isNullOrUndefined, preventRefreshing } from '@/utils'; import template from '%/directives/account-menu.pug'; import { ProtectedAction, ContentType } from 'snjs'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; @@ -18,7 +18,8 @@ import { STRING_GENERATING_LOGIN_KEYS, STRING_GENERATING_REGISTER_KEYS, StringImportError, - STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE + STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE, + STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_REMOVAL } from '@/strings'; import { SyncOpStatus } from 'snjs/dist/@types/services/sync/sync_op_status'; import { PasswordWizardType } from '@/types'; @@ -514,9 +515,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> { return; } - const onBeforeUnload = window.onbeforeunload; - try { - window.onbeforeunload = () => STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE; + preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE, async () => { if (this.application!.hasPasscode()) { await this.application!.changePasscode(passcode); } else { @@ -527,9 +526,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> { confirmPasscode: undefined, showPasscodeForm: false }); - } finally { - window.onbeforeunload = onBeforeUnload; - } + }); } async changePasscodePressed() { @@ -552,7 +549,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> { async removePasscodePressed() { const run = async () => { - const signedIn = !isNullOrUndefined(await this.application!.getUser()); + const signedIn = this.application!.hasAccount(); let message = STRING_REMOVE_PASSCODE_CONFIRMATION; if (!signedIn) { message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM; @@ -561,7 +558,9 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> { text: message, confirmButtonStyle: 'danger' })) { - this.application!.removePasscode(); + preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_REMOVAL, async () => { + await this.application!.removePasscode(); + }); } }; const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege( diff --git a/app/assets/javascripts/strings.ts b/app/assets/javascripts/strings.ts index bbcdf82e3..c05e011da 100644 --- a/app/assets/javascripts/strings.ts +++ b/app/assets/javascripts/strings.ts @@ -62,3 +62,7 @@ export const STRING_CONFIRM_APP_QUIT_DURING_UPGRADE = export const STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE = "A passcode change is in progress. You may lose data if you quit the app. " + "Are you sure you want to quit?" + +export const STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_REMOVAL = + "A passcode removal is in progress. You may lose data if you quit the app. " + + "Are you sure you want to quit?" diff --git a/app/assets/javascripts/utils.ts b/app/assets/javascripts/utils.ts index e95cd83cb..dcac3d380 100644 --- a/app/assets/javascripts/utils.ts +++ b/app/assets/javascripts/utils.ts @@ -135,3 +135,16 @@ if (!Array.prototype.includes) { } }); } + +export async function preventRefreshing( + message: string, + operation: () => Promise | void +) { + const onBeforeUnload = window.onbeforeunload; + try { + window.onbeforeunload = () => message; + await operation(); + } finally { + window.onbeforeunload = onBeforeUnload; + } +} diff --git a/app/assets/javascripts/views/footer/footer_view.ts b/app/assets/javascripts/views/footer/footer_view.ts index 461a20fea..7185e2707 100644 --- a/app/assets/javascripts/views/footer/footer_view.ts +++ b/app/assets/javascripts/views/footer/footer_view.ts @@ -1,5 +1,5 @@ import { FooterStatus, WebDirective } from '@/types'; -import { dateToLocalizedString } from '@/utils'; +import { dateToLocalizedString, preventRefreshing } from '@/utils'; import { ApplicationEvent, SyncQueueStrategy, @@ -309,13 +309,9 @@ class FooterViewCtrl extends PureViewCtrl<{}, { } async openSecurityUpdate() { - const onBeforeUnload = window.onbeforeunload; - try { - window.onbeforeunload = () => STRING_CONFIRM_APP_QUIT_DURING_UPGRADE; + preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_UPGRADE, async () => { await this.application!.performProtocolUpgrade(); - } finally { - window.onbeforeunload = onBeforeUnload; - } + }); } findErrors() {