Skip to content

Commit

Permalink
fix bug
Browse files Browse the repository at this point in the history
  • Loading branch information
vincerubinetti committed Feb 28, 2024
1 parent 9adf3c5 commit b7631ae
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 34 deletions.
5 changes: 2 additions & 3 deletions app/create-pr.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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}`,
Expand Down
13 changes: 2 additions & 11 deletions app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
3 changes: 2 additions & 1 deletion app/src/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,8 @@ img {

/** interactive */

[disabled] {
[disabled],
[aria-disabled="true"] {
opacity: 0.5;
pointer-events: none;
}
Expand Down
20 changes: 10 additions & 10 deletions app/src/pages/Edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.`;
19 changes: 10 additions & 9 deletions app/src/pages/edit/Footer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -107,7 +108,7 @@ function Footer() {
/>

<Button
disabled={edits === 0 || submitting}
aria-disabled={disabled}
text={
submitting
? "Submitting"
Expand All @@ -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>
Expand All @@ -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.`;

0 comments on commit b7631ae

Please sign in to comment.