From b7631aeb21b5af3c15d47c30a176e64b9f4de28e Mon Sep 17 00:00:00 2001
From: Vincent Rubinetti <vince.rubinetti@gmail.com>
Date: Tue, 27 Feb 2024 22:45:14 -0500
Subject: [PATCH] fix bug

---
 app/create-pr.js              |  5 ++---
 app/src/api.ts                | 13 ++-----------
 app/src/index.css             |  3 ++-
 app/src/pages/Edit.tsx        | 20 ++++++++++----------
 app/src/pages/edit/Footer.tsx | 19 ++++++++++---------
 5 files changed, 26 insertions(+), 34 deletions(-)

diff --git a/app/create-pr.js b/app/create-pr.js
index 958c8978d..14f6c5811 100644
--- a/app/create-pr.js
+++ b/app/create-pr.js
@@ -10,7 +10,7 @@ const repo = "captions";
 /** endpoint */
 functions.http("create-pr", async (request, response) => {
   response.set("Access-Control-Allow-Origin", "*");
-  response.set("Access-Control-Allow-Methods", "POST");
+  response.set("Access-Control-Allow-Methods", "OPTIONS, POST");
   response.set("Access-Control-Allow-Headers", "Accept, Content-Type");
 
   if (request.method == "OPTIONS") return response.status(200).send();
@@ -55,9 +55,8 @@ async function createPr(params, debug = false) {
   }
 
   /** create new branch */
-  let newBranch;
   try {
-    newBranch = await octokit.rest.git.createRef({
+    await octokit.rest.git.createRef({
       owner,
       repo,
       ref: `refs/heads/${branch}`,
diff --git a/app/src/api.ts b/app/src/api.ts
index 2548c29f2..8a8340f2c 100644
--- a/app/src/api.ts
+++ b/app/src/api.ts
@@ -73,17 +73,8 @@ export async function createPr(params: CreatePR) {
       body: JSON.stringify(params),
     });
 
-    try {
-      return (await response.json()) as PR;
-    } catch {
-      //
-    }
-    try {
-      return await response.text();
-    } catch {
-      //
-    }
-    throw Error("Couldn't parse");
+    if (!response.ok) return await response.text();
+    else return (await response.json()) as PR;
   } catch (error) {
     console.error(error);
     return "Request or parsing error";
diff --git a/app/src/index.css b/app/src/index.css
index 4a66f9d08..51bcde6c9 100644
--- a/app/src/index.css
+++ b/app/src/index.css
@@ -173,7 +173,8 @@ img {
 
 /** interactive */
 
-[disabled] {
+[disabled],
+[aria-disabled="true"] {
   opacity: 0.5;
   pointer-events: none;
 }
diff --git a/app/src/pages/Edit.tsx b/app/src/pages/Edit.tsx
index 0bb925d26..ac8c0d4a5 100644
--- a/app/src/pages/Edit.tsx
+++ b/app/src/pages/Edit.tsx
@@ -194,20 +194,20 @@ export async function submitPr(
 
   console.info(pr);
 
-  /** handle errors */
-  if (typeof pr === "string") {
-    window.alert(submitMessage(pr));
-    return;
-  }
-
-  return { link: pr.data.html_url, number: pr.data.number };
+  /** error */
+  if (typeof pr === "string") window.alert(submitMessage(pr));
+  /** success */ else if (window.confirm(successMessage(pr.data.html_url)))
+    window.location.href = pr.data.html_url;
 }
 
-const navMessage =
-  "Are you sure you want to navigate away from this page? Unsaved edits will be lost.";
-
 const lockedMessage =
   "This lesson/language is already being edited by someone, see github.com/3b1b/captions/pulls. Please work on something else for now.";
 
+const navMessage =
+  "Are you sure you want to navigate away from this page? Unsaved edits will be lost.";
+
 const submitMessage = (error: string) =>
   `There was an error submitting: ${error.replace(/\.$/, "")}. Please save your edits as a backup, then try again later or report this issue.`;
+
+const successMessage = (link: string) =>
+  `Edits submitted successfully at ${link}! Would you like to go there now? This is where you can make further edits, make comments, and watch for reviews.`;
diff --git a/app/src/pages/edit/Footer.tsx b/app/src/pages/edit/Footer.tsx
index 2ed89306d..4aa7e00b9 100644
--- a/app/src/pages/edit/Footer.tsx
+++ b/app/src/pages/edit/Footer.tsx
@@ -56,7 +56,8 @@ function Footer() {
   /** edit filter count */
   const edits = filterOptions.find((filter) => filter.id === "my")?.count || 0;
 
-  /** when page submitted */
+  /** whether to disable submitting */
+  const disabled = edits === 0 || submitting;
 
   return (
     <footer
@@ -107,7 +108,7 @@ function Footer() {
         />
 
         <Button
-          disabled={edits === 0 || submitting}
+          aria-disabled={disabled}
           text={
             submitting
               ? "Submitting"
@@ -121,12 +122,15 @@ function Footer() {
 
         <form
           id="submit-edits"
-          onSubmit={async () => {
+          onSubmit={async (event) => {
+            /** prevent page from nav'ing away before submitting finished */
+            event.preventDefault();
+
+            if (disabled) return;
+
             setSubmitting(true);
-            const pr = await submitPr(lesson, language, author || "");
+            await submitPr(lesson, language, author || "");
             setSubmitting(false);
-            if (pr && window.confirm(successMessage(pr.link)))
-              window.location.href = pr.link;
           }}
         />
       </div>
@@ -135,6 +139,3 @@ function Footer() {
 }
 
 export default Footer;
-
-const successMessage = (link: string) =>
-  `Edits submitted successfully at ${link}! Would you like to go there now? This is where you can make further edits, make comments, and watch for reviews.`;