Have got soft and hard deleting
This commit is contained in:
parent
aef3bb6a1e
commit
3ea5823ca0
27 changed files with 588 additions and 55 deletions
77
assets/js/controllers/postlist.js
Normal file
77
assets/js/controllers/postlist.js
Normal 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.",
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
24
assets/js/controllers/toast.js
Normal file
24
assets/js/controllers/toast.js
Normal 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();
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue