Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat : autosave functionality in editor #125

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 11 additions & 4 deletions admin/src/api/post/content-types/post/lifecycles.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,9 @@ async function modifyContentAndSetErrorMsg(event) {
// generate table of contents
await generateTOC(result, event);
await generateNewToc(result, event);
if (result.tags) {
event.params.data.tags = await TagsInput(result.tags);
}
}
}

Expand Down Expand Up @@ -240,10 +243,12 @@ async function generateTOC(result, event) {

event.params.data.content = dom.serialize();
event.params.data.toc = toc += "</ul></li>";
event.params.data.tags = await TagsInput(result.tags);

// set published on
if (!event.params.data.published_on) {
if (
event.params.data.publishedAt == null &&
!event.params.data.published_on
) {
event.params.data.published_on = new Date();
}
}
Expand All @@ -256,10 +261,12 @@ async function generateNewToc(result, event) {

event.params.data.new_content = dom.serialize();
event.params.data.new_toc = toc += "</ul>";
event.params.data.tags = await TagsInput(result.tags);

// set published on
if (!event.params.data.published_on) {
if (
event.params.data.publishedAt == null &&
!event.params.data.published_on
) {
event.params.data.published_on = new Date();
}
} else {
Expand Down
22 changes: 22 additions & 0 deletions admin/src/api/post/controllers/post.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,26 @@ module.exports = createCoreController("api::post.post", ({ strapi }) => ({

return this.transformResponse(posts);
},

async autosave(ctx) {
const { postId, newData } = ctx.request.body;
let posts = await strapi.db.query("api::post.post").update({
where: { id: postId },
data: {
new_blog_content: newData,
},
populate: {
author: {
populate: {
image: true,
},
},
tags: true,
category: true,
image: true,
},
});

return this.transformResponse(posts);
},
}));
8 changes: 8 additions & 0 deletions admin/src/api/post/routes/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ module.exports = {
auth: false,
},
},
{
method: "PUT",
path: "/post/autosave",
handler: "post.autosave",
config: {
auth: false,
},
},
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ const Editor = ({ onChange, name, value }) => {
},
};

async function autosave(newData) {
const urlPart = window.location.href.split("/");
const postId = urlPart[urlPart.length - 1];

try {
await fetch("/v1/post/autosave", {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
postId,
newData,
}),
})
.then((response) => {
// console.log(response);
})
.catch((error) => {
console.log(error);
});
} catch (error) {
console.error("Autosave error:", error);
}
}

return (
<>
<div
Expand All @@ -67,6 +93,7 @@ const Editor = ({ onChange, name, value }) => {
onChange({ target: { name, value: newData } });
} else {
onChange({ target: { name, value: JSON.stringify(newData) } });
autosave(newData);
}
}}
tools={{ ...requiredTools, ...customTools, ...customImageTool }}
Expand Down
Loading