Have got soft and hard deleting

This commit is contained in:
Leon Mika 2026-02-23 21:18:34 +11:00
parent aef3bb6a1e
commit 3ea5823ca0
27 changed files with 588 additions and 55 deletions

View file

@ -0,0 +1,77 @@
import { Controller } from "@hotwired/stimulus"
import { showToast } from "../services/toast";
export default class PostlistController extends Controller {
static values = {
siteId: Number,
postId: Number,
nanoSummary: String,
};
async deletePost(ev) {
ev.preventDefault();
let isHardDelete = ev.params && ev.params.hardDelete;
if (isHardDelete) {
if (!confirm("Are you sure you want to delete this post?")) {
return;
}
}
try {
let deleteQuery = isHardDelete ? '?hard=true' : '';
this.element.remove();
await fetch(`/sites/${this.siteIdValue}/posts/${this.postIdValue}${deleteQuery}`, {
method: 'DELETE',
headers: { 'Accept': 'application/json' },
});
if (isHardDelete) {
showToast({
title: "🔥 Post Delete",
body: this.nanoSummaryValue,
});
} else {
showToast({
title: "🗑️ Sent To Trash",
body: this.nanoSummaryValue,
});
}
} catch (error) {
showToast({
title: "❌ Error",
body: "Failed to delete post. Please try again later.",
});
}
}
async restorePost(ev) {
ev.preventDefault();
try {
this.element.remove();
await fetch(`/sites/${this.siteIdValue}/posts/${this.postIdValue}`, {
method: 'PATCH',
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
action: 'restore'
})
});
showToast({
title: "🗑️ Restored From Trash",
body: this.nanoSummaryValue,
});
} catch (error) {
showToast({
title: "❌ Error",
body: "Failed to rstore post. Please try again later.",
});
}
}
}

View file

@ -0,0 +1,24 @@
import { Toast } from 'bootstrap/dist/js/bootstrap.js';
import { Controller } from "@hotwired/stimulus"
export default class ToastController extends Controller {
static targets = ['title', 'body'];
initialize() {
this._toast = new Toast(this.element);
}
showToast(ev) {
let toastDetails = ev.detail;
if (!toastDetails) {
return;
}
this.titleTarget.innerText = toastDetails.title || "Title";
this.bodyTarget.innerText = toastDetails.body || "Body";
if (!this._toast.isShown()) {
this._toast.show();
}
}
}

8
assets/js/main.js Normal file
View file

@ -0,0 +1,8 @@
import { Application } from "@hotwired/stimulus";
import ToastController from "./controllers/toast";
import PostlistController from "./controllers/postlist";
window.Stimulus = Application.start()
Stimulus.register("toast", ToastController);
Stimulus.register("postlist", PostlistController);

View file

@ -0,0 +1,6 @@
export function showToast(details) {
let event = new CustomEvent('weiroToast', {
detail: details
});
window.dispatchEvent(event);
}