feat: prevent refreshing when setting passcode

This commit is contained in:
Baptiste Grob
2020-08-26 17:32:00 +02:00
parent c4506a5407
commit 5b72f4e88f
4 changed files with 29 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
import { WebDirective } from './../../types'; import { WebDirective } from './../../types';
import { isDesktopApplication, isNullOrUndefined } from '@/utils'; import { isDesktopApplication, isNullOrUndefined, preventRefreshing } from '@/utils';
import template from '%/directives/account-menu.pug'; import template from '%/directives/account-menu.pug';
import { ProtectedAction, ContentType } from 'snjs'; import { ProtectedAction, ContentType } from 'snjs';
import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl'; import { PureViewCtrl } from '@Views/abstract/pure_view_ctrl';
@@ -18,7 +18,8 @@ import {
STRING_GENERATING_LOGIN_KEYS, STRING_GENERATING_LOGIN_KEYS,
STRING_GENERATING_REGISTER_KEYS, STRING_GENERATING_REGISTER_KEYS,
StringImportError, StringImportError,
STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE,
STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_REMOVAL
} from '@/strings'; } from '@/strings';
import { SyncOpStatus } from 'snjs/dist/@types/services/sync/sync_op_status'; import { SyncOpStatus } from 'snjs/dist/@types/services/sync/sync_op_status';
import { PasswordWizardType } from '@/types'; import { PasswordWizardType } from '@/types';
@@ -514,9 +515,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> {
return; return;
} }
const onBeforeUnload = window.onbeforeunload; preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE, async () => {
try {
window.onbeforeunload = () => STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE;
if (this.application!.hasPasscode()) { if (this.application!.hasPasscode()) {
await this.application!.changePasscode(passcode); await this.application!.changePasscode(passcode);
} else { } else {
@@ -527,9 +526,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> {
confirmPasscode: undefined, confirmPasscode: undefined,
showPasscodeForm: false showPasscodeForm: false
}); });
} finally { });
window.onbeforeunload = onBeforeUnload;
}
} }
async changePasscodePressed() { async changePasscodePressed() {
@@ -552,7 +549,7 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> {
async removePasscodePressed() { async removePasscodePressed() {
const run = async () => { const run = async () => {
const signedIn = !isNullOrUndefined(await this.application!.getUser()); const signedIn = this.application!.hasAccount();
let message = STRING_REMOVE_PASSCODE_CONFIRMATION; let message = STRING_REMOVE_PASSCODE_CONFIRMATION;
if (!signedIn) { if (!signedIn) {
message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM; message += STRING_REMOVE_PASSCODE_OFFLINE_ADDENDUM;
@@ -561,7 +558,9 @@ class AccountMenuCtrl extends PureViewCtrl<{}, AccountMenuState> {
text: message, text: message,
confirmButtonStyle: 'danger' 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( const needsPrivilege = await this.application!.privilegesService!.actionRequiresPrivilege(

View File

@@ -62,3 +62,7 @@ export const STRING_CONFIRM_APP_QUIT_DURING_UPGRADE =
export const STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE = export const STRING_CONFIRM_APP_QUIT_DURING_PASSCODE_CHANGE =
"A passcode change is in progress. You may lose data if you quit the app. " + "A passcode change is in progress. You may lose data if you quit the app. " +
"Are you sure you want to quit?" "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?"

View File

@@ -135,3 +135,16 @@ if (!Array.prototype.includes) {
} }
}); });
} }
export async function preventRefreshing(
message: string,
operation: () => Promise<void> | void
) {
const onBeforeUnload = window.onbeforeunload;
try {
window.onbeforeunload = () => message;
await operation();
} finally {
window.onbeforeunload = onBeforeUnload;
}
}

View File

@@ -1,5 +1,5 @@
import { FooterStatus, WebDirective } from '@/types'; import { FooterStatus, WebDirective } from '@/types';
import { dateToLocalizedString } from '@/utils'; import { dateToLocalizedString, preventRefreshing } from '@/utils';
import { import {
ApplicationEvent, ApplicationEvent,
SyncQueueStrategy, SyncQueueStrategy,
@@ -309,13 +309,9 @@ class FooterViewCtrl extends PureViewCtrl<{}, {
} }
async openSecurityUpdate() { async openSecurityUpdate() {
const onBeforeUnload = window.onbeforeunload; preventRefreshing(STRING_CONFIRM_APP_QUIT_DURING_UPGRADE, async () => {
try {
window.onbeforeunload = () => STRING_CONFIRM_APP_QUIT_DURING_UPGRADE;
await this.application!.performProtocolUpgrade(); await this.application!.performProtocolUpgrade();
} finally { });
window.onbeforeunload = onBeforeUnload;
}
} }
findErrors() { findErrors() {