Edit Post
After completing the required setup, you can create an instance from window.Predis
to enable all SDK functionality. The editPost
method allows users to modify existing posts through the Predis iFrame interface.
const predis = new window.Predis();
// Event callbacks should be registered before initialization to capture any initialization errors
predis.on("ready", () => {
predis.editPost({
post_id: "POST_ID", // Post ID received from API or createPost onPublish callback
onPostPublish: function (err, data) {
if (err) {
console.error("Error updating post:", err);
} else {
console.log("Post updated successfully:", data);
}
},
});
});
predis.on("error", (error) => {
console.error("SDK Error:", error);
});
predis.initialize({
appId: "YOUR_APP_ID",
embedToken: "USER_EMBED_TOKEN" // Required for user authentication
});
Ensure you call the initialize
method before invoking the editPost
method.
The editPost
method requires a valid post_id
parameter. Additionally, the embedToken
must be included in the initialize
method for user authentication, as post editing requires proper user identification through SSO (Single Sign-On).
The editPost
method provides direct access to the post editor interface, allowing users to modify previously created posts.
Important Considerations
Scope Limitation: The editPost
method does not provide post creation functionality. It only allows editing of existing posts. This method is typically used in combination with Predis APIs, where post creation is handled through API calls, and the SDK's editPost
method is used for subsequent editing operations.
Complete Implementation Example
The following example demonstrates a comprehensive implementation of the Predis SDK for post editing:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Predis SDK - Edit Post</title>
</head>
<!-- Predis.ai SDK -->
<script
type="text/javascript"
src="https://predis.ai/sdk/embed.js"
async
defer
crossorigin="anonymous"
></script>
<body>
<!-- Button to launch the post editor -->
<button id="edit-post-button">Edit Post</button>
<script type="text/javascript">
document.getElementById("edit-post-button").addEventListener("click", function () {
try {
// Create an instance of the Predis SDK
const predis = new window.Predis();
// Handle successful initialization
predis.on("ready", () => {
console.log("SDK initialized successfully");
predis.editPost({
post_id: "YOUR_POST_ID", // Replace with actual post ID
onPostPublish: function (err, data) {
if (err) {
console.error("Error during post update:", err);
// Handle error appropriately in your application
} else {
console.log("Post updated successfully:", data);
// Process the updated post data
}
},
});
});
// Handle SDK errors
predis.on("error", (error) => {
console.error("SDK Error:", error);
// Implement error handling logic
});
// Initialize the SDK with your application ID and embed token
predis.initialize({
appId: "YOUR_APP_ID",
embedToken: "USER_EMBED_TOKEN" // Required for user authentication
});
} catch (error) {
console.error("Failed to initialize Predis SDK:", error);
}
});
</script>
</body>
</html>