Skip to content

Commit

Permalink
feat: add support for GetResponse api
Browse files Browse the repository at this point in the history
  • Loading branch information
Mateusz Cholcha committed Oct 22, 2024
1 parent 44129b3 commit bf723dd
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 13 deletions.
2 changes: 1 addition & 1 deletion components/auth/RegisterForm.vue
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const onSubmit = form.handleSubmit(async () => {
...registerFormDto.value,
captcha_token: recaptchaToken,
})
if (newsletterConsent.value) newsletterSubscribe(user.email)
if (newsletterConsent.value) await newsletterSubscribe(user.email)
ev.emit(HeseyaEvent.Register, user)
emit('registered', user)
Expand Down
27 changes: 27 additions & 0 deletions composables/useGetResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
export const useGetResponse = () => {
const config = usePublicRuntimeConfig()

const enabled = computed(() => config.getresponseApiEnabled)
const webConnectEnabled = computed(() => !!config.getresponseWebConnect)

const subscribe = async (data: { email: string }) => {
if (!config.getresponseApiEnabled) {
// eslint-disable-next-line no-console
console.error('[useGetResponse] GetResponse key not set in environment variables')
return Promise.resolve({ success: false })
}

// sending a request to the server (nuxt)
return await fetch('/api/newsletter', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ email: data.email }),
})
.then(() => ({ success: true }))
.catch(() => ({ success: false }))
}

return { subscribe, enabled, webConnectEnabled }
}
35 changes: 25 additions & 10 deletions composables/useNewsletter.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,35 @@
import { useGetResponse } from '~/composables/useGetResponse'

/**
* Wrapper for Edrone newsletter subscription providers that may be used in the future.
* Wrapper for newsletter subscription providers that may be used in the future.
*/
export const useNewsletter = () => {
const { subscribe: edroneSubscribe, enabled: edroneEnabled } = useEdrone()
const { subscribe: grSubscribe, enabled: grEnabled } = useGetResponse()

const enabled = computed(() => edroneEnabled.value || grEnabled.value)

const subscribe = async (email: string) => {
subscribeEdrone(email)
await subscribeGetResponse(email)
}

const subscribeEdrone = (email: string) => {
if (!edroneEnabled.value) return

const enabled = computed(() => edroneEnabled.value)
edroneSubscribe({
email,
customer_tags: 'Newsletter',
first_name: '',
})
}

const subscribe = (email: string) => {
if (!enabled.value) return
const subscribeGetResponse = async (email: string) => {
if (!grEnabled.value) return

if (edroneEnabled.value)
edroneSubscribe({
email,
customer_tags: 'Newsletter',
first_name: '',
})
await grSubscribe({
email,
})
}

return { subscribe, enabled }
Expand Down
2 changes: 1 addition & 1 deletion pages/checkout/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const createOrder = async () => {
try {
order = await checkout.createOrder()
if (checkout.consents.newsletter) newsletterSubscribe(checkout.email)
if (checkout.consents.newsletter) await newsletterSubscribe(checkout.email)
} catch (e: any) {
const error = formatError(e)
notify({
Expand Down
2 changes: 1 addition & 1 deletion server/api/newsletter.post.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default defineEventHandler(async (event) => {

if (!(!!config.getresponseApiKey && !!config.getresponseCampaignId)) {
// eslint-disable-next-line no-console
console.error('[useGetResponse] GetResponse key not set in environment variables')
console.error('[SERVER [POST] /newsletter] GetResponse key not set in environment variables')

throw createError({
statusCode: 500,
Expand Down

0 comments on commit bf723dd

Please sign in to comment.