Skip to main content

Function Signature

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

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 to update

Return Value

PersonPrevState
object

Validation Schema

The function uses Zod for validation:
const personSchema = z.object({
  id: z.string().min(1, "ID is required"),
  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/update-one/{id}
  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 updatePerson from "@/lib/actions/people/updatePerson";

function EditPersonForm({ personId }: { personId: string }) {
  const [state, formAction] = useActionState(updatePerson, {});
  
  return (
    <form action={formAction}>
      <input type="hidden" name="id" value={personId} />
      <input name="name" placeholder="Full Name" required />
      <input name="phone" placeholder="8801XXXXXXXXX" required />
      <button type="submit">Update Person</button>
      {state.error && <p className="error">{state.error}</p>}
      {state.success && <p className="success">Person updated!</p>}
    </form>
  );
}

Backend Endpoint

POST /people/update-one/:id Headers:
  • Authorization: Basic authentication
  • Content-Type: application/json
Body:
{
  "id": "person-123",
  "name": "Jane Smith",
  "phone": "8801987654321"
}

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)

Differences from createPerson

  • ID Required: Must include the person ID to identify which record to update
  • Endpoint: Uses /update-one/:id instead of /create-one
  • Validation: Same validation rules as create, plus ID validation

Build docs developers (and LLMs) love