feat:convert && fix:api

This commit is contained in:
Gautier Arcin 2021-09-02 20:02:58 +02:00
parent c27c6752c9
commit 9a9faeb015
6 changed files with 70 additions and 27 deletions

View File

@ -4,7 +4,16 @@ NEXT_PUBLIC_GISCUS_CATEGORY=
NEXT_PUBLIC_GISCUS_CATEGORY_ID=
NEXT_PUBLIC_UTTERANCES_REPO=
NEXT_PUBLIC_DISQUS_SHORTNAME=
NEXT_PUBLIC_MAILCHIMP_API_KEY=
NEXT_PUBLIC_MAILCHIMP_API_SERVER=
NEXT_PUBLIC_MAILCHIMP_AUDIENCE_ID=
NEXT_PUBLIC_BUTTONDOWN=
MAILCHIMP_API_KEY=
MAILCHIMP_API_SERVER=
MAILCHIMP_AUDIENCE_ID=
BUTTONDOWN_API_URL=https://api.buttondown.email/v1/
BUTTONDOWN_API_KEY=
CONVERTKIT_API_URL=https://api.convertkit.com/v3/
CONVERTKIT_API_KEY=
CONVERTKIT_TIPS_FORM_ID=
CONVERTKIT_MONTHLY_FORM_ID=

View File

@ -46,7 +46,7 @@ const FormSubscribe = () => {
className="appearance-none w-full px-4 py-2 border border-neutrals-cool-grey-300 text-base rounded-md text-neutrals-cool-grey-900 bg-white dark:bg-black placeholder-gray-500 focus:outline-none focus:ring-primary-400 dark:focus:border-primary-600 lg:max-w-xs"
id="email-input"
name="email"
placeholder="Enter your email"
placeholder={subscribed ? "You're subscribed !" : 'Enter your email'}
ref={inputEl}
required
type="email"

View File

@ -23,13 +23,9 @@ const siteMetadata = {
googleAnalyticsId: '', // e.g. UA-000000-2 or G-XXXXXXX
},
newsletter: {
provider: 'mailchimp', // supported providers: mailchimp, buttondown
mailChimpConfig: {
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
apiServer: process.env.NEXT_PUBLIC_MAILCHIMP_API_SERVER,
audienceId: process.env.NEXT_PUBLIC_MAILCHIMP_AUDIENCE_ID,
},
buttondownConfig: process.env.NEXT_PUBLIC_BUTTONDOWN,
// supports mailchimp, buttondown, convertkit
// Please add your .env file and modify it according to your selection
provider: 'mailchimp',
},
comment: {
// Select a provider and use the environment variables associated to it

View File

@ -1,15 +1,15 @@
// eslint-disable-next-line import/no-anonymous-default-export
export default async (req, res) => {
const { email } = req.body
console.log('hello 1')
if (!email) {
return res.status(400).json({ error: 'Email is required' })
}
try {
const API_KEY = process.env.NEXT_PUBLIC_BUTTONDOWN
const response = await fetch(`https://api.buttondown.email/v1/subscribers`, {
const API_KEY = process.env.BUTTONDOWN_API_KEY
const buttondownRoute = `${process.env.BUTTONDOWN_API_URL}subscribers`
console.log('route : ', buttondownRoute)
const response = await fetch(buttondownRoute, {
body: JSON.stringify({
email,
}),
@ -19,10 +19,9 @@ export default async (req, res) => {
},
method: 'POST',
})
const responseMessage = await response.json()
if (response.status >= 400) {
return res.status(500).json({ error: responseMessage[0] })
return res.status(500).json({ error: `There was an error subscribing to the list.` })
}
return res.status(201).json({ error: '' })

42
pages/api/convertkit.js Normal file
View File

@ -0,0 +1,42 @@
/* eslint-disable import/no-anonymous-default-export */
export default async (req, res) => {
const { email, list } = req.query
if (!email) {
return res.status(400).json({ error: 'Email is required' })
}
if (!list) {
return res.status(400).json({ error: 'List is required' })
}
try {
const FORM_ID =
list === '12-tips-production-apps'
? process.env.CONVERTKIT_TIPS_FORM_ID
: process.env.CONVERTKIT_MONTHLY_FORM_ID
const API_KEY = process.env.CONVERTKIT_API_KEY
const API_URL = process.env.CONVERTKIT_API_URL
// Send request to ConvertKit
const data = { email, api_key: API_KEY }
const response = await fetch(`${API_URL}forms/${FORM_ID}/subscribe`, {
body: JSON.stringify(data),
headers: {
'Content-Type': 'application/json',
},
method: 'POST',
})
if (response.status >= 400) {
return res.status(400).json({
error: `There was an error subscribing to the list.`,
})
}
return res.status(201).json({ error: '' })
} catch (error) {
return res.status(500).json({ error: error.message || error.toString() })
}
}

View File

@ -1,8 +1,8 @@
import mailchimp from '@mailchimp/mailchimp_marketing'
mailchimp.setConfig({
apiKey: process.env.NEXT_PUBLIC_MAILCHIMP_API_KEY,
server: process.env.NEXT_PUBLIC_MAILCHIMP_API_SERVER, // E.g. us1
apiKey: process.env.MAILCHIMP_API_KEY,
server: process.env.MAILCHIMP_API_SERVER, // E.g. us1
})
// eslint-disable-next-line import/no-anonymous-default-export
@ -14,13 +14,10 @@ export default async (req, res) => {
}
try {
const test = await mailchimp.lists.addListMember(
process.env.NEXT_PUBLIC_MAILCHIMP_AUDIENCE_ID,
{
email_address: email,
status: 'subscribed',
}
)
const test = await mailchimp.lists.addListMember(process.env.MAILCHIMP_AUDIENCE_ID, {
email_address: email,
status: 'subscribed',
})
return res.status(201).json({ error: '' })
} catch (error) {
return res.status(500).json({ error: error.message || error.toString() })