Skip to content

Commit

Permalink
Merge branch 'main' into jakubkarbowski/sc-25514/for-investigation-sp…
Browse files Browse the repository at this point in the history
…lit-nameguard-code-into
  • Loading branch information
djstrong authored Oct 21, 2024
2 parents d3907d2 + 9f15fdf commit a48997c
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 37 deletions.
2 changes: 1 addition & 1 deletion apps/namehashlabs.org/app/api/contact-form/route.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ContactFormDataProps,
validateContactFormData,
} from "@namehash/internal/src/contact-form";
} from "@namehash/internal";
import { host } from "@/lib/shared/origin";
import { type NextRequest, NextResponse } from "next/server";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import cc from "classcat";
import { CheckIcon, XCircleIcon } from "@heroicons/react/24/solid";
import * as Yup from "yup";
import { Button, Input, TextArea } from "@namehash/namekit-react";
import { contactFormSchema } from "./validation";
import { ContactFormDataProps } from "./types";

enum FormFields {
Name = "name",
Expand All @@ -14,25 +16,6 @@ enum FormFields {
Source = "source",
}

export interface ContactFormDataProps {
name: string;
email: string;
telegram: string;
message: string;
source: string;
}

const contactFormSchema = Yup.object().shape({
name: Yup.string().required("Name is required"),
email: Yup.string()
.email("Invalid email format")
.required("Email is required"),
telegram: Yup.string()
.matches(/^$|^[A-Za-z0-9_]+$/, "Invalid Telegram username")
.optional(),
message: Yup.string().required("Message is required"),
});

const validationErrorsInitialState = {
[FormFields.Name]: "",
[FormFields.Email]: "",
Expand All @@ -50,22 +33,10 @@ interface ContactUsFormProps {
submissionEndpoint: string;
}

// Validation function
export async function validateContactFormData(
data: ContactFormDataProps,
): Promise<Yup.ValidationError | null> {
try {
await contactFormSchema.validate(data, { abortEarly: false });
return null; // No errors, return null
} catch (error) {
if (error instanceof Yup.ValidationError) {
return error; // Return the validation error
}
throw error; // Rethrow other types of errors
}
}

export const ContactUsForm = ({ title, submissionEndpoint }: ContactUsFormProps) => {
export const ContactUsForm = ({
title,
submissionEndpoint,
}: ContactUsFormProps): JSX.Element => {
const [isLoading, setIsLoading] = useState(false);
const [errorMessage, setErrorMessage] = useState("");
const [successfulFormSubmit, setSuccessfulFormSubmit] = useState(false);
Expand Down
7 changes: 7 additions & 0 deletions internal/internal/src/contact-form/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
export interface ContactFormDataProps {
name: string;
email: string;
telegram: string;
message: string;
source: string;
}
28 changes: 28 additions & 0 deletions internal/internal/src/contact-form/validation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import * as Yup from "yup";
import { ContactFormDataProps } from "./types";

// Validation function
export async function validateContactFormData(
data: ContactFormDataProps,
): Promise<Yup.ValidationError | null> {
try {
await contactFormSchema.validate(data, { abortEarly: false });
return null; // No errors, return null
} catch (error) {
if (error instanceof Yup.ValidationError) {
return error; // Return the validation error
}
throw error; // Rethrow other types of errors
}
}

export const contactFormSchema = Yup.object().shape({
name: Yup.string().required("Name is required"),
email: Yup.string()
.email("Invalid email format")
.required("Email is required"),
telegram: Yup.string()
.matches(/^$|^[A-Za-z0-9_]+$/, "Invalid Telegram username")
.optional(),
message: Yup.string().required("Message is required"),
});
5 changes: 4 additions & 1 deletion internal/internal/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import "./styles.css";

export * as Metadata from "./metadata";
export { ContactUsForm } from "./contact-form";

export { ContactUsForm } from "./contact-form/contact-form";
export { validateContactFormData } from "./contact-form/validation";
export { type ContactFormDataProps } from "./contact-form/types";

0 comments on commit a48997c

Please sign in to comment.