Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Remove jQuery AJAX from the project page #29814

Merged
merged 3 commits into from
Mar 15, 2024
Merged
Changes from 2 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
128 changes: 60 additions & 68 deletions web_src/js/features/repo-projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,31 @@ import $ from 'jquery';
import {useLightTextOnBackground} from '../utils/color.js';
import tinycolor from 'tinycolor2';
import {createSortable} from '../modules/sortable.js';

const {csrfToken} = window.config;
import {POST, DELETE, PUT} from '../modules/fetch.js';

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

function createNewColumn(url, columnTitle, projectColorInput) {
$.ajax({
url,
data: JSON.stringify({title: columnTitle.val(), color: projectColorInput.val()}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'POST',
}).done(() => {
async function createNewColumn(url, columnTitle, projectColorInput) {
try {
await POST(url, {
data: {
title: columnTitle.val(),
color: projectColorInput.val(),
},
});
} catch (error) {
console.error(error);
} finally {
columnTitle.closest('form').removeClass('dirty');
window.location.reload();
silverwind marked this conversation as resolved.
Show resolved Hide resolved
});
}
}

function moveIssue({item, from, to, oldIndex}) {
async function moveIssue({item, from, to, oldIndex}) {
const columnCards = to.getElementsByClassName('issue-card');
updateIssueCount(from);
updateIssueCount(to);
Expand All @@ -38,18 +38,14 @@ function moveIssue({item, from, to, oldIndex}) {
})),
};

$.ajax({
url: `${to.getAttribute('data-url')}/move`,
data: JSON.stringify(columnSorting),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
type: 'POST',
error: () => {
from.insertBefore(item, from.children[oldIndex]);
},
});
try {
await POST(`${to.getAttribute('data-url')}/move`, {
data: columnSorting,
});
} catch (error) {
console.error(error);
from.insertBefore(item, from.children[oldIndex]);
}
}

async function initRepoProjectSortable() {
Expand All @@ -67,20 +63,21 @@ async function initRepoProjectSortable() {
ghostClass: 'card-ghost',
delayOnTouchOnly: true,
delay: 500,
onSort: () => {
onSort: async () => {
boardColumns = mainBoard.getElementsByClassName('project-column');
for (let i = 0; i < boardColumns.length; i++) {
const column = boardColumns[i];
if (parseInt($(column).data('sorting')) !== i) {
$.ajax({
url: $(column).data('url'),
data: JSON.stringify({sorting: i, color: rgbToHex($(column).css('backgroundColor'))}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'PUT',
});
try {
await PUT($(column).data('url'), {
data: {
sorting: i,
color: rgbToHex($(column).css('backgroundColor')),
},
});
} catch (error) {
console.error(error);
}
}
}
},
Expand Down Expand Up @@ -118,26 +115,27 @@ export function initRepoProject() {
setLabelColor(projectHeader, rgbToHex(boardColumn.css('backgroundColor')));
}

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

$.ajax({
url: $(this).data('url'),
data: JSON.stringify({title: projectTitleInput.val(), color: projectColorInput.val()}),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'PUT',
}).done(() => {
try {
await PUT($(this).data('url'), {
data: {
title: projectTitleInput.val(),
color: projectColorInput.val(),
},
});
} catch (error) {
console.error(error);
} finally {
projectTitleLabel.text(projectTitleInput.val());
projectTitleInput.closest('form').removeClass('dirty');
if (projectColorInput.val()) {
setLabelColor(projectHeader, projectColorInput.val());
}
boardColumn.attr('style', `background: ${projectColorInput.val()}!important`);
$('.ui.modal').modal('hide');
});
}
});
});

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

$(commitButton).on('click', (e) => {
$(commitButton).on('click', async (e) => {
e.preventDefault();

$.ajax({
method: 'POST',
url: $(showButton).data('url'),
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
}).done(() => {
try {
await POST($(showButton).data('url'));
} catch (error) {
console.error(error);
} finally {
window.location.reload();
});
}
});
});

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

deleteColumnButton.on('click', (e) => {
deleteColumnButton.on('click', async (e) => {
e.preventDefault();

$.ajax({
url: deleteUrl,
headers: {
'X-Csrf-Token': csrfToken,
},
contentType: 'application/json',
method: 'DELETE',
}).done(() => {
try {
await DELETE(deleteUrl);
} catch (error) {
console.error(error);
} finally {
window.location.reload();
});
}
});
});

Expand All @@ -190,7 +182,7 @@ export function initRepoProject() {
if (!columnTitle.val()) {
return;
}
const url = $(this).data('url');
const url = e.target.getAttribute('data-url');
silverwind marked this conversation as resolved.
Show resolved Hide resolved
createNewColumn(url, columnTitle, projectColorInput);
});
}
Expand Down