Function Signature
async function reschedule(): Promise<RescheduleState>
Source: src/lib/actions/reschedule/reschedule.ts:10
Parameters
This function takes no parameters. Unlike other server actions in this project, it doesn’t use the prevState and formData pattern.
Return Value
Set to true when the reschedule operation completes successfully
Set to false when an error occurs
Error message describing what went wrongPossible errors:
- Error message from caught exception
- “Unknown error” for non-Error exceptions
Behavior
- API Call: Makes a GET request to
{BACKEND_URL}/messages/reschedule
- Authentication: Uses Basic Auth with environment credentials
- Revalidation: Calls
revalidatePath("/") on success to refresh the home page
- Error Handling: Returns
success: false with optional error message
Usage Example
With Server Action
import reschedule from "@/lib/actions/reschedule/reschedule";
function RescheduleButton() {
const handleReschedule = async () => {
const result = await reschedule();
if (result.success) {
alert("Messages rescheduled successfully!");
} else {
alert(result.error || "Failed to reschedule messages");
}
};
return (
<button onClick={handleReschedule}>
Reschedule All Messages
</button>
);
}
With useTransition for Better UX
import { useTransition } from "react";
import reschedule from "@/lib/actions/reschedule/reschedule";
function RescheduleButton() {
const [isPending, startTransition] = useTransition();
const [result, setResult] = useState<RescheduleState | null>(null);
const handleReschedule = () => {
startTransition(async () => {
const res = await reschedule();
setResult(res);
});
};
return (
<div>
<button onClick={handleReschedule} disabled={isPending}>
{isPending ? "Rescheduling..." : "Reschedule All Messages"}
</button>
{result?.success && <p>Successfully rescheduled!</p>}
{result?.error && <p>Error: {result.error}</p>}
</div>
);
}
With Server Component
// app/actions.ts
"use server";
import reschedule from "@/lib/actions/reschedule/reschedule";
export async function handleReschedule() {
return await reschedule();
}
// app/reschedule-button.tsx
import { handleReschedule } from "./actions";
function RescheduleButton() {
return (
<form action={handleReschedule}>
<button type="submit">Reschedule All Messages</button>
</form>
);
}
Backend Endpoint
GET /messages/reschedule
Headers:
Authorization: Basic authentication
Body: None
Response: The function doesn’t parse the response body, only checks the status code.
What Does Rescheduling Do?
The reschedule operation typically:
- Recalculates send times for all pending messages
- Updates message schedules based on current date/time
- Handles messages that may have missed their send window
- Reorders the message queue
The exact behavior depends on your backend implementation. Check your backend API documentation for specific details.
Error Handling
The function catches all errors and returns a structured response:
try {
const response = await fetch(`${process.env.BACKEND_URL}/messages/reschedule`, {
method: "GET",
headers: {
Authorization: `Basic ${Buffer.from(
`${process.env.USERNAME}:${process.env.PASSWORD}`
).toString("base64")}`,
},
});
if (!response.ok) {
return { success: false };
}
revalidatePath("/");
return { success: true };
} catch (e) {
return {
success: false,
error: e instanceof Error ? e.message : "Unknown error"
};
}
Differences from Other Server Actions
- No Parameters: Doesn’t follow the
(prevState, formData) pattern
- GET Request: Uses GET instead of POST
- Simple Response: Only returns success/error, no complex state
- No Validation: No Zod schema or form data validation
- Direct Invocation: Can be called directly without form submission
Best Practices
- User Feedback: Provide clear feedback during the operation
- Loading States: Show loading indicator while rescheduling
- Error Messages: Display helpful error messages to users
- Confirmation: Consider asking for confirmation before rescheduling
- Permissions: Ensure only authorized users can trigger rescheduling