fixing forms
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Vercel Production Deployment / Deploy-Production (push) Successful in 1m7s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Vercel Production Deployment / Deploy-Production (push) Successful in 1m7s
				
			This commit is contained in:
		
							parent
							
								
									c90eedcabd
								
							
						
					
					
						commit
						067c31d745
					
				@ -2,22 +2,16 @@
 | 
				
			|||||||
import { Input } from "~/components/ui/input";
 | 
					import { Input } from "~/components/ui/input";
 | 
				
			||||||
import { addCountry } from "~/server/actions/addCountry";
 | 
					import { addCountry } from "~/server/actions/addCountry";
 | 
				
			||||||
import SubmitButton from "../SubmitButton";
 | 
					import SubmitButton from "../SubmitButton";
 | 
				
			||||||
import { useRef } from "react";
 | 
					import { useFormState } from "react-dom";
 | 
				
			||||||
 | 
					 | 
				
			||||||
const initialState = {
 | 
					 | 
				
			||||||
  name: "" as string,
 | 
					 | 
				
			||||||
};
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
export default function CreateCountryForm() {
 | 
					export default function CreateCountryForm() {
 | 
				
			||||||
  const ref = useRef<HTMLFormElement>(null);
 | 
					  const [state, formAction] = useFormState(addCountry, null);
 | 
				
			||||||
  const handleSubmit = async (formData: FormData) => {
 | 
					
 | 
				
			||||||
    ref.current?.reset();
 | 
					 | 
				
			||||||
    await addCountry(formData);
 | 
					 | 
				
			||||||
  };
 | 
					 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <form ref={ref} action={handleSubmit}>
 | 
					    <form action={formAction}>
 | 
				
			||||||
      <Input name="name" required />
 | 
					      <Input name="name" />
 | 
				
			||||||
      <SubmitButton text={"Country"} />
 | 
					      <SubmitButton text={"Country"} />
 | 
				
			||||||
 | 
					      {state?.message}
 | 
				
			||||||
    </form>
 | 
					    </form>
 | 
				
			||||||
  );
 | 
					  );
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -1,7 +1,7 @@
 | 
				
			|||||||
"use client";
 | 
					"use client";
 | 
				
			||||||
import { Input } from "~/components/ui/input";
 | 
					import { Input } from "~/components/ui/input";
 | 
				
			||||||
import SubmitButton from "../SubmitButton";
 | 
					import SubmitButton from "../SubmitButton";
 | 
				
			||||||
import { useRef } from "react";
 | 
					import { useActionState } from "react";
 | 
				
			||||||
import { addRegion } from "~/server/actions/addRegion";
 | 
					import { addRegion } from "~/server/actions/addRegion";
 | 
				
			||||||
import {
 | 
					import {
 | 
				
			||||||
  Select,
 | 
					  Select,
 | 
				
			||||||
@ -20,10 +20,9 @@ interface Country {
 | 
				
			|||||||
export default function CreateRegionForm(props: { countries: Country[] }) {
 | 
					export default function CreateRegionForm(props: { countries: Country[] }) {
 | 
				
			||||||
  const [state, formAction] = useFormState(addRegion, null);
 | 
					  const [state, formAction] = useFormState(addRegion, null);
 | 
				
			||||||
  const { countries } = props;
 | 
					  const { countries } = props;
 | 
				
			||||||
  const ref = useRef<HTMLFormElement>(null);
 | 
					 | 
				
			||||||
  return (
 | 
					  return (
 | 
				
			||||||
    <form ref={ref} action={formAction}>
 | 
					    <form action={formAction}>
 | 
				
			||||||
      <Input name="name" required />
 | 
					      <Input name="name" />
 | 
				
			||||||
      <Select name="country">
 | 
					      <Select name="country">
 | 
				
			||||||
        <SelectTrigger className="w-[180px]">
 | 
					        <SelectTrigger className="w-[180px]">
 | 
				
			||||||
          <SelectValue placeholder="Country" />
 | 
					          <SelectValue placeholder="Country" />
 | 
				
			||||||
 | 
				
			|||||||
@ -3,15 +3,41 @@
 | 
				
			|||||||
import { revalidatePath } from "next/cache";
 | 
					import { revalidatePath } from "next/cache";
 | 
				
			||||||
import { db } from "../db";
 | 
					import { db } from "../db";
 | 
				
			||||||
import { countries } from "../db/schema";
 | 
					import { countries } from "../db/schema";
 | 
				
			||||||
 | 
					import * as z from "zod";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type NewCountry = {
 | 
					const schema = z.object({
 | 
				
			||||||
  name: string;
 | 
					  name: z.string().min(1, { message: "Name is required" }),
 | 
				
			||||||
};
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const addCountry = async (formData: FormData) => {
 | 
					export async function addCountry(prevstate: any, formData: FormData) {
 | 
				
			||||||
  const newCountry: NewCountry = {
 | 
					  const name = formData.get("name") as string;
 | 
				
			||||||
    name: (formData.get("name") as string).toLowerCase(),
 | 
					  const newCountry = schema.safeParse({ name: name.toLowerCase() });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (!newCountry.success) {
 | 
				
			||||||
 | 
					    const errors = newCountry.error.issues.map((issue) =>
 | 
				
			||||||
 | 
					      console.log(issue.message),
 | 
				
			||||||
 | 
					    );
 | 
				
			||||||
 | 
					    return {
 | 
				
			||||||
 | 
					      message: errors,
 | 
				
			||||||
 | 
					      data: newCountry.data,
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					  } else {
 | 
				
			||||||
 | 
					    const confirmCreate = await db
 | 
				
			||||||
 | 
					      .insert(countries)
 | 
				
			||||||
 | 
					      .values(newCountry.data)
 | 
				
			||||||
 | 
					      .onConflictDoNothing()
 | 
				
			||||||
 | 
					      .returning({ name: countries.name });
 | 
				
			||||||
 | 
					    if (!confirmCreate[0]) {
 | 
				
			||||||
 | 
					      return {
 | 
				
			||||||
 | 
					        message: `${newCountry.data.name} already exists`,
 | 
				
			||||||
 | 
					        data: newCountry.data,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
      revalidatePath("/");
 | 
					      revalidatePath("/");
 | 
				
			||||||
  await db.insert(countries).values(newCountry).returning();
 | 
					      return {
 | 
				
			||||||
};
 | 
					        message: `${confirmCreate[0].name} added`,
 | 
				
			||||||
 | 
					        data: newCountry.data,
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -11,15 +11,15 @@ const schema = z.object({
 | 
				
			|||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export const addRegion = async (prevstate: any, formData: FormData) => {
 | 
					export const addRegion = async (prevstate: any, formData: FormData) => {
 | 
				
			||||||
  const newRegion = schema.safeParse({
 | 
					  const regionData = {
 | 
				
			||||||
    name: (formData.get("name") as string).toLowerCase(),
 | 
					    name: (formData.get("name") as string).toLowerCase(),
 | 
				
			||||||
    countryId: formData.get("country") as string,
 | 
					    countryId: formData.get("country") as string,
 | 
				
			||||||
  });
 | 
					  };
 | 
				
			||||||
 | 
					  const newRegion = schema.safeParse(regionData);
 | 
				
			||||||
  if (!newRegion.success) {
 | 
					  if (!newRegion.success) {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      message: "failed",
 | 
					      message: newRegion.error.issues[0]?.message,
 | 
				
			||||||
      errors: newRegion.error,
 | 
					      data: newRegion.data,
 | 
				
			||||||
      data: null,
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
  const confirmedRegion = await db
 | 
					  const confirmedRegion = await db
 | 
				
			||||||
@ -27,11 +27,10 @@ export const addRegion = async (prevstate: any, formData: FormData) => {
 | 
				
			|||||||
    .values(newRegion.data)
 | 
					    .values(newRegion.data)
 | 
				
			||||||
    .onConflictDoNothing()
 | 
					    .onConflictDoNothing()
 | 
				
			||||||
    .returning({ name: regions.name });
 | 
					    .returning({ name: regions.name });
 | 
				
			||||||
  if (!confirmedRegion[0]?.name) {
 | 
					  if (!confirmedRegion[0]) {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
      message: "region already exists",
 | 
					      message: `${newRegion.data.name} already exists`,
 | 
				
			||||||
      errors: newRegion.error,
 | 
					      data: newRegion.data,
 | 
				
			||||||
      data: null,
 | 
					 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
  } else {
 | 
					  } else {
 | 
				
			||||||
    const message = `${newRegion.data.name} added`;
 | 
					    const message = `${newRegion.data.name} added`;
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user