-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(root): 🚑 fixed email Invoice feature
- Loading branch information
Showing
9 changed files
with
287 additions
and
153 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,20 +3,22 @@ import { COMPANY, __prod__ } from "../constants"; | |
import { Attachment } from "nodemailer/lib/mailer"; | ||
import SMTPTransport from "nodemailer/lib/smtp-transport"; | ||
|
||
const transportOptions: SMTPTransport.Options = { | ||
host: process.env.RESEND_HOST, | ||
port: parseInt(process.env.RESEND_PORT!), | ||
secure: __prod__, | ||
auth: { | ||
user: process.env.RESEND_AUTH_USER, | ||
pass: process.env.RESEND_AUTH_PASS, | ||
}, | ||
}; | ||
|
||
export async function sendEmail( | ||
to: string, | ||
subject: string, | ||
html: string | ||
): Promise<SMTPTransport.SentMessageInfo> { | ||
const transporter = nodemailer.createTransport({ | ||
host: "smtp.resend.com", | ||
port: 465, | ||
secure: true, | ||
auth: { | ||
user: "resend", | ||
pass: process.env.RESEND_API_KEY, | ||
}, | ||
}); | ||
const transporter = nodemailer.createTransport(transportOptions); | ||
|
||
const info = await transporter.sendMail({ | ||
from: `"${COMPANY.name} 👻" <[email protected]>`, | ||
|
@@ -37,15 +39,7 @@ export async function sendEmailWithAttachment( | |
html: string, | ||
attachments: Attachment[] | ||
): Promise<SMTPTransport.SentMessageInfo> { | ||
const transporter = nodemailer.createTransport({ | ||
host: "smtp.resend.com", | ||
port: 465, | ||
secure: __prod__, | ||
auth: { | ||
user: "resend", | ||
pass: process.env.RESEND_API_KEY, | ||
}, | ||
}); | ||
const transporter = nodemailer.createTransport(transportOptions); | ||
|
||
const info = await transporter.sendMail({ | ||
from: `"${COMPANY.name} 👻" <[email protected]>`, | ||
|
92 changes: 92 additions & 0 deletions
92
apps/storefront/src/components/pages/account/order/EmailInvoice.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
import { Button, Card, HStack, Stack, useToast } from "@chakra-ui/react"; | ||
import { useForm } from "react-hook-form"; | ||
import { yupResolver } from "@hookform/resolvers/yup"; | ||
import * as Yup from "yup"; | ||
import { BiMailSend } from "react-icons/bi"; | ||
import { useEmailInvoiceMutation } from "@/generated/graphql"; | ||
import InputField from "@/components/ui/InputField"; | ||
|
||
interface FormValues { | ||
email: string; | ||
} | ||
|
||
const EmailInvoiceFormSchema = Yup.object({ | ||
email: Yup.string().email().required("Required"), | ||
}); | ||
|
||
const EmailInvoice = ({ orderId }: { orderId: string }) => { | ||
const toast = useToast(); | ||
|
||
const { | ||
register, | ||
handleSubmit, | ||
formState: { errors, touchedFields, isSubmitting }, | ||
} = useForm<FormValues>({ | ||
defaultValues: { | ||
email: "", | ||
}, | ||
resolver: yupResolver(EmailInvoiceFormSchema), | ||
}); | ||
|
||
const [emailInvoiceMutation, { error }] = useEmailInvoiceMutation(); | ||
|
||
return ( | ||
<Card w="full" py="8" px={{ base: "4", md: "10" }} shadow="none"> | ||
<form | ||
onSubmit={handleSubmit(async (values) => { | ||
const res = await emailInvoiceMutation({ | ||
variables: { | ||
email: values.email, | ||
orderId, | ||
}, | ||
}); | ||
|
||
if (!res.data?.emailInvoice) { | ||
toast({ | ||
title: "An Error Occured", | ||
description: error?.message ?? "Please try again later.", | ||
status: "error", | ||
duration: 4000, | ||
isClosable: true, | ||
}); | ||
} else { | ||
toast({ | ||
title: "Email sent", | ||
description: "We've sent you an email with an invoice.", | ||
status: "success", | ||
duration: 4000, | ||
isClosable: true, | ||
}); | ||
} | ||
})} | ||
> | ||
<Stack spacing="6"> | ||
<InputField | ||
register={{ ...register("email") }} | ||
error={errors.email} | ||
touched={touchedFields.email} | ||
name="email" | ||
type="email" | ||
size="lg" | ||
autoComplete="email" | ||
label="Email" | ||
placeholder="[email protected]" | ||
/> | ||
<HStack w="full" justify="center"> | ||
<Button | ||
type="submit" | ||
fontSize="md" | ||
colorScheme="blue" | ||
leftIcon={<BiMailSend fontSize={20} />} | ||
isLoading={isSubmitting} | ||
> | ||
Send Invoice | ||
</Button> | ||
</HStack> | ||
</Stack> | ||
</form> | ||
</Card> | ||
); | ||
}; | ||
|
||
export default EmailInvoice; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
4a0b352
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ecommerce-storefront-client – ./apps/storefront
hamropasal.vercel.app
ecommerce-storefront-client-sahrohit.vercel.app
www.rudejellyfish.live
ecommerce-storefront-client-git-main-sahrohit.vercel.app
rudejellyfish.live
4a0b352
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ecommerce-admin-client – ./apps/admin
ecommerce-admin-client-sahrohit.vercel.app
ecommerce-admin-client-git-main-sahrohit.vercel.app
ecommerce-admin-client.vercel.app