Skip to content

Instantly share code, notes, and snippets.

@dugjason
Created March 26, 2021 16:23
Show Gist options
  • Save dugjason/85c2a45c2c8e7b844812cd693addaf6b to your computer and use it in GitHub Desktop.
Save dugjason/85c2a45c2c8e7b844812cd693addaf6b to your computer and use it in GitHub Desktop.
Front Context API - Updating Drafts
<!DOCTYPE html>
<html ng-app="app">
<head>
<title>Front Plugin</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script type="text/javascript" src="//dl.frontapp.com/libs/plugin-sdk-1.0.1.min.js"></script>
</head>
<body>
<button id="updateDraft">Update Draft</button>
<script type="text/javascript" src="./index.js"></script>
</body>
</html>
const updateDraftButton = document.getElementById('updateDraft');
Front.contextUpdates.subscribe(context => {
switch(context.type) {
case 'noConversation':
console.log('No conversation selected');
break;
case 'singleConversation':
console.log('Selected conversation context:', context);
updateDraftButton.addEventListener('click', async () => {
let draftContent = await fetchDemoData()
Front.updateDraft(
context.conversation.draftId,
{
content: {
body: draftContent.body,
type: 'text'
},
updateMode: 'insert'
})
})
break;
case 'multiConversations':
console.log('Multiple conversations selected', context.conversations);
break;
default:
console.error(`Unsupported context type: ${context.type}`);
break;
}
});
async function fetchDemoData() {
let response = await fetch('https://jsonplaceholder.typicode.com/posts/1')
return response.json()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment