Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Remove jQuery AJAX from the comment edit box #29812

Merged
merged 4 commits into from
Mar 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 29 additions & 19 deletions web_src/js/features/repo-legacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {initRepoPullRequestCommitStatus} from './repo-issue-pr-status.js';
import {hideElem, showElem} from '../utils/dom.js';
import {getComboMarkdownEditor, initComboMarkdownEditor} from './comp/ComboMarkdownEditor.js';
import {attachRefIssueContextPopup} from './contextpopup.js';
import {POST} from '../modules/fetch.js';
import {POST, GET} from '../modules/fetch.js';

const {csrfToken} = window.config;

Expand Down Expand Up @@ -83,7 +83,7 @@ export function initRepoCommentForm() {
await POST(form.attr('action'), {data: params});
window.location.reload();
} catch (error) {
console.error('Error:', error);
console.error(error);
}
} else if (editMode === '') {
$selectBranch.find('.ui .branch-name').text(selectedValue);
Expand Down Expand Up @@ -355,23 +355,26 @@ async function onEditContent(event) {
const input = $(`<input id="${data.uuid}" name="files" type="hidden">`).val(data.uuid);
$dropzone.find('.files').append(input);
});
this.on('removedfile', (file) => {
this.on('removedfile', async (file) => {
if (disableRemovedfileEvent) return;
$(`#${file.uuid}`).remove();
if ($dropzone.attr('data-remove-url') && !fileUuidDict[file.uuid].submitted) {
$.post($dropzone.attr('data-remove-url'), {
file: file.uuid,
_csrf: csrfToken,
});
try {
await POST($dropzone.attr('data-remove-url'), {data: new URLSearchParams({file: file.uuid})});
} catch (error) {
console.error(error);
}
}
});
this.on('submit', () => {
$.each(fileUuidDict, (fileUuid) => {
fileUuidDict[fileUuid].submitted = true;
});
});
this.on('reload', () => {
$.getJSON($editContentZone.attr('data-attachment-url'), (data) => {
this.on('reload', async () => {
try {
const response = await GET($editContentZone.attr('data-attachment-url'));
const data = await response.json();
// do not trigger the "removedfile" event, otherwise the attachments would be deleted from server
disableRemovedfileEvent = true;
dz.removeAllFiles(true);
Expand All @@ -390,7 +393,9 @@ async function onEditContent(event) {
const input = $(`<input id="${attachment.uuid}" name="files" type="hidden">`).val(attachment.uuid);
$dropzone.find('.files').append(input);
}
});
} catch (error) {
console.error(error);
}
});
},
});
Expand All @@ -406,22 +411,25 @@ async function onEditContent(event) {
}
};

const saveAndRefresh = (dz) => {
const saveAndRefresh = async (dz) => {
showElem($renderContent);
hideElem($editContentZone);
$.post($editContentZone.attr('data-update-url'), {
_csrf: csrfToken,
content: comboMarkdownEditor.value(),
context: $editContentZone.attr('data-context'),
files: dz.files.map((file) => file.uuid),
}, (data) => {

try {
const params = new URLSearchParams({
content: comboMarkdownEditor.value(),
context: $editContentZone.attr('data-context'),
});
for (const file of dz.files) params.append('files[]', file.uuid);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TIL that URLSearchParams can have duplicate keys 😆


const response = await POST($editContentZone.attr('data-update-url'), {data: params});
const data = await response.json();
if (!data.content) {
$renderContent.html($('#no-content').html());
$rawContent.text('');
} else {
$renderContent.html(data.content);
$rawContent.text(comboMarkdownEditor.value());

const refIssues = $renderContent.find('p .ref-issue');
attachRefIssueContextPopup(refIssues);
}
Expand All @@ -442,7 +450,9 @@ async function onEditContent(event) {
}
initMarkupContent();
initCommentContent();
});
} catch (error) {
console.error(error);
}
};

if (!$editContentZone.html()) {
Expand Down