Skip to content

Open-source document signature platform. Not affiliated with DocuSign.

TypeScript

Ask before a reload throws away a signer's unfinished entries

Claudeec6a43551e85
diff --git a/e2e/signing-reload.spec.ts b/e2e/signing-reload.spec.ts
new file mode 100644
index 0000000..019ed6c
--- /dev/null
+++ b/e2e/signing-reload.spec.ts
@@ -0,0 +1,118 @@
+import { expect, test } from "@playwright/test";
+
+import { signInAsAdministrator } from "./support/admin-session";
+import { extractLink, waitForEmail } from "./support/outbox";
+import { writeSamplePdf } from "./support/sample-pdf";
+
+// Runs after signing-flow.spec.ts, which sets up the install and its
+// administrator.
+
+const SIGNER_EMAIL = "[email protected]";
+const ANSWER = "One answer before refresh";
+
+test("a signer is asked before a reload throws away what they entered", async ({
+ browser,
+ page,
+}, testInfo) => {
+ await test.step("the sender sends a signature and a text field", async () => {
+ await signInAsAdministrator(page);
+
+ const pdfPath = await writeSamplePdf(testInfo.outputPath("reload.pdf"));
+
+ await page.goto("/envelopes/new");
+ await page.getByLabel("Envelope title").fill("Reload before finishing");
+ await page.getByLabel("Upload PDF").setInputFiles(pdfPath);
+ await page.getByRole("button", { name: "Next ->" }).click();
+ await expect(page).toHaveURL(/\/envelopes\/[^/]+\/signers$/);
+
+ await page.getByLabel("Signer 1 name").fill("Reese Reload");
+ await page.getByLabel("Signer 1 email").fill(SIGNER_EMAIL);
+ await page.getByRole("button", { name: "Next →" }).click();
+ await expect(page).toHaveURL(/\/prepare$/);
+
+ const placementTarget = page.getByLabel("Place Signature on page 1");
+
+ await page.getByRole("button", { exact: true, name: "Signature" }).click();
+
+ const box = await placementTarget.boundingBox();
+
+ await placementTarget.click({
+ position: { x: box!.width * 0.3, y: box!.height * 0.6 },
+ });
+ await page.getByRole("button", { exact: true, name: "Text" }).click();
+ await page.getByLabel("Place Text on page 1").click({
+ position: { x: box!.width * 0.3, y: box!.height * 0.3 },
+ });
+ await expect(page.getByRole("status")).toHaveText("Saved");
+ await page.getByRole("button", { name: "Next →" }).click();
+ await expect(page).toHaveURL(/\/review$/);
+ await page.getByRole("button", { name: "Send envelope" }).click();
+
+ const confirmation = page.getByRole("dialog", {
+ name: "Send this envelope?",
+ });
+
+ await confirmation.getByRole("checkbox").check();
+ await confirmation.getByRole("button", { name: "Send now" }).click();
+ await expect(page).toHaveURL(/\/envelopes\/[^/]+$/);
+ });
+
+ await test.step("the signer fills both in, tries to reload, and stays", async () => {
+ const invitation = await waitForEmail({
+ template: "signing_invitation",
+ to: SIGNER_EMAIL,
+ });
+ const signerContext = await browser.newContext({
+ viewport: { height: 900, width: 1440 },
+ });
+ const signerPage = await signerContext.newPage();
+
+ await signerPage.goto(extractLink(invitation.text, "/sign/"));
+
+ const consent = signerPage.getByRole("dialog", { name: "Before you sign" });
+
+ await consent
+ .getByRole("checkbox", {
+ name: "I agree to use electronic records and signatures",
+ })
+ .check();
+ await consent.getByRole("button", { name: "Agree and continue" }).click();
+ await expect(consent).toBeHidden();
+ await signerPage
+ .getByRole("button", { exact: true, name: "Signature" })
+ .click();
+ await signerPage
+ .getByRole("dialog", { name: "Add your signature" })
+ .getByRole("button", { name: "Apply signature" })
+ .click();
+ await signerPage.getByLabel("Enter text").fill(ANSWER);
+
+ const finish = signerPage.getByRole("button", { name: "Finish" });
+
+ await expect(finish).toBeEnabled();
+
+ // The browser asks before leaving; the signer chooses to stay.
+ const warning = signerPage.waitForEvent("dialog", { timeout: 5_000 });
+
+ await signerPage.evaluate(() => {
+ window.location.reload();
+ });
+
+ const dialog = await warning;
+
+ expect(dialog.type()).toBe("beforeunload");
+ await dialog.dismiss();
+
+ await expect(signerPage.getByLabel("Enter text")).toHaveValue(ANSWER);
+ await expect(finish).toBeEnabled();
+
+ // Finishing leaves the page with no question asked.
+ await finish.click();
+ await signerPage
+ .getByRole("dialog", { name: "Finish signing?" })
+ .getByRole("button", { name: "Finish signing" })
+ .click();
+ await expect(signerPage).toHaveURL(/\/sign\/[^/]+\/done/);
+ await signerContext.close();
+ });
+});
diff --git a/e2e/support/admin-session.ts b/e2e/support/admin-session.ts
new file mode 100644
index 0000000..91d743d
--- /dev/null
+++ b/e2e/support/admin-session.ts
@@ -0,0 +1,51 @@
+import { expect, type Page } from "@playwright/test";
+import pg from "pg";
+
+import { e2eDatabaseUrl } from "../../playwright.config";
+
+// The password every account in signing-flow.spec.ts is made with.
+export const E2E_PASSWORD = "correct horse battery staple 42";
+
+/**
+ * Signs in as the administrator that the first test in signing-flow.spec.ts
+ * sets up. The last test there turns on two-step sign-in for them, so it is
+ * turned off again first: these tests are about other things, and a
+ * password is enough for them.
+ */
+export async function signInAsAdministrator(page: Page): Promise<string> {
+ const database = new pg.Client({ connectionString: e2eDatabaseUrl });
+ let adminEmail = "";
+
+ await database.connect();
+
+ try {
+ const admin = await database.query<{ email: string; id: string }>(
+ `select id, email from public."user" where role = 'admin' order by "createdAt" limit 1`,
+ );
+
+ adminEmail = admin.rows[0]?.email ?? "";
+
+ await database.query(
+ `update public."user" set "twoFactorEnabled" = false where id = $1`,
+ [admin.rows[0]?.id],
+ );
+ await database.query(`delete from public."twoFactor" where "userId" = $1`, [
+ admin.rows[0]?.id,
+ ]);
+ await database.query(
+ `update public.app_settings set require_two_factor = false`,
+ );
+ } finally {
+ await database.end();
+ }
+
+ expect(adminEmail).not.toBe("");
+
+ await page.goto("/login");
+ await page.getByLabel("Email").fill(adminEmail);
+ await page.getByLabel("Password").fill(E2E_PASSWORD);
+ await page.getByRole("button", { name: "Log in" }).click();
+ await expect(page).toHaveURL(/\/dashboard$/);
+
+ return adminEmail;
+}
diff --git a/src/components/signing/signing-live-shell.tsx b/src/components/signing/signing-live-shell.tsx
index 6d4c435..a5b03cd 100644
--- a/src/components/signing/signing-live-shell.tsx
+++ b/src/components/signing/signing-live-shell.tsx
@@ -572,6 +572,37 @@ export function SigningLiveShell({
signatureValues,
textValues,
};
+ // Nothing the signer enters is saved until Finish, so leaving or
+ // reloading the page first would lose it. The browser asks before that
+ // happens (issue #99). Finishing or declining leaves on purpose.
+ const hasUnsubmittedEntries =
+ !hasDeclined &&
+ (Object.keys(signatureValues).length > 0 ||
+ Object.values(textValues).some((value) => value.trim().length > 0) ||
+ Object.values(checkboxValues).some(Boolean));
+ const leavingOnPurposeRef = useRef(false);
+
+ useEffect(() => {
+ if (!hasUnsubmittedEntries) {
+ return;
+ }
+
+ function warnBeforeLeaving(event: BeforeUnloadEvent) {
+ if (leavingOnPurposeRef.current) {
+ return;
+ }
+
+ event.preventDefault();
+ // Older browsers ask only when this is set.
+ event.returnValue = "";
+ }
+
+ window.addEventListener("beforeunload", warnBeforeLeaving);
+
+ return () => {
+ window.removeEventListener("beforeunload", warnBeforeLeaving);
+ };
+ }, [hasUnsubmittedEntries]);
// A group of radio buttons is one question: it is listed and counted once.
const seenRadioGroups = new Set<string>();
const actionSummaryFields = record.currentSignerFields.filter((field) => {
@@ -790,6 +821,7 @@ export function SigningLiveShell({
if (!response.ok) {
if (payload?.state) {
+ leavingOnPurposeRef.current = true;
window.location.reload();
return;
}
@@ -807,12 +839,14 @@ export function SigningLiveShell({
return;
}
+ leavingOnPurposeRef.current = true;
window.location.assign(payload.redirectUrl);
return;
}
const fallbackRedirectPath = `${window.location.pathname.replace(/\/$/, "")}/done?title=${encodeURIComponent(record.envelopeTitle)}`;
+ leavingOnPurposeRef.current = true;
window.location.assign(payload?.redirectPath ?? fallbackRedirectPath);
} catch {
setSubmitError(defaultSubmitErrorMessage);
@@ -875,6 +909,7 @@ export function SigningLiveShell({
if (!response.ok) {
if (payload?.state) {
+ leavingOnPurposeRef.current = true;
window.location.reload();
return;
}