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 { 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(

View File

@@ -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?"

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 { 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() {