Skip to content

Commit 2089b97

Browse files
authored
Remove jQuery AJAX from the repo tag edit form (#29526)
- Removed all jQuery AJAX calls and replaced with our fetch wrapper - Tested the repo tag edit form functionality and it works as before # Demo using `fetch` instead of jQuery AJAX ![action](https://github.com/go-gitea/gitea/assets/20454870/11126bc4-1666-44ae-8644-a6351da43514) --------- Signed-off-by: Yarden Shoham <[email protected]>
1 parent a53d268 commit 2089b97

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

web_src/js/features/repo-home.js

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import $ from 'jquery';
22
import {stripTags} from '../utils.js';
33
import {hideElem, showElem} from '../utils/dom.js';
4+
import {POST} from '../modules/fetch.js';
45

5-
const {appSubUrl, csrfToken} = window.config;
6+
const {appSubUrl} = window.config;
67

78
export function initRepoTopicBar() {
89
const mgrBtn = $('#manage_topic');
@@ -30,50 +31,50 @@ export function initRepoTopicBar() {
3031
mgrBtn.focus();
3132
});
3233

33-
saveBtn.on('click', () => {
34+
saveBtn.on('click', async () => {
3435
const topics = $('input[name=topics]').val();
3536

36-
$.post(saveBtn.attr('data-link'), {
37-
_csrf: csrfToken,
38-
topics
39-
}, (_data, _textStatus, xhr) => {
40-
if (xhr.responseJSON.status === 'ok') {
37+
const data = new FormData();
38+
data.append('topics', topics);
39+
40+
const response = await POST(saveBtn.attr('data-link'), {data});
41+
42+
if (response.ok) {
43+
const responseData = await response.json();
44+
if (responseData.status === 'ok') {
4145
viewDiv.children('.topic').remove();
4246
if (topics.length) {
4347
const topicArray = topics.split(',');
4448
topicArray.sort();
45-
for (let i = 0; i < topicArray.length; i++) {
49+
for (const topic of topicArray) {
4650
const link = $('<a class="ui repo-topic large label topic gt-m-0"></a>');
47-
link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topicArray[i])}&topic=1`);
48-
link.text(topicArray[i]);
51+
link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`);
52+
link.text(topic);
4953
link.insertBefore(mgrBtn); // insert all new topics before manage button
5054
}
5155
}
5256
hideElem(editDiv);
5357
showElem(viewDiv);
5458
}
55-
}).fail((xhr) => {
56-
if (xhr.status === 422) {
57-
if (xhr.responseJSON.invalidTopics.length > 0) {
58-
topicPrompts.formatPrompt = xhr.responseJSON.message;
59-
60-
const {invalidTopics} = xhr.responseJSON;
61-
const topicLabels = topicDropdown.children('a.ui.label');
62-
63-
for (const [index, value] of topics.split(',').entries()) {
64-
for (let i = 0; i < invalidTopics.length; i++) {
65-
if (invalidTopics[i] === value) {
66-
topicLabels.eq(index).removeClass('green').addClass('red');
67-
}
68-
}
59+
} else if (response.status === 422) {
60+
const responseData = await response.json();
61+
if (responseData.invalidTopics.length > 0) {
62+
topicPrompts.formatPrompt = responseData.message;
63+
64+
const {invalidTopics} = responseData;
65+
const topicLabels = topicDropdown.children('a.ui.label');
66+
for (const [index, value] of topics.split(',').entries()) {
67+
if (invalidTopics.includes(value)) {
68+
topicLabels.eq(index).removeClass('green').addClass('red');
6969
}
70-
} else {
71-
topicPrompts.countPrompt = xhr.responseJSON.message;
7270
}
71+
} else {
72+
topicPrompts.countPrompt = responseData.message;
7373
}
74-
}).always(() => {
75-
topicForm.form('validate form');
76-
});
74+
}
75+
76+
// Always validate the form
77+
topicForm.form('validate form');
7778
});
7879

7980
topicDropdown.dropdown({

0 commit comments

Comments
 (0)