Skip to content

Commit

Permalink
fix: removed wrong event listener, closes #3135
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Aug 11, 2024
1 parent ed912e5 commit 2879d86
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions phpmyfaq/admin/assets/src/content/attachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,33 @@ import { deleteAttachments } from '../api/attachment';
import { pushErrorNotification, pushNotification } from '../utils';

export const handleDeleteAttachments = () => {
const attachmentTable = document.getElementById('attachment-table');
const deleteButtons = document.querySelectorAll('.btn-delete-attachment');

if (attachmentTable) {
attachmentTable.addEventListener('click', async (event) => {
event.preventDefault();
if (deleteButtons.length > 0) {
deleteButtons.forEach((button) => {
// Clone the button to remove all existing event listeners
const newButton = button.cloneNode(true);
button.replaceWith(newButton);

const isButton = event.target.className.includes('btn-delete-attachment');
if (isButton) {
const attachmentId = event.target.getAttribute('data-attachment-id');
const csrf = event.target.getAttribute('data-csrf');
// Attach the event listener to the new button
newButton.addEventListener('click', async (event) => {
event.preventDefault();

const attachmentId = newButton.getAttribute('data-attachment-id');
const csrf = newButton.getAttribute('data-csrf');

const response = await deleteAttachments(attachmentId, csrf);

if (response.success) {
pushNotification(response.success);
const row = document.getElementById(`attachment_${attachmentId}`);
row.addEventListener('click', () => (row.style.opacity = '0'));
row.style.opacity = '0';
row.addEventListener('transitionend', () => row.remove());
}
if (response.error) {
pushErrorNotification(response.error);
}
}
});
});
}
};

0 comments on commit 2879d86

Please sign in to comment.