Skip to content

Commit

Permalink
chore: removed email.js, now entirely using resend
Browse files Browse the repository at this point in the history
  • Loading branch information
mde3 committed Dec 23, 2024
1 parent a702276 commit e4f6429
Show file tree
Hide file tree
Showing 8 changed files with 170 additions and 125 deletions.
6 changes: 3 additions & 3 deletions apps/website/.env.example
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
RESEND_API_KEY=
NEXT_PUBLIC_SERVICE_ID=
NEXT_PUBLIC_TEMPLATE_ID_BUSINESS=
NEXT_PUBLIC_PUBLIC_KEY=
RESEND_FROM_DOMAIN_EMAIL=
RESEND_REPLY_TO_EMAIL=
RESEND_TO_EMAIL=
45 changes: 41 additions & 4 deletions apps/website/src/app/_action.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Resend } from "resend";
import { basePartnerSchema } from "@/lib/schema";
import PartnerEmail from "@/emails/partners";
import { FormInputType } from "@/components/shared/partners/type";
import PartnerMessage from "@/emails/partner-message";

const resend = new Resend(process.env.RESEND_API_KEY)

Expand All @@ -13,10 +14,9 @@ export async function sendEmail(data: FormInputType) {
const { name, email } = result.data
try {
const data = await resend.emails.send({
// from: `Sahil <${process.env.RESEND_FROM_EMAIL}>`,
from: 'Sahil <[email protected]>',
// to: [email],
to: ['[email protected]'],
from: `Sahil <${process.env.RESEND_FROM_DOMAIN_EMAIL}>`,
to: [email],
replyTo: `${process.env.RESEND_REPLY_TO_EMAIL}`,
subject: 'Partnering with Sahil',
text: `Name: ${name}\nEmail: ${email}`,
react: PartnerEmail({ name })
Expand All @@ -31,3 +31,40 @@ export async function sendEmail(data: FormInputType) {
return { success: false, error: result.error.format() }
}
}

export async function storeUserDetails(data: FormInputType) {
const result = basePartnerSchema.safeParse(data)

if (result.success) {
const {
name,
email,
phoneNumber,
companyName,
supplyDetails,
vehicleDetails
} = result.data

try {
const data = await resend.emails.send({
from: `Sahil <${process.env.RESEND_FROM_DOMAIN_EMAIL}>`,
to: `${process.env.RESEND_TO_EMAIL}`,
subject: `New Partner Submission from ${name}`,
react: PartnerMessage({
name,
email,
phoneNumber,
companyName,
supplyDetails,
vehicleDetails
})
})
return { success: true, data }
} catch (error) {
return { success: false, error }
}
}
if (result.error) {
return { success: false, error: result.error.format() }
}
}
39 changes: 7 additions & 32 deletions apps/website/src/components/shared/partners/PartnerBusiness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { businessPartnerSchema } from "@/lib/schema";
import { basePartnerSchema } from "@/lib/schema";
import { Input } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
import { sendEmail } from "@/app/_action";
import emailjs from '@emailjs/browser';
import { sendEmail, storeUserDetails } from "@/app/_action";

export const PartnerBusiness = () => {
const {
Expand All @@ -16,7 +15,7 @@ export const PartnerBusiness = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(businessPartnerSchema),
resolver: zodResolver(basePartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand All @@ -34,10 +33,10 @@ export const PartnerBusiness = () => {
try {
// first, send email via Resend
const resendResult = await sendEmail(data);
// then, send email via Email.js
const emailJsResult = await sendEmailViaEmailJs(data);
// store user details
const storeResult = await storeUserDetails(data);

if (resendResult?.success && emailJsResult) {
if (resendResult?.success && storeResult) {
setSubmissionStatus({
type: 'success',
message: 'Message sent successfully!'
Expand All @@ -58,30 +57,6 @@ export const PartnerBusiness = () => {
}
}

// helper function to send email via Email.js
const sendEmailViaEmailJs = async (data: FormInputType): Promise<boolean> => {
try {
await emailjs.send(
process.env.NEXT_PUBLIC_SERVICE_ID!,
process.env.NEXT_PUBLIC_TEMPLATE_ID_BUSINESS!,
{
// map form data to Email.js template fields
user_name: data.name,
user_email: data.email,
user_phone: data.phoneNumber.toString(),
user_company: data.companyName,
},
{
publicKey: process.env.NEXT_PUBLIC_PUBLIC_KEY!,
}
);
return true;
} catch (error) {
console.error('Email.js send error:', error);
return false;
}
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col space-y-8">
Expand Down Expand Up @@ -137,7 +112,7 @@ export const PartnerBusiness = () => {
group inline-flex items-center justify-center gap-2 rounded-full px-6 py-3 text-sm text-white font-semibold transition-colors
${
isSubmitting
? 'bg-gray-400 cursor-not-allowed'
? 'bg-gray-400 text-white cursor-not-allowed'
: 'bg-primary hover:bg-secondary'
}
`}
Expand Down
43 changes: 9 additions & 34 deletions apps/website/src/components/shared/partners/PartnerCourier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { courierPartnerSchema, mappedVehicleStatuses } from "@/lib/schema";
import { basePartnerSchema, mappedVehicleStatuses } from "@/lib/schema";
import { Input, Select } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
import { sendEmail } from "@/app/_action";
import emailjs from '@emailjs/browser';
import { sendEmail, storeUserDetails } from "@/app/_action";

export const PartnerCourier = () => {
const {
Expand All @@ -16,7 +15,7 @@ export const PartnerCourier = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(courierPartnerSchema),
resolver: zodResolver(basePartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand All @@ -38,10 +37,10 @@ export const PartnerCourier = () => {
try {
// first, send email via Resend
const resendResult = await sendEmail(data);
// then, send email via Email.js
const emailJsResult = await sendEmailViaEmailJs(data);
// store user details
const storeResult = await storeUserDetails(data);

if (resendResult?.success && emailJsResult) {
if (resendResult?.success && storeResult) {
setSubmissionStatus({
type: 'success',
message: 'Message sent successfully!'
Expand All @@ -62,30 +61,6 @@ export const PartnerCourier = () => {
}
}

// helper function to send email via Email.js
const sendEmailViaEmailJs = async (data: FormInputType): Promise<boolean> => {
try {
await emailjs.send(
process.env.NEXT_PUBLIC_SERVICE_ID!,
process.env.NEXT_PUBLIC_TEMPLATE_ID_BUSINESS!,
{
// map form data to Email.js template fields
user_name: data.name,
user_email: data.email,
user_phone: data.phoneNumber.toString(),
user_vehicle_status: data.vehicleDetails
},
{
publicKey: process.env.NEXT_PUBLIC_PUBLIC_KEY!,
}
);
return true;
} catch (error) {
console.error('Email.js send error:', error);
return false;
}
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col space-y-8">
Expand Down Expand Up @@ -138,11 +113,11 @@ export const PartnerCourier = () => {
type="submit"
disabled={isSubmitting}
className={`
group inline-flex items-center justify-center gap-2 rounded-full px-6 py-3 text-sm font-semibold transition-colors
group inline-flex items-center justify-center gap-2 rounded-full px-6 py-3 text-sm text-white font-semibold transition-colors
${
isSubmitting
? 'bg-gray-400 text-black cursor-not-allowed'
: 'bg-primary text-white hover:bg-secondary'
? 'bg-gray-400 text-white cursor-not-allowed'
: 'bg-primary hover:bg-secondary'
}
`}
>
Expand Down
44 changes: 9 additions & 35 deletions apps/website/src/components/shared/partners/PartnerSupplier.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { supplierPartnerSchema } from "@/lib/schema";
import { basePartnerSchema } from "@/lib/schema";
import { Input, TextArea } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
import { sendEmail } from "@/app/_action";
import emailjs from '@emailjs/browser';
import { sendEmail, storeUserDetails } from "@/app/_action";

export const PartnerSupplier = () => {
const {
Expand All @@ -16,7 +15,7 @@ export const PartnerSupplier = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(supplierPartnerSchema),
resolver: zodResolver(basePartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand All @@ -34,10 +33,10 @@ export const PartnerSupplier = () => {
try {
// first, send email via Resend
const resendResult = await sendEmail(data);
// then, send email via Email.js
const emailJsResult = await sendEmailViaEmailJs(data);
// store user details
const storeResult = await storeUserDetails(data);

if (resendResult?.success && emailJsResult) {
if (resendResult?.success && storeResult) {
setSubmissionStatus({
type: 'success',
message: 'Message sent successfully!'
Expand All @@ -58,31 +57,6 @@ export const PartnerSupplier = () => {
}
}

// helper function to send email via Email.js
const sendEmailViaEmailJs = async (data: FormInputType): Promise<boolean> => {
try {
await emailjs.send(
process.env.NEXT_PUBLIC_SERVICE_ID!,
process.env.NEXT_PUBLIC_TEMPLATE_ID_BUSINESS!,
{
// map form data to Email.js template fields
user_name: data.name,
user_email: data.email,
user_phone: data.phoneNumber.toString(),
user_company: data.companyName,
user_message: `Supply Details: ${data.supplyDetails}`
},
{
publicKey: process.env.NEXT_PUBLIC_PUBLIC_KEY!,
}
);
return true;
} catch (error) {
console.error('Email.js send error:', error);
return false;
}
}

return (
<form onSubmit={handleSubmit(onSubmit)}>
<div className="flex flex-col space-y-8">
Expand Down Expand Up @@ -142,11 +116,11 @@ export const PartnerSupplier = () => {
type="submit"
disabled={isSubmitting}
className={`
group inline-flex items-center justify-center gap-2 rounded-full px-6 py-3 text-sm font-semibold transition-colors
group inline-flex items-center justify-center gap-2 rounded-full px-6 py-3 text-sm text-white font-semibold transition-colors
${
isSubmitting
? 'bg-gray-400 text-black cursor-not-allowed'
: 'bg-primary text-white hover:bg-secondary'
? 'bg-gray-400 text-white cursor-not-allowed'
: 'bg-primary hover:bg-secondary'
}
`}
>
Expand Down
4 changes: 2 additions & 2 deletions apps/website/src/components/shared/partners/type.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { businessPartnerSchema, courierPartnerSchema, supplierPartnerSchema } from "@/lib/schema";
// import { z } from "zod";
// import { businessPartnerSchema, courierPartnerSchema, supplierPartnerSchema } from "@/lib/schema";

export type FormInputType = {
name: string;
Expand Down
Loading

0 comments on commit e4f6429

Please sign in to comment.