Created
December 8, 2020 23:33
-
-
Save kmelve/35d470f27ad8a5c5022eeb46bd3bad71 to your computer and use it in GitHub Desktop.
Create Mailchimp campaign document action proof of concept
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useDocumentOperation } from '@sanity/react-hooks' | |
const HOST = '' | |
const URL = `${HOST}/api/create-community-newsletter` | |
const secret = '' | |
export function NewsletterAction (props) { | |
const { type, id, onComplete, draft, published } = props | |
const doc = draft || published | |
if (!doc) { | |
return null | |
} | |
const { newsLetterCampaign } = doc | |
const { patch, commit } = useDocumentOperation(id, type) | |
return { | |
label: newsLetterCampaign ? 'Open Newsletter' : 'Create newsletter', | |
icon: () => '🐒', | |
onHandle: async () => { | |
if (!doc.newsletter) { | |
onComplete() | |
return null | |
} | |
if (newsLetterCampaign) { | |
const MCURL = `https://us3.admin.mailchimp.com/campaigns/edit?id=${ | |
newsLetterCampaign | |
}` | |
window.open(MCURL, '_blank') | |
onComplete() | |
return null | |
} | |
const campaign = await fetch(URL, { | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json' | |
}, | |
body: JSON.stringify({ | |
title: doc.title, | |
subject_line: doc.excerpt, | |
secret | |
}) | |
}).then(res => console.log(res) || res.json()).catch(error => console.error(error)) | |
patch.execute([ | |
{ | |
set: { | |
newsLetterCampaign: campaign.web_id | |
} | |
} | |
]) | |
commit.execute() | |
onComplete() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment