Documentation Index
Fetch the complete documentation index at: https://mintlify.com/anupom69/bun-hono-frontend-nextjs/llms.txt
Use this file to discover all available pages before exploring further.
Function Signature
async function deletePerson(
prevState: PersonPrevState,
formData: FormData
): Promise<PersonPrevState>
Source: src/lib/actions/people/deletePerson.ts:9
Parameters
Previous state object returned from the last invocation. Used by React’s useActionState hook.
Indicates if the previous operation was successful
Error message from the previous operation
Form data containing the person identifier
The unique identifier of the person to deleteValidation:
- Must be present (non-null, non-empty)
- Error: “ID is required”
Return Value
Set to true when the person is deleted successfully
Error message describing what went wrongPossible errors:
- “ID is required” - No ID provided in form data
- “Backend Error: ” - Backend API error
- Error message or “Something went wrong” - Network or unexpected errors
Validation
This function performs simple validation without Zod:
const id = formData.get("id");
if (!id) {
return {
error: "ID is required",
};
}
Behavior
- Validation: Checks that the ID field is present
- API Call: Makes a POST request to
{BACKEND_URL}/people/delete-one/{id}
- Authentication: Uses Basic Auth with environment credentials
- Revalidation: Calls
revalidatePath("/") on success to refresh the home page
- Error Handling: Returns structured error messages
Usage Example
import { useActionState } from "react";
import deletePerson from "@/lib/actions/people/deletePerson";
function DeletePersonButton({ personId }: { personId: string }) {
const [state, formAction] = useActionState(deletePerson, {});
return (
<form action={formAction}>
<input type="hidden" name="id" value={personId} />
<button type="submit" className="danger">Delete Person</button>
{state.error && <p className="error">{state.error}</p>}
{state.success && <p className="success">Person deleted!</p>}
</form>
);
}
Alternative Usage with Confirmation
function PersonList({ people }) {
const handleDelete = async (personId: string, name: string) => {
if (!confirm(`Are you sure you want to delete ${name}?`)) {
return;
}
const formData = new FormData();
formData.set("id", personId);
const result = await deletePerson({}, formData);
if (result.success) {
console.log("Person deleted");
} else {
alert(result.error);
}
};
return (
<ul>
{people.map((person) => (
<li key={person.id}>
{person.name} - {person.phone}
<button onClick={() => handleDelete(person.id, person.name)}>
Delete
</button>
</li>
))}
</ul>
);
}
Backend Endpoint
POST /people/delete-one/:id
Headers:
Authorization: Basic authentication
Body: None (ID is in the URL path)
Cascading Effects
Deleting a person may affect related data:
- Messages: Any scheduled messages associated with the deleted person’s phone number may become orphaned
- Data Integrity: Ensure your backend handles cascading deletes or orphaned references appropriately
Best Practices
- Confirmation: Always ask for user confirmation before deleting
- Soft Deletes: Consider implementing soft deletes on the backend to allow recovery
- Error Handling: Display clear error messages to users
- Audit Trail: Log deletion actions for compliance and debugging