Skip to content

Commit

Permalink
refactor(core): Moved resend query to #/state
Browse files Browse the repository at this point in the history
  • Loading branch information
wootsbot committed Aug 21, 2024
1 parent 908293a commit b5d1066
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changeset/fresh-panthers-drive.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'x-boilerplate': patch
---

moved resend query to `#/state`
4 changes: 2 additions & 2 deletions src/app/(marketing)/resend/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as z from 'zod';
import { sendEmailSchema } from '@/utils/validations/send-email';

import Header from '@/components/header';
import { useResendEmail } from '@/hooks/services/resend/email/use-resend-email.hook';
import { useResendEmailMutation } from '#/state/queries/resend';

type FormValues = z.infer<typeof sendEmailSchema>;

Expand All @@ -29,7 +29,7 @@ function ResendPage() {
resolver: zodResolver(sendEmailSchema),
});

const { mutate: handleResendEmail, isPending } = useResendEmail({
const { mutate: handleResendEmail, isPending } = useResendEmailMutation({
onSuccess: (_data) => {
toast.success('Congratulations, you have successfully sent an email.');
reset();
Expand Down
4 changes: 2 additions & 2 deletions src/app/api/resend/email/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@ import InviteEmail from '~/app/(marketing)/resend/InviteEmail';

import { Resend } from 'resend';

import { emailSchema } from '@/hooks/services/resend/email/email.schema';
import { zSendEmailParams } from '#/state/queries/resend';

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

export async function POST(req: Request) {
try {
const json = await req.json();
const emailContent = emailSchema.parse(json);
const emailContent = zSendEmailParams().parse(json);

const { data } = await resend.emails.send({
from: process.env.RESEND_DOMAIN,
Expand Down
18 changes: 0 additions & 18 deletions src/hooks/services/resend/email/email.schema.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { useMutation, UseMutationOptions } from '@tanstack/react-query';

import { Email, EmailPayload, zEmailPayload } from './email.schema';
import { Email, EmailPayload, zEmailPayload } from './schema';

export const useResendEmail = (config: UseMutationOptions<EmailPayload, Error, Email> = {}) => {
export * from '#/state/queries/resend/schema';

export const useResendEmailMutation = (config: UseMutationOptions<EmailPayload, Error, Email> = {}) => {
return useMutation({
mutationFn: async ({ emailTo, subject, inviteLink }) => {
const response = await fetch('/api/resend/email', {
Expand All @@ -18,8 +20,5 @@ export const useResendEmail = (config: UseMutationOptions<EmailPayload, Error, E
return zEmailPayload().parse(json);
},
...config,
onSuccess: (data, ...args) => {
config?.onSuccess?.(data, ...args);
},
});
};
19 changes: 19 additions & 0 deletions src/state/queries/resend/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { z } from 'zod';

export const zSendEmailParams = () =>
z.object({
emailTo: z.string().email(),
subject: z.string(),
inviteLink: z.string(),
invitedByUsername: z.string().optional(),
});
export type SendEmailParams = z.infer<ReturnType<typeof zSendEmailParams>>;

export const zEmail = () => zSendEmailParams();
export type Email = z.infer<ReturnType<typeof zEmail>>;

export const zEmailPayload = () =>
z.object({
id: z.string().min(1),
});
export type EmailPayload = z.infer<ReturnType<typeof zEmailPayload>>;

0 comments on commit b5d1066

Please sign in to comment.