Connect, collaborate, and grow with a community of TypeScript developers
migrate emails to mailchimp (#745)
bautistaaa5972fa4a39ef
diff --git a/.gitignore b/.gitignoreindex 5b149ea1..e366061b 100644--- a/.gitignore+++ b/.gitignore@@ -24,6 +24,7 @@ dist# misc.DS_Store*.pem+.tmp# debugnpm-debug.log*diff --git a/apps/web/package.json b/apps/web/package.jsonindex 6ffcda94..b4b99058 100644--- a/apps/web/package.json+++ b/apps/web/package.json@@ -19,6 +19,7 @@"dependencies": {"@hookform/error-message": "^2.0.1","@hookform/resolvers": "^3.1.1",+ "@mailchimp/mailchimp_marketing": "^3.0.80","@monaco-editor/loader": "^1.3.3","@monaco-editor/react": "^4.5.1","@next/bundle-analyzer": "^13.4.19",@@ -37,6 +38,7 @@"@tanstack/react-query-devtools": "^4.35.3","@tanstack/react-table": "^8.9.3","@types/lodash": "^4.14.195",+ "@types/mailchimp__mailchimp_marketing": "^3.0.13","@uiw/react-md-editor": "^3.23.3","@uploadthing/react": "^5.5.0","@vercel/analytics": "^1.0.2",diff --git a/apps/web/src/app/[locale]/(email)/waitlist/_components/waitlist-form.tsx b/apps/web/src/app/[locale]/(email)/waitlist/_components/waitlist-form.tsxindex 01fb6ad0..c0c3b70e 100644--- a/apps/web/src/app/[locale]/(email)/waitlist/_components/waitlist-form.tsx+++ b/apps/web/src/app/[locale]/(email)/waitlist/_components/waitlist-form.tsx@@ -1,13 +1,8 @@'use client';-import { useState } from 'react';-import { z } from 'zod';import { zodResolver } from '@hookform/resolvers/zod';-import { useForm } from 'react-hook-form';-import { AlertCircle, MailCheck } from '@repo/ui/icons';-import clsx from 'clsx';-import { uploadWaitlistEntry } from './create.action';import { Alert, AlertDescription } from '@repo/ui/components/alert';+import { Button } from '@repo/ui/components/button';import { Form, FormControl, FormField, FormItem } from '@repo/ui/components/form';import { Input } from '@repo/ui/components/input';import {@@ -17,7 +12,11 @@ import {SelectTrigger,SelectValue,} from '@repo/ui/components/select';-import { Button } from '@repo/ui/components/button';+import { AlertCircle, MailCheck } from '@repo/ui/icons';+import { useMutation } from '@tanstack/react-query';+import clsx from 'clsx';+import { useForm } from 'react-hook-form';+import { z } from 'zod';const waitlistFormSchema = z.object({name: z.string().min(1, 'Please enter your name'),@@ -28,11 +27,12 @@ const waitlistFormSchema = z.object({export type WaitlistFormSchema = z.infer<typeof waitlistFormSchema>;export function WaitlistForm() {- const [state, setState] = useState<'duplicate' | 'error' | 'idle' | 'submitting' | 'success'>(- 'idle',- );-- const isSubmitting = state === 'submitting';+ const mutation = useMutation({+ mutationFn: subscribe,+ onSuccess: () => {+ form.reset();+ },+ });const form = useForm<WaitlistFormSchema>({resolver: zodResolver(waitlistFormSchema),@@ -43,18 +43,7 @@ export function WaitlistForm() {});async function onSubmit(data: WaitlistFormSchema) {- try {- setState('submitting');- const status = await uploadWaitlistEntry(data);- if (status === 'duplicate') {- setState('duplicate');- } else {- setState('success');- }- form.reset();- } catch (e) {- setState('error');- }+ mutation.mutate(data);}return (@@ -146,21 +135,18 @@ export function WaitlistForm() {<ButtonclassName="wl-form-button group relative mt-6 w-full overflow-hidden rounded-xl px-[2px] py-[2px] font-bold transition-shadow duration-300 hover:shadow-[0_0.5rem_2rem_-0.75rem_#3178c6] dark:hover:shadow-[0_0.5rem_2rem_-0.75rem_#5198f6]"- disabled={isSubmitting}+ disabled={mutation.status === 'loading'}type="submit"><span className="h-full w-full rounded-[10px] bg-white px-4 py-2 text-center font-bold text-black transition-colors duration-300 group-hover:bg-blue-100 dark:bg-black dark:text-white group-hover:dark:bg-cyan-950">- {isSubmitting ? 'Submitting...' : 'Join the waitlist'}+ {mutation.status === 'loading' ? 'Submitting...' : 'Join the waitlist'}</span></Button></form></Form><div className="mt-3">- {state === 'duplicate' && (- <AlertDestructive text="We already have your email. Thanks for signing up!" />- )}- {state === 'success' && <AlertSuccess />}- {state === 'error' && <AlertDestructive />}+ {mutation.status === 'success' && <AlertSuccess />}+ {mutation.status === 'error' && <AlertDestructive />}</div></div>);@@ -200,3 +186,17 @@ export function AlertSuccess() {</Alert>);}++async function subscribe(data: WaitlistFormSchema) {+ const response = await fetch('/api/subscribe', {+ method: 'POST',+ body: JSON.stringify(data),+ });++ if (!response.ok) {+ const text = await response.text();+ throw new Error(text);+ }++ return response.json();+}diff --git a/apps/web/src/app/api/subscribe/route.ts b/apps/web/src/app/api/subscribe/route.tsnew file mode 100644index 00000000..03b49b56--- /dev/null+++ b/apps/web/src/app/api/subscribe/route.ts@@ -0,0 +1,39 @@+import mailchimp from '@mailchimp/mailchimp_marketing';++const AUDIENCE_ID = process.env.MAILCHIMP_AUDIENCE_ID;+mailchimp.setConfig({+ apiKey: process.env.MAILCHIMP_API_KEY,+ server: process.env.MAILCHIMP_API_SERVER,+});++export async function POST(req: Request) {+ const { name, email, intention } = await req.json();++ const merge_fields = {+ name,+ REASON: intention,+ };++ try {+ const response = await mailchimp.lists.addListMember(AUDIENCE_ID!, {+ email_address: email,+ status: 'subscribed',+ merge_fields,+ });++ return new Response(JSON.stringify(response), {+ status: Number(response.status),+ headers: {+ 'content-type': 'application/json',+ },+ });+ } catch (error) {+ // @ts-ignore+ return new Response(error.response.text, {+ status: 500,+ headers: {+ 'content-type': 'application/json',+ },+ });+ }+}diff --git a/pnpm-lock.yaml b/pnpm-lock.yamlindex 31eb25b6..1fa2f524 100644--- a/pnpm-lock.yaml+++ b/pnpm-lock.yaml@@ -201,6 +201,9 @@ importers:'@hookform/resolvers':specifier: ^3.1.1version: 3.1.1([email protected])+ '@mailchimp/mailchimp_marketing':+ specifier: ^3.0.80+ version: 3.0.80'@monaco-editor/loader':specifier: ^1.3.3version: 1.3.3@@ -255,6 +258,9 @@ importers:'@types/lodash':specifier: ^4.14.195version: 4.14.195+ '@types/mailchimp__mailchimp_marketing':+ specifier: ^3.0.13+ version: 3.0.13'@uiw/react-md-editor':specifier: ^3.23.3@@ -1702,6 +1708,16 @@ packages:resolution: {integrity: sha512-GaHYm+c0O9MjZRu0ongGBRbinu8gVAMd2UZjji6jVmqKtZluZnptXGWhz1E8j8D2HJ3f/yMxKAUC0b+57wncIw==}dev: true+ /@mailchimp/[email protected]:+ resolution: {integrity: sha512-Cgz0xPb+1DUjmrl5whAsmqfAChBko+Wf4/PLQE4RvwfPlcq2agfHr1QFiXEhZ8e+GQwQ3hZQn9iLGXwIXwxUCg==}+ engines: {node: '>=10.0.0'}+ dependencies:+ dotenv: 8.6.0+ superagent: 3.8.1+ transitivePeerDependencies:+ - supports-color+ dev: false+/@manypkg/[email protected]:resolution: {integrity: sha512-NEYRVlZCJYhRTqQURhv+WBpDcvmsp/M423Wcdvggv8lYJYD4GtqnTMLrQaTjA10fYt/PIc3tSdwV+wxJnWqPfQ==}engines: {node: '>=14.18.0'}@@ -4113,6 +4129,10 @@ packages:resolution: {integrity: sha512-Hwx9EUgdwf2GLarOjQp5ZH8ZmblzcbTBC2wtQWNKARBSxM9ezRIAUpeDTgoQRAFB0+8CNWXVA9+MaSOzOF3nPg==}dev: false+ /@types/[email protected]:+ resolution: {integrity: sha512-TENsQx6pw3PRkjI2KSPwA4YWfsFp+XVVg7FlX1vqaCATl6r9xZYVat2tBQKSapNQuBDX54G7eaBc2fcUJCzvbA==}+ dev: false+/@types/[email protected]:resolution: {integrity: sha512-DT+iNIRNX884cx0/Q1ja7NyUPpZuv0KPyL5rGNxm1WC1OtHstl7n4Jb7nk+xacNShQMbczJjt8uFzznpp6kYBg==}dependencies:@@ -5266,6 +5286,10 @@ packages:resolution: {integrity: sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==}dev: false+ resolution: {integrity: sha512-Rd3se6QB+sO1TwqZjscQrurpEPIfO0/yYnSin6Q/rD3mOutHvUrCAhJub3r90uNb+SESBuE0QYoB90YdfatsRg==}+ dev: false+resolution: {integrity: sha512-/Srv4dswyQNBfohGpz9o6Yb3Gz3SrUDqBH5rTuhGR7ahtlbYKnVxw2bCFMRljaA7EXHaXZ8wsHdodFvbkhKmqg==}@@ -5299,6 +5323,10 @@ packages:engines: {node: '>= 0.6'}dev: false+ resolution: {integrity: sha512-LDx6oHrK+PhzLKJU9j5S7/Y3jM/mUHvD/DeI1WQmJn652iPC5Y4TBzC9l+5OMOXlyTTA+SmVUPm0HQUwpD5Jqw==}+ dev: false+resolution: {integrity: sha512-yCEafptTtb4bk7GLEQoM8KVJpxAfdBJYaXyzQEgQQQgYrZiDp8SJmGKlYza6CYjEDNstAdNdKA3UuoULlEbS6w==}engines: {node: '>=12.13'}@@ -5592,6 +5620,11 @@ packages:engines: {node: '>=12'}dev: true+ resolution: {integrity: sha512-IrPdXQsk2BbzvCBGBOTmmSH5SodmqZNt4ERAZDmW4CT+tL8VtvinqywuANaFu4bOMWki16nqf0e4oC0QIaDr/g==}+ engines: {node: '>=10'}+ dev: false+resolution: {integrity: sha512-asLFVfWWtJ90ZyOUHMqk7/S2w2guQKxUI2itj3d92ADHhxUSbCMGi1f1cBcJ7xM1To+pE/Khbwo1yuNbMEPKeA==}dependencies:@@ -6673,6 +6706,15 @@ packages:dependencies:is-callable: 1.2.7+ resolution: {integrity: sha512-m21N3WOmEEURgk6B9GLOE4RuWOFf28Lhh9qGYeNlGq4VDXUlJy2th2slBNU8Gp8EzloYZOibZJ7t5ecIrFSjVA==}+ engines: {node: '>= 0.12'}+ dependencies:+ asynckit: 0.4.0+ combined-stream: 1.0.8+ mime-types: 2.1.35+ dev: false+resolution: {integrity: sha512-RHkBKtLWUVwd7SqRIvCZMEvAMoGUp0XU+seQiZejj0COz3RI3hWP4sCv3gZWWLjJTd7rGwcsF5eKZGii0r/hbg==}engines: {node: '>= 6'}@@ -6703,6 +6745,11 @@ packages:fetch-blob: 3.2.0dev: true+ resolution: {integrity: sha512-KcpbcpuLNOwrEjnbpMC0gS+X8ciDoZE1kkqzat4a8vrprf+s9pKNQ/QIwWfbfs4ltgmFl3MD177SNTkve3BwGQ==}+ deprecated: 'Please upgrade to latest, formidable@v2 or formidable@v3! Check these notes: https://bit.ly/2ZEqIau'+ dev: false+resolution: {integrity: sha512-MhLuK+2gUcnZe8ZHlaaINnQLl0xRIGRfcGk2yl8xoQAfHrSsL3rYu6FCmBdkdbhc9EPlwyGHewaRsvwRMJtAlA==}dev: true@@ -7969,6 +8016,11 @@ packages:resolution: {integrity: sha512-8q7VEgMJW4J8tcfVPy8g09NcQwZdbwFEqhe/WZkoIzjn/3TGDwtOCYtXGxA3O8tPzpczCCDgv+P2P5y00ZJOOg==}engines: {node: '>= 8'}+ resolution: {integrity: sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==}+ engines: {node: '>= 0.6'}+ dev: false+resolution: {integrity: sha512-BgHO1aRbolh2hcrzL2d1La37V0Aoz73ymF8rAcKnohLy93titmv62E0gP8Hrx9PKcKrqCZ1BbLGbP3bEhoXYlw==}dependencies:@@ -8248,6 +8300,12 @@ packages:dependencies:mime-db: 1.52.0+ resolution: {integrity: sha512-x0Vn8spI+wuJ1O6S7gnbaQg8Pxh4NNHb7KSINmEWKiPE4RKOplvijn+NkmYmmRgP68mc70j2EbeTFRsrswaQeg==}+ engines: {node: '>=4'}+ hasBin: true+ dev: false+resolution: {integrity: sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==}engines: {node: '>=6'}@@ -9129,6 +9187,13 @@ packages:resolution: {integrity: sha512-rRV+zQD8tVFys26lAGR9WUuS4iUAngJScM+ZRSKtvl5tKeZ2t5bvdNFdNHBW9FWR4guGHlgmsZ1G7BSm2wTbuA==}engines: {node: '>=6'}+ resolution: {integrity: sha512-tDNIz22aBzCDxLtVH++VnTfzxlfeK5CbqohpSqpJgj1Wg/cQbStNAz3NuqCs5vV+pjBsK4x4pN9HlVh7rcYRiA==}+ engines: {node: '>=0.6'}+ dependencies:+ side-channel: 1.0.4+ dev: false+resolution: {integrity: sha512-NuaNSa6flKT5JaSYQzJok04JzTL1CA6aGhv5rfLW3PgqA+M2ChpZQnAC8h8i4ZFkBS8X5RqkDBHA7r4hej3K9A==}@@ -10159,6 +10224,25 @@ packages:pirates: 4.0.6ts-interface-checker: 0.1.13+ resolution: {integrity: sha512-VMBFLYgFuRdfeNQSMLbxGSLfmXL/xc+OO+BZp41Za/NRDBet/BNbkRJrYzCUu0u4GU0i/ml2dtT8b9qgkw9z6Q==}+ engines: {node: '>= 4.0'}+ deprecated: Please upgrade to v7.0.2+ of superagent. We have fixed numerous issues with streams, form-data, attach(), filesystem errors not bubbling up (ENOENT on attach()), and all tests are now passing. See the releases tab for more information at <https://github.com/visionmedia/superagent/releases>.+ dependencies:+ component-emitter: 1.3.0+ cookiejar: 2.1.4+ debug: 3.2.7+ extend: 3.0.2+ form-data: 2.5.1+ formidable: 1.2.6+ methods: 1.1.2+ mime: 1.6.0+ qs: 6.11.2+ readable-stream: 2.3.8+ transitivePeerDependencies:+ - supports-color+ dev: false+resolution: {integrity: sha512-vkpPQAxdCg9SLfPv5GPC5fnGrui/WryktoN9O5+Zif/14QIMjw+RITf/5LbBh+9QpBFb3KNvJth+puz2H8o6GQ==}engines: {node: '>=10'}diff --git a/turbo.json b/turbo.jsonindex 0f550436..24e3dbbf 100644--- a/turbo.json+++ b/turbo.json@@ -23,7 +23,10 @@"PORT","USERNAME","PASSWORD",- "CI"+ "CI",+ "MAILCHIMP_API_KEY",+ "MAILCHIMP_AUDIENCE_ID",+ "MAILCHIMP_API_SERVER"]},"topo": {