Skip to content

Commit 1e75212

Browse files
author
zsbahtiar
committed
feat(websrc): add repo action for handling delete
1 parent c4bdf78 commit 1e75212

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
import {queryElems, toggleElem} from '../utils/dom.ts';
2+
import {confirmModal} from './comp/ConfirmModal.ts';
3+
import {showErrorToast} from '../modules/toast.ts';
4+
import {POST} from '../modules/fetch.ts';
5+
6+
function initRepoActionListCheckboxes() {
7+
const actionListSelectAll = document.querySelector<HTMLInputElement>('.action-checkbox-all');
8+
if (!actionListSelectAll) return; // logged out state
9+
const issueCheckboxes = document.querySelectorAll<HTMLInputElement>('.action-checkbox:not([disabled])');
10+
const actionDelete = document.querySelector('#action-delete');
11+
const syncIssueSelectionState = () => {
12+
const enabledCheckboxes = Array.from(issueCheckboxes).filter((el) => !el.disabled);
13+
const checkedCheckboxes = enabledCheckboxes.filter((el) => el.checked);
14+
const anyChecked = Boolean(checkedCheckboxes.length);
15+
const allChecked = anyChecked && checkedCheckboxes.length === enabledCheckboxes.length;
16+
17+
if (allChecked) {
18+
actionListSelectAll.checked = true;
19+
actionListSelectAll.indeterminate = false;
20+
} else if (anyChecked) {
21+
actionListSelectAll.checked = false;
22+
actionListSelectAll.indeterminate = true;
23+
} else {
24+
actionListSelectAll.checked = false;
25+
actionListSelectAll.indeterminate = false;
26+
}
27+
if (actionDelete) {
28+
toggleElem('#action-delete', anyChecked);
29+
}
30+
};
31+
32+
for (const el of issueCheckboxes) {
33+
el.addEventListener('change', syncIssueSelectionState);
34+
}
35+
36+
actionListSelectAll.addEventListener('change', () => {
37+
for (const el of issueCheckboxes) {
38+
if (!el.disabled) {
39+
el.checked = actionListSelectAll.checked;
40+
}
41+
}
42+
syncIssueSelectionState();
43+
});
44+
45+
queryElems(document, '.action-action', (el) => el.addEventListener('click',
46+
async (e: MouseEvent) => {
47+
e.preventDefault();
48+
49+
const action = el.getAttribute('data-action');
50+
const url = el.getAttribute('data-url');
51+
const actionIDList: number[] = [];
52+
const radix = 10;
53+
for (const el of document.querySelectorAll<HTMLInputElement>('.action-checkbox:checked:not([disabled])')) {
54+
const id = el.getAttribute('data-action-id');
55+
if (id) {
56+
actionIDList.push(parseInt(id, radix));
57+
}
58+
}
59+
if (actionIDList.length < 1) return;
60+
61+
// for delete
62+
if (action === 'delete') {
63+
const confirmText = el.getAttribute('data-action-delete-confirm');
64+
if (!await confirmModal({content: confirmText, confirmButtonColor: 'red'})) {
65+
return;
66+
}
67+
}
68+
69+
try {
70+
await deleteActions(url, actionIDList);
71+
window.location.reload();
72+
} catch (err) {
73+
showErrorToast(err.responseJSON?.error ?? err.message);
74+
}
75+
},
76+
));
77+
}
78+
79+
async function deleteActions(url: string, actionIds: number[]) {
80+
try {
81+
const response = await POST(url, {
82+
data: {
83+
actionIds,
84+
},
85+
});
86+
if (!response.ok) {
87+
throw new Error('failed to delete actions');
88+
}
89+
} catch (error) {
90+
console.error(error);
91+
}
92+
}
93+
export function initRepoActionList() {
94+
if (document.querySelector('.page-content.repository.actions')) {
95+
initRepoActionListCheckboxes();
96+
}
97+
}

web_src/js/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ import {
8686
initGlobalEnterQuickSubmit,
8787
initGlobalFormDirtyLeaveConfirm,
8888
} from './features/common-form.ts';
89+
import {initRepoActionList} from './features/repo-action-list.ts';
8990

9091
initGiteaFomantic();
9192
initDirAuto();
@@ -214,5 +215,6 @@ onDomReady(() => {
214215
initColorPickers,
215216

216217
initOAuth2SettingsDisableCheckbox,
218+
initRepoActionList,
217219
]);
218220
});

0 commit comments

Comments
 (0)