41 lines
1.1 KiB
TypeScript
41 lines
1.1 KiB
TypeScript
"use server";
|
|
|
|
import { getServerSession } from "next-auth";
|
|
import { revalidatePath } from "next/cache";
|
|
import { authOptions } from "@/lib/auth";
|
|
import { profileService } from "@/server/services/profile.service";
|
|
import { updateProfileSchema } from "./schemas";
|
|
|
|
export async function updateProfileAction(formData: FormData) {
|
|
const session = await getServerSession(authOptions);
|
|
if (!session?.user) {
|
|
return { error: "Kamu harus login terlebih dahulu" };
|
|
}
|
|
|
|
const interests = formData
|
|
.getAll("interests")
|
|
.map((v) => (v as string).trim())
|
|
.filter(Boolean);
|
|
|
|
const raw = {
|
|
bio: formData.get("bio"),
|
|
city: formData.get("city"),
|
|
instagram: formData.get("instagram"),
|
|
interests,
|
|
};
|
|
|
|
const parsed = updateProfileSchema.safeParse(raw);
|
|
if (!parsed.success) {
|
|
return { error: parsed.error.issues[0].message };
|
|
}
|
|
|
|
try {
|
|
await profileService.updateProfile(session.user.id, parsed.data);
|
|
revalidatePath("/profile");
|
|
revalidatePath(`/u/${session.user.id}`);
|
|
return { success: true };
|
|
} catch (err) {
|
|
return { error: (err as Error).message };
|
|
}
|
|
}
|