Skip to content

Commit

Permalink
HACK: Fix bug when cancelling confirm which disallows navigation. Force
Browse files Browse the repository at this point in the history
window to reload
  • Loading branch information
nealfennimore committed Dec 7, 2024
1 parent 43bb721 commit a3b6deb
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 53 deletions.
25 changes: 14 additions & 11 deletions client/src/app/components/clear_db.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ import { useActionState } from "react";

export const ClearDB = () => {
const action = async (prevState, formData: FormData) => {
return new Promise(async (resolve) => {
const confirm = window.confirm(
"Clear the current database. Continue?"
);
if (!confirm) {
return;
}
return await new Promise(async (resolve) => {
try {
const confirm = window.confirm(
"Clear the current database. Continue?"
);
if (!confirm) {
return;
}

await IDB.securityRequirements.clear();
await IDB.requirements.clear();
await IDB.securityRequirements.clear();
await IDB.requirements.clear();

resolve(null);
window.location.reload();
resolve(null);
} finally {
window.location.reload();
}
});
};

Expand Down
88 changes: 46 additions & 42 deletions client/src/app/components/export_import.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,57 +60,61 @@ export const Export = () => {
export const Import = () => {
const inputRef = useRef<HTMLInputElement>(null);
const action = async (prevState, formData: FormData) => {
return new Promise(async (resolve, reject) => {
const file = formData.get("file") as File;
try {
return await new Promise(async (resolve, reject) => {
const file = formData.get("file") as File;

if (file) {
const reader = new FileReader();
if (file) {
const reader = new FileReader();

reader.onload = async (event) => {
const payload = JSON.parse(
event?.target?.result as string
) as ImportExportPayload;
reader.onload = async (event) => {
const payload = JSON.parse(
event?.target?.result as string
) as ImportExportPayload;

if (payload.version !== IDB.version) {
throw new Error("Database version mismatch");
}

const confirm = window.confirm(
"Importing will overwrite the current database. Continue?"
);
if (!confirm) {
return;
}

await IDB.securityRequirements.clear();
await IDB.requirements.clear();
if (payload.version !== IDB.version) {
throw new Error("Database version mismatch");
}

const requirements: Record<string, IDBRequirement> = {};
const confirm = window.confirm(
"Importing will overwrite the current database. Continue?"
);
if (!confirm) {
return;
}

for (const secReq of payload.securityRequirements) {
const reqId = secReq.id.slice(0, 8);
await IDB.securityRequirements.put(secReq);
if (!requirements[reqId]) {
requirements[reqId] = {
id: reqId,
bySecurityRequirementId: {},
};
await IDB.securityRequirements.clear();
await IDB.requirements.clear();

const requirements: Record<string, IDBRequirement> = {};

for (const secReq of payload.securityRequirements) {
const reqId = secReq.id.slice(0, 8);
await IDB.securityRequirements.put(secReq);
if (!requirements[reqId]) {
requirements[reqId] = {
id: reqId,
bySecurityRequirementId: {},
};
}
requirements[reqId].bySecurityRequirementId[
secReq.id
] = secReq.status as Status;
}
requirements[reqId].bySecurityRequirementId[secReq.id] =
secReq.status as Status;
}

for (const req of Object.values(requirements)) {
await IDB.requirements.put(req);
}
for (const req of Object.values(requirements)) {
await IDB.requirements.put(req);
}

resolve(payload);
window.location.reload();
};
resolve(payload);
};

reader.readAsText(file);
}
});
reader.readAsText(file);
}
});
} finally {
window.location.reload();
}
};

const onClick = () => {
Expand Down

0 comments on commit a3b6deb

Please sign in to comment.