Skip to content

Commit

Permalink
refactor: remove chained then/finally call
Browse files Browse the repository at this point in the history
  • Loading branch information
Enter-tainer committed Sep 28, 2024
1 parent 26549e6 commit fa53d48
Showing 1 changed file with 67 additions and 63 deletions.
130 changes: 67 additions & 63 deletions frontend/lib/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ const _openCommentsPanel = async () => {
});
}

_renderComments(comments);
await _renderComments(comments);
let selectedCommentsGroup = document.querySelector(
`#review-comments-panel .comments_group[data-original-document-start="${selectedOffset?.dataset.originalDocumentStart}"][data-original-document-end="${selectedOffset?.dataset.originalDocumentEnd}"]`,
) as HTMLElement;
Expand Down Expand Up @@ -503,7 +503,7 @@ const _fetchComments = async (force: boolean = false) => {
return comments;
};

const _renderComments = (comments: Comment[]) => {
const _renderComments = async (comments: Comment[]) => {
const commentsEl = commentsPanel.querySelector(
".panel_main",
)! as HTMLDivElement;
Expand Down Expand Up @@ -767,7 +767,7 @@ const _renderComments = (comments: Comment[]) => {
}

for (const actions of container.querySelectorAll(".comment_actions_item")) {
actions.addEventListener("click", () => {
actions.addEventListener("click", async () => {
const target = actions as HTMLButtonElement;

const textarea = container.querySelector(
Expand Down Expand Up @@ -818,9 +818,8 @@ const _renderComments = (comments: Comment[]) => {

const permalink = new URL(window.location.href);
permalink.hash = `#comment-${id}`;
navigator.clipboard.writeText(permalink.toString()).then(() => {
notification.textContent = "已复制评论链接地址";
});
await navigator.clipboard.writeText(permalink.toString());
notification.textContent = "已复制评论链接地址";
break;
}
case "login": {
Expand Down Expand Up @@ -854,43 +853,45 @@ const _renderComments = (comments: Comment[]) => {

notification.textContent = "";

_submitComment({
offsets: [
parseInt(selectedOffset!.dataset.originalDocumentStart!),
parseInt(selectedOffset!.dataset.originalDocumentEnd!),
],
content: textarea.value,
})
.then(() => {
textarea.value = "";
notification.textContent = "";
})
.catch(_handleError)
.finally(() => {
_openCommentsPanel().then(() => {
const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
);
const newTextArea = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_panel textarea",
) as HTMLTextAreaElement;
if (newNotification) {
newNotification.textContent = notification.textContent;
}
if (newTextArea) {
newTextArea.value = textarea.value;
}
});
try {
await _submitComment({
offsets: [
parseInt(selectedOffset!.dataset.originalDocumentStart!),
parseInt(selectedOffset!.dataset.originalDocumentEnd!),
],
content: textarea.value,
});

_openCommentsPanel().then(() => {
const newSubmitButton = commentsPanel.querySelector(
"[data-review-selected] button[data-action='submit']",
) as HTMLButtonElement;
if (newSubmitButton) {
newSubmitButton.disabled = true;
textarea.value = "";
notification.textContent = "";
} catch (error) {
_handleError(error);
} finally {
await _openCommentsPanel();

const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification"
);
const newTextArea = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_panel textarea"
) as HTMLTextAreaElement;

if (newNotification) {
newNotification.textContent = notification.textContent;
}
});
if (newTextArea) {
newTextArea.value = textarea.value;
}
}

await _openCommentsPanel();
const newSubmitButton = commentsPanel.querySelector(
"[data-review-selected] button[data-action='submit']"
) as HTMLButtonElement;
if (newSubmitButton) {
newSubmitButton.disabled = true;
}

break;
}
case "modify": {
Expand Down Expand Up @@ -943,30 +944,33 @@ const _renderComments = (comments: Comment[]) => {
const id = container.dataset.modifingId;
if (id == undefined) return;

_modifyComment({ id: parseInt(id), comment: textarea.value })
.then(() => {
textarea.value = "";
notification.textContent = "";
})
.catch(_handleError)
.finally(() => {
_openCommentsPanel().then(() => {
const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification",
);
const newTextArea = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_panel textarea",
) as HTMLTextAreaElement;
if (newNotification) {
newNotification.textContent = notification.textContent;
}
if (newTextArea) {
newTextArea.value = textarea.value;
}
});
});
try {
await _modifyComment({ id: parseInt(id), comment: textarea.value });

textarea.value = "";
notification.textContent = "";
} catch (error) {
_handleError(error);
} finally {
await _openCommentsPanel();

const newNotification = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_notification"
);
const newTextArea = commentsPanel.querySelector(
"[data-review-selected] .comment_actions_panel textarea"
) as HTMLTextAreaElement;

if (newNotification) {
newNotification.textContent = notification.textContent;
}
if (newTextArea) {
newTextArea.value = textarea.value;
}
}


_openCommentsPanel();
await _openCommentsPanel(); // ??
break;
}
case "delete": {
Expand Down

0 comments on commit fa53d48

Please sign in to comment.