Skip to content

Commit 3979970

Browse files
authored
Remove jQuery AJAX from the project page (#29814)
Removed all jQuery AJAX calls and replaced with our fetch wrapper. Tested the following functionalities and they work as before: - column creation - column deletion - issue movement between columns - column reordering - column edit - default column changing # Demo using `fetch` instead of jQuery AJAX ![demo](https://github.com/go-gitea/gitea/assets/20454870/99e6898f-baa3-462c-acec-46a910874dbe) --------- Signed-off-by: Yarden Shoham <[email protected]>
1 parent c006339 commit 3979970

File tree

1 file changed

+60
-68
lines changed

1 file changed

+60
-68
lines changed

web_src/js/features/repo-projects.js

Lines changed: 60 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,31 @@ import $ from 'jquery';
22
import {useLightTextOnBackground} from '../utils/color.js';
33
import tinycolor from 'tinycolor2';
44
import {createSortable} from '../modules/sortable.js';
5-
6-
const {csrfToken} = window.config;
5+
import {POST, DELETE, PUT} from '../modules/fetch.js';
76

87
function updateIssueCount(cards) {
98
const parent = cards.parentElement;
109
const cnt = parent.getElementsByClassName('issue-card').length;
1110
parent.getElementsByClassName('project-column-issue-count')[0].textContent = cnt;
1211
}
1312

14-
function createNewColumn(url, columnTitle, projectColorInput) {
15-
$.ajax({
16-
url,
17-
data: JSON.stringify({title: columnTitle.val(), color: projectColorInput.val()}),
18-
headers: {
19-
'X-Csrf-Token': csrfToken,
20-
},
21-
contentType: 'application/json',
22-
method: 'POST',
23-
}).done(() => {
13+
async function createNewColumn(url, columnTitle, projectColorInput) {
14+
try {
15+
await POST(url, {
16+
data: {
17+
title: columnTitle.val(),
18+
color: projectColorInput.val(),
19+
},
20+
});
21+
} catch (error) {
22+
console.error(error);
23+
} finally {
2424
columnTitle.closest('form').removeClass('dirty');
2525
window.location.reload();
26-
});
26+
}
2727
}
2828

29-
function moveIssue({item, from, to, oldIndex}) {
29+
async function moveIssue({item, from, to, oldIndex}) {
3030
const columnCards = to.getElementsByClassName('issue-card');
3131
updateIssueCount(from);
3232
updateIssueCount(to);
@@ -38,18 +38,14 @@ function moveIssue({item, from, to, oldIndex}) {
3838
})),
3939
};
4040

41-
$.ajax({
42-
url: `${to.getAttribute('data-url')}/move`,
43-
data: JSON.stringify(columnSorting),
44-
headers: {
45-
'X-Csrf-Token': csrfToken,
46-
},
47-
contentType: 'application/json',
48-
type: 'POST',
49-
error: () => {
50-
from.insertBefore(item, from.children[oldIndex]);
51-
},
52-
});
41+
try {
42+
await POST(`${to.getAttribute('data-url')}/move`, {
43+
data: columnSorting,
44+
});
45+
} catch (error) {
46+
console.error(error);
47+
from.insertBefore(item, from.children[oldIndex]);
48+
}
5349
}
5450

5551
async function initRepoProjectSortable() {
@@ -67,20 +63,21 @@ async function initRepoProjectSortable() {
6763
ghostClass: 'card-ghost',
6864
delayOnTouchOnly: true,
6965
delay: 500,
70-
onSort: () => {
66+
onSort: async () => {
7167
boardColumns = mainBoard.getElementsByClassName('project-column');
7268
for (let i = 0; i < boardColumns.length; i++) {
7369
const column = boardColumns[i];
7470
if (parseInt($(column).data('sorting')) !== i) {
75-
$.ajax({
76-
url: $(column).data('url'),
77-
data: JSON.stringify({sorting: i, color: rgbToHex($(column).css('backgroundColor'))}),
78-
headers: {
79-
'X-Csrf-Token': csrfToken,
80-
},
81-
contentType: 'application/json',
82-
method: 'PUT',
83-
});
71+
try {
72+
await PUT($(column).data('url'), {
73+
data: {
74+
sorting: i,
75+
color: rgbToHex($(column).css('backgroundColor')),
76+
},
77+
});
78+
} catch (error) {
79+
console.error(error);
80+
}
8481
}
8582
}
8683
},
@@ -118,26 +115,27 @@ export function initRepoProject() {
118115
setLabelColor(projectHeader, rgbToHex(boardColumn.css('backgroundColor')));
119116
}
120117

121-
$(this).find('.edit-project-column-button').on('click', function (e) {
118+
$(this).find('.edit-project-column-button').on('click', async function (e) {
122119
e.preventDefault();
123120

124-
$.ajax({
125-
url: $(this).data('url'),
126-
data: JSON.stringify({title: projectTitleInput.val(), color: projectColorInput.val()}),
127-
headers: {
128-
'X-Csrf-Token': csrfToken,
129-
},
130-
contentType: 'application/json',
131-
method: 'PUT',
132-
}).done(() => {
121+
try {
122+
await PUT($(this).data('url'), {
123+
data: {
124+
title: projectTitleInput.val(),
125+
color: projectColorInput.val(),
126+
},
127+
});
128+
} catch (error) {
129+
console.error(error);
130+
} finally {
133131
projectTitleLabel.text(projectTitleInput.val());
134132
projectTitleInput.closest('form').removeClass('dirty');
135133
if (projectColorInput.val()) {
136134
setLabelColor(projectHeader, projectColorInput.val());
137135
}
138136
boardColumn.attr('style', `background: ${projectColorInput.val()}!important`);
139137
$('.ui.modal').modal('hide');
140-
});
138+
}
141139
});
142140
});
143141

@@ -146,19 +144,16 @@ export function initRepoProject() {
146144
const showButton = $(boardColumn).find('.default-project-column-show');
147145
const commitButton = $(this).find('.actions > .ok.button');
148146

149-
$(commitButton).on('click', (e) => {
147+
$(commitButton).on('click', async (e) => {
150148
e.preventDefault();
151149

152-
$.ajax({
153-
method: 'POST',
154-
url: $(showButton).data('url'),
155-
headers: {
156-
'X-Csrf-Token': csrfToken,
157-
},
158-
contentType: 'application/json',
159-
}).done(() => {
150+
try {
151+
await POST($(showButton).data('url'));
152+
} catch (error) {
153+
console.error(error);
154+
} finally {
160155
window.location.reload();
161-
});
156+
}
162157
});
163158
});
164159

@@ -167,19 +162,16 @@ export function initRepoProject() {
167162
const deleteColumnButton = deleteColumnModal.find('.actions > .ok.button');
168163
const deleteUrl = $(this).attr('data-url');
169164

170-
deleteColumnButton.on('click', (e) => {
165+
deleteColumnButton.on('click', async (e) => {
171166
e.preventDefault();
172167

173-
$.ajax({
174-
url: deleteUrl,
175-
headers: {
176-
'X-Csrf-Token': csrfToken,
177-
},
178-
contentType: 'application/json',
179-
method: 'DELETE',
180-
}).done(() => {
168+
try {
169+
await DELETE(deleteUrl);
170+
} catch (error) {
171+
console.error(error);
172+
} finally {
181173
window.location.reload();
182-
});
174+
}
183175
});
184176
});
185177

@@ -190,7 +182,7 @@ export function initRepoProject() {
190182
if (!columnTitle.val()) {
191183
return;
192184
}
193-
const url = $(this).data('url');
185+
const url = e.target.getAttribute('data-url');
194186
createNewColumn(url, columnTitle, projectColorInput);
195187
});
196188
}

0 commit comments

Comments
 (0)