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 <git@yardenshoham.com>
(cherry picked from commit 2089b974c8bf670de5c6801fa48c229fd9291a7b)
This commit is contained in:
Yarden Shoham 2024-03-02 11:29:04 +02:00 committed by Earl Warren
parent 66632c4958
commit d1dbb1f780
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00

View file

@ -1,8 +1,9 @@
import $ from 'jquery'; import $ from 'jquery';
import {stripTags} from '../utils.js'; import {stripTags} from '../utils.js';
import {hideElem, showElem} from '../utils/dom.js'; import {hideElem, showElem} from '../utils/dom.js';
import {POST} from '../modules/fetch.js';
const {appSubUrl, csrfToken} = window.config; const {appSubUrl} = window.config;
export function initRepoTopicBar() { export function initRepoTopicBar() {
const mgrBtn = $('#manage_topic'); const mgrBtn = $('#manage_topic');
@ -30,51 +31,51 @@ export function initRepoTopicBar() {
mgrBtn.focus(); mgrBtn.focus();
}); });
saveBtn.on('click', () => { saveBtn.on('click', async () => {
const topics = $('input[name=topics]').val(); const topics = $('input[name=topics]').val();
$.post(saveBtn.attr('data-link'), { const data = new FormData();
_csrf: csrfToken, data.append('topics', topics);
topics
}, (_data, _textStatus, xhr) => { const response = await POST(saveBtn.attr('data-link'), {data});
if (xhr.responseJSON.status === 'ok') {
if (response.ok) {
const responseData = await response.json();
if (responseData.status === 'ok') {
viewDiv.children('.topic').remove(); viewDiv.children('.topic').remove();
if (topics.length) { if (topics.length) {
const topicArray = topics.split(','); const topicArray = topics.split(',');
topicArray.sort(); topicArray.sort();
for (let i = 0; i < topicArray.length; i++) { for (const topic of topicArray) {
const link = $('<a class="ui repo-topic large label topic gt-m-0"></a>'); const link = $('<a class="ui repo-topic large label topic gt-m-0"></a>');
link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topicArray[i])}&topic=1`); link.attr('href', `${appSubUrl}/explore/repos?q=${encodeURIComponent(topic)}&topic=1`);
link.text(topicArray[i]); link.text(topic);
link.insertBefore(mgrBtn); // insert all new topics before manage button link.insertBefore(mgrBtn); // insert all new topics before manage button
} }
} }
hideElem(editDiv); hideElem(editDiv);
showElem(viewDiv); showElem(viewDiv);
} }
}).fail((xhr) => { } else if (response.status === 422) {
if (xhr.status === 422) { const responseData = await response.json();
if (xhr.responseJSON.invalidTopics.length > 0) { if (responseData.invalidTopics.length > 0) {
topicPrompts.formatPrompt = xhr.responseJSON.message; topicPrompts.formatPrompt = responseData.message;
const {invalidTopics} = xhr.responseJSON; const {invalidTopics} = responseData;
const topicLabels = topicDropdown.children('a.ui.label'); const topicLabels = topicDropdown.children('a.ui.label');
for (const [index, value] of topics.split(',').entries()) { for (const [index, value] of topics.split(',').entries()) {
for (let i = 0; i < invalidTopics.length; i++) { if (invalidTopics.includes(value)) {
if (invalidTopics[i] === value) {
topicLabels.eq(index).removeClass('green').addClass('red'); topicLabels.eq(index).removeClass('green').addClass('red');
} }
} }
}
} else { } else {
topicPrompts.countPrompt = xhr.responseJSON.message; topicPrompts.countPrompt = responseData.message;
} }
} }
}).always(() => {
// Always validate the form
topicForm.form('validate form'); topicForm.form('validate form');
}); });
});
topicDropdown.dropdown({ topicDropdown.dropdown({
allowAdditions: true, allowAdditions: true,