77 lines
2.2 KiB
JavaScript
77 lines
2.2 KiB
JavaScript
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.",
|
|
});
|
|
}
|
|
}
|
|
}
|