Skip to main content

Function Signature

async function createPerson(
  prevState: PersonPrevState,
  formData: FormData
): Promise<PersonPrevState>
Source: src/lib/actions/people/createPerson.ts:18

Parameters

prevState
PersonPrevState
required
Previous state object returned from the last invocation. Used by React’s useActionState hook.
formData
FormData
required
Form data containing the person details

Return Value

PersonPrevState
object

Validation Schema

The function uses Zod for validation:
const personSchema = z.object({
  name: z.string().min(1, "Name cannot be empty"),
  phone: z
    .string()
    .regex(/^8801\d{9}$/, "Invalid phone number format. Must be 8801XXXXXXXXX"),
});

Behavior

  1. Validation: Form data is validated against the Zod schema
  2. API Call: Makes a POST request to {BACKEND_URL}/people/create-one
  3. Authentication: Uses Basic Auth with environment credentials
  4. Revalidation: Calls revalidatePath("/") on success to refresh the home page
  5. Error Handling: Returns structured error messages for validation, backend, or network errors

Usage Example

import { useActionState } from "react";
import createPerson from "@/lib/actions/people/createPerson";

function PersonForm() {
  const [state, formAction] = useActionState(createPerson, {});
  
  return (
    <form action={formAction}>
      <input name="name" placeholder="Full Name" required />
      <input name="phone" placeholder="8801XXXXXXXXX" required />
      <button type="submit">Create Person</button>
      {state.error && <p className="error">{state.error}</p>}
      {state.success && <p className="success">Person created!</p>}
    </form>
  );
}

Backend Endpoint

POST /people/create-one Headers:
  • Authorization: Basic authentication
  • Content-Type: application/json
Body:
{
  "name": "John Doe",
  "phone": "8801234567890"
}

Phone Number Format

The phone number must follow Bangladesh’s mobile number format:
  • Prefix: 8801 (country code 880 + mobile prefix 1)
  • Digits: Exactly 9 additional digits
  • Total Length: 13 characters
  • Example: 8801712345678

Valid Examples

  • 8801712345678 (Grameenphone)
  • 8801812345678 (Robi)
  • 8801912345678 (Banglalink)
  • 8801612345678 (Airtel)
  • 8801512345678 (Teletalk)

Invalid Examples

  • 01712345678 (missing country code)
  • +8801712345678 (includes + symbol)
  • 88017123456 (too few digits)
  • 880171234567890 (too many digits)

Build docs developers (and LLMs) love