Skip to content

Commit

Permalink
feat: added resend & email.js on forms
Browse files Browse the repository at this point in the history
  • Loading branch information
mde3 committed Dec 17, 2024
1 parent 292b002 commit a702276
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 29 deletions.
1 change: 0 additions & 1 deletion apps/website/.env.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
RESEND_API_KEY=
RESEND_FROM_EMAIL=
NEXT_PUBLIC_SERVICE_ID=
NEXT_PUBLIC_TEMPLATE_ID_BUSINESS=
NEXT_PUBLIC_PUBLIC_KEY=
10 changes: 4 additions & 6 deletions apps/website/src/app/_action.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
"use server";
import { z } from "zod";
import { Resend } from "resend";
import { formSchema } from "@/lib/schema";
import { basePartnerSchema } from "@/lib/schema";
import PartnerEmail from "@/emails/partners";

type Inputs = z.infer<typeof formSchema>
import { FormInputType } from "@/components/shared/partners/type";

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

export async function sendEmail(data: Inputs) {
const result = formSchema.safeParse(data)
export async function sendEmail(data: FormInputType) {
const result = basePartnerSchema.safeParse(data)

if (result.success) {
const { name, email } = result.data
Expand Down
10 changes: 5 additions & 5 deletions apps/website/src/components/shared/partners/PartnerBusiness.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { formSchema } from "@/lib/schema";
import { businessPartnerSchema } from "@/lib/schema";
import { Input } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
Expand All @@ -16,7 +16,7 @@ export const PartnerBusiness = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(formSchema),
resolver: zodResolver(businessPartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand Down Expand Up @@ -134,11 +134,11 @@ export const PartnerBusiness = () => {
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 cursor-not-allowed'
: 'bg-primary hover:bg-secondary'
}
`}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { formSchema, mappedVehicleStatuses } from "@/lib/schema";
import { courierPartnerSchema, mappedVehicleStatuses } from "@/lib/schema";
import { Input, Select } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
Expand All @@ -16,7 +16,7 @@ export const PartnerCourier = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(formSchema),
resolver: zodResolver(courierPartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand Down Expand Up @@ -73,7 +73,7 @@ export const PartnerCourier = () => {
user_name: data.name,
user_email: data.email,
user_phone: data.phoneNumber.toString(),
user_message: `Vehicle Status: ${data.vehicleStatus}`
user_vehicle_status: data.vehicleDetails
},
{
publicKey: process.env.NEXT_PUBLIC_PUBLIC_KEY!,
Expand Down Expand Up @@ -115,7 +115,7 @@ export const PartnerCourier = () => {
</div>
<Select
label="Do you own a vehicle?"
name="vehicleStatus"
name="vehicleDetails"
register={register}
errors={errors}
options={vehicleStatusOptions}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { useState } from "react";
import { useForm, SubmitHandler } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { formSchema } from "@/lib/schema";
import { supplierPartnerSchema } from "@/lib/schema";
import { Input, TextArea } from "../Form";
import { HiOutlineArrowUpRight } from "react-icons/hi2";
import { FormInputType } from "./type";
Expand All @@ -16,7 +16,7 @@ export const PartnerSupplier = () => {
reset,
formState: {errors, isSubmitting}
} = useForm<FormInputType>({
resolver: zodResolver(formSchema),
resolver: zodResolver(supplierPartnerSchema),
})

const [submissionStatus, setSubmissionStatus] = useState<{
Expand Down
11 changes: 9 additions & 2 deletions apps/website/src/components/shared/partners/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import { formSchema } from "@/lib/schema";
import { z } from "zod";
import { businessPartnerSchema, courierPartnerSchema, supplierPartnerSchema } from "@/lib/schema";

export type FormInputType = z.infer<typeof formSchema>
export type FormInputType = {
name: string;
email: string;
phoneNumber: string;
companyName: string;
vehicleDetails?: "yes" | "no";
supplyDetails?: string;
}
28 changes: 19 additions & 9 deletions apps/website/src/lib/schema.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,39 @@ const phoneRegex = new RegExp(
/^([+]?[\s0-9]+)?(\d{3}|[(]?[0-9]+[)])?([-]?[\s]?[0-9])+$/
);

//array for select elements
// array for select elements
export const vehicleStatuses = [
'yes',
'no',
] as const
] as const;

export type VehicleStatus = typeof vehicleStatuses[number]
export type VehicleStatus = typeof vehicleStatuses[number];

export const mappedVehicleStatuses: { [key in VehicleStatus]: string } = {
yes: 'Yes',
no: 'No',
}
};

export const formSchema = z.object({
export const basePartnerSchema = z.object({
name: z.string().min(3, 'Must be at least 3 characters'),
email: z.string().email({message: "Email is required",}),
email: z.string().email({message: "Email is required"}),
phoneNumber: z.string().regex(phoneRegex, 'Phone number is required'),
});

export const businessPartnerSchema = basePartnerSchema.extend({
companyName: z.string().min(3, 'Must be at least 3 characters'),
});

export const supplierPartnerSchema = basePartnerSchema.extend({
companyName: z.string().min(3, 'Must be at least 3 characters'),
vehicleStatus: z.enum(vehicleStatuses, {
errorMap: () => ({ message: 'Please select your vehicle status' }),
}),
supplyDetails: z
.string()
.min(3, {message: 'Must be at least 3 characters'})
.max(250, {message: 'Must not exceed 250 characters'}),
});

export const courierPartnerSchema = basePartnerSchema.extend({
vehicleDetails: z.enum(vehicleStatuses, {
errorMap: () => ({ message: 'Please select your vehicle status' }),
}),
});

0 comments on commit a702276

Please sign in to comment.