43 lines
1.1 KiB
TypeScript
43 lines
1.1 KiB
TypeScript
"use server";
|
|
|
|
import { revalidatePath } from "next/cache";
|
|
import { sendFriendRequest, acceptFriend } from "@/utils/friends/friends";
|
|
import { UUID_REGEX } from "@/utils/validation";
|
|
|
|
export type AddFriendState = {
|
|
success?: boolean;
|
|
error?: string;
|
|
} | null;
|
|
|
|
export async function addFriendAction(
|
|
_prevState: AddFriendState,
|
|
formData: FormData,
|
|
): Promise<AddFriendState> {
|
|
const userId = formData.get("userId");
|
|
if (typeof userId !== "string" || !userId.trim()) {
|
|
return { error: "User ID is required." };
|
|
}
|
|
|
|
const trimmed = userId.trim();
|
|
|
|
if (!UUID_REGEX.test(trimmed)) {
|
|
return { error: "Please enter a valid user ID (UUID format)." };
|
|
}
|
|
|
|
try {
|
|
const result = await sendFriendRequest(trimmed);
|
|
if (result === false) {
|
|
return { error: "Could not send friend request. It may already exist." };
|
|
}
|
|
revalidatePath("/friends");
|
|
return { success: true };
|
|
} catch {
|
|
return { error: "Something went wrong. Please try again." };
|
|
}
|
|
}
|
|
|
|
export async function acceptRequestAction(senderId: string) {
|
|
await acceptFriend(senderId);
|
|
revalidatePath("/friends");
|
|
}
|