mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2026-03-01 12:59:36 +00:00
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
import { Controller } from '@hotwired/stimulus';
|
|
|
|
export default class extends Controller {
|
|
static values = {
|
|
url: String,
|
|
confirmMsg: String,
|
|
successMsg: String,
|
|
errorMsg: String
|
|
}
|
|
|
|
static targets = ["count"]
|
|
|
|
async cleanup(event) {
|
|
event.preventDefault();
|
|
|
|
if (!confirm(this.confirmMsgValue)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(this.urlValue, {
|
|
method: 'POST',
|
|
headers: {
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
}
|
|
});
|
|
|
|
if (response.ok) {
|
|
const data = await response.json();
|
|
alert(this.successMsgValue.replace('%count%', data.count));
|
|
// Update the count displayed in the UI
|
|
if (this.hasCountTarget) {
|
|
this.countTarget.innerText = '0';
|
|
}
|
|
// Reload page to reflect changes if needed, or just let the user see 0
|
|
window.location.reload();
|
|
} else {
|
|
alert(this.errorMsgValue);
|
|
}
|
|
} catch (error) {
|
|
console.error('Cleanup failed:', error);
|
|
alert(this.errorMsgValue);
|
|
}
|
|
}
|
|
}
|