Skip to content

Commit

Permalink
fix: pending for submit & modify submit is not correctly displayed (#119
Browse files Browse the repository at this point in the history
)

* fix: pending for submit & modify submit is not correctly displayed

* refactor: fix race

---------

Co-authored-by: mgt <[email protected]>
  • Loading branch information
shaokeyibb and Enter-tainer authored Sep 29, 2024
1 parent b86ded8 commit eaaf912
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions frontend/lib/dom/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,11 @@ export const closeCommentsPanel = () => {
export const submitComment = async ({
offsets,
content,
onPendingFulfilled,
}: {
offsets: [number, number];
content: string;
onPendingFulfilled?: () => Promise<void>;
}) => {
const commitHash = sessionStorage.getItem("commitHash");
if (!commitHash) {
Expand Down Expand Up @@ -243,7 +245,7 @@ export const submitComment = async ({
pending: true,
});
}

await onPendingFulfilled?.();
const resp = await res;

if (!resp.ok) {
Expand All @@ -263,12 +265,14 @@ export const submitComment = async ({
updateAvailableComments();
};

export const _modifyComment = async ({
export const modifyComment = async ({
id,
comment,
onPendingFulfilled,
}: {
id: number;
comment: string;
onPendingFulfilled?: () => Promise<void>;
}) => {
const res = fetch(
`${apiEndpoint}comment/${encodeURIComponent(new URL(window.location.href).pathname)}/id/${id}`,
Expand All @@ -290,6 +294,7 @@ export const _modifyComment = async ({
);
}

await onPendingFulfilled?.();
const resp = await res;

if (!resp.ok) {
Expand Down Expand Up @@ -714,7 +719,6 @@ export const renderComments = async (comments: Comment[]) => {
}

notification.textContent = "";
await openCommentsPanel();
const newSubmitButton = commentsPanel.querySelector(
"[data-review-selected] button[data-action='submit']",
) as HTMLButtonElement;
Expand All @@ -730,6 +734,9 @@ export const renderComments = async (comments: Comment[]) => {
parseInt(selectedOffset!.dataset.originalDocumentEnd!),
],
content: textarea.value,
onPendingFulfilled: async () => {
await openCommentsPanel();
},
});

textarea.value = "";
Expand Down Expand Up @@ -805,12 +812,14 @@ export const renderComments = async (comments: Comment[]) => {
case "modify_submit": {
const id = container.dataset.modifingId;
if (id == undefined) return;
await openCommentsPanel();

try {
await _modifyComment({
await modifyComment({
id: parseInt(id),
comment: textarea.value,
onPendingFulfilled: async () => {
await openCommentsPanel();
},
});

textarea.value = "";
Expand Down

0 comments on commit eaaf912

Please sign in to comment.