Added keyboard shortcuts for post editing.
This commit is contained in:
parent
4f7058bf36
commit
44d35c6ccb
13 changed files with 215 additions and 28 deletions
79
assets/js/controllers/postedit.js
Normal file
79
assets/js/controllers/postedit.js
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
import { Controller } from "@hotwired/stimulus"
|
||||
import { showToast } from "../services/toast";
|
||||
|
||||
export default class PosteditController extends Controller {
|
||||
static values = {
|
||||
saveAction: String,
|
||||
};
|
||||
|
||||
connect() {
|
||||
console.log("connected");
|
||||
}
|
||||
|
||||
async save(ev) {
|
||||
ev.preventDefault();
|
||||
|
||||
try {
|
||||
await this._postForm(this.saveActionValue);
|
||||
|
||||
showToast({
|
||||
title: "💾 Post Saved",
|
||||
body: (this.saveActionValue === "Save Draft") ? "Post saved as draft." : "Post updated.",
|
||||
});
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
showToast({
|
||||
title: "❌ Error",
|
||||
body: "Unable to save post. Please try again later.",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async publish(ev) {
|
||||
ev.preventDefault();
|
||||
|
||||
try {
|
||||
await this._postForm("Publish");
|
||||
|
||||
window.location.href = this.element.getAttribute("action");
|
||||
} catch (e) {
|
||||
console.error(e);
|
||||
showToast({
|
||||
title: "❌ Error",
|
||||
body: "Unable to publish post. Please try again later.",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async _postForm(action) {
|
||||
if (this._isPosting) {
|
||||
return;
|
||||
}
|
||||
this._isPosting = true;
|
||||
|
||||
try {
|
||||
const formData = new FormData(this.element);
|
||||
let data = Object.fromEntries(formData.entries());
|
||||
data = {...data, action: action || 'save'};
|
||||
|
||||
const response = await fetch(this.element.getAttribute("action"), {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP error! status: ${response.status}`);
|
||||
}
|
||||
|
||||
return response.json();
|
||||
} finally {
|
||||
this._isPosting = false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,5 +1,4 @@
|
|||
import { Controller } from "@hotwired/stimulus"
|
||||
|
||||
import { showToast } from "../services/toast";
|
||||
|
||||
export default class PostlistController extends Controller {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue