From 5fea32393829520315d820f66e5be198ccd4a07f Mon Sep 17 00:00:00 2001
From: Pattadon
Date: Fri, 11 Oct 2024 13:56:29 +0700
Subject: [PATCH] Refactor Apply page component to handle yes/no choices with
DualOptionSelector and Selector components, and update form field change
handling
---
src/app/business/apply/page.tsx | 66 +++++++++++++++++++++++++++++++--
1 file changed, 62 insertions(+), 4 deletions(-)
diff --git a/src/app/business/apply/page.tsx b/src/app/business/apply/page.tsx
index d2df985..9d2c243 100644
--- a/src/app/business/apply/page.tsx
+++ b/src/app/business/apply/page.tsx
@@ -33,7 +33,30 @@ export default function Apply() {
message: "Company name must be at least 5 characters.",
}),
industry: z.string(),
- isInUS: z.string(),
+ isInUS: z
+ .string({
+ required_error: "Please select either 'Yes' or 'No'.",
+ })
+ .transform((val) => val.toLowerCase())
+ .refine((val) => val === "yes" || val === "no", {
+ message: "Please select either 'Yes' or 'No'.",
+ }),
+ isForSale: z
+ .string({
+ required_error: "Please select either 'Yes' or 'No'.",
+ })
+ .transform((val) => val.toLowerCase())
+ .refine((val) => val === "yes" || val === "no", {
+ message: "Please select either 'Yes' or 'No'.",
+ }),
+ isGenerating: z
+ .string({
+ required_error: "Please select either 'Yes' or 'No'.",
+ })
+ .transform((val) => val.toLowerCase())
+ .refine((val) => val === "yes" || val === "no", {
+ message: "Please select either 'Yes' or 'No'.",
+ }),
totalRaised: z
.number({
required_error: "Total raised must be a number.",
@@ -90,7 +113,28 @@ export default function Apply() {
};
const onSubmit = (data: any) => {
- console.table(data);
+ // console.table(data);
+ // convert any yes and no to true or false
+ const transformedData = Object.entries(data).reduce(
+ (acc: Record, [key, value]) => {
+ if (typeof value === "string") {
+ const lowerValue = value.toLowerCase();
+ if (lowerValue === "yes") {
+ acc[key] = true;
+ } else if (lowerValue === "no") {
+ acc[key] = false;
+ } else {
+ acc[key] = value; // keep other string values unchanged
+ }
+ } else {
+ acc[key] = value; // keep other types unchanged
+ }
+ return acc;
+ },
+ {}
+ );
+
+ alert(JSON.stringify(transformedData));
};
const handleFieldChange = (fieldName: string, value: any) => {
switch (fieldName) {
@@ -264,6 +308,15 @@ export default function Apply() {
}
value={isInUS}
/>
+ {errors.isInUS && (
+
+ {errors.isInUS && (
+
+ {errors.isInUS.message as string}
+
+ )}
+
+ )}
{/* Is your product available (for sale) in market? */}
Your pitch deck and other application info will be used for{" "}