File size: 851 Bytes
6076169
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
"use server";

import getDbConnection from "@/lib/db";
import { currentUser } from "@clerk/nextjs/server";
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";

export async function updatePostAction(data: {
	postId: string;
	content: string;
}) {
	const { postId, content } = data;

	const user = await currentUser();
	if (!user) {
		redirect("/sign-in");
	}

	try {
		const sql = await getDbConnection();

		const [title, ...contentParts] = content?.split("\n\n") || [];
		const updatedTitle = title.split("#")[1].trim();

		await sql`UPDATE posts SET content = ${content}, title = ${updatedTitle} where id = ${postId}`;
	} catch (error) {
		console.error("Error occurred in updating the post", postId);
		return {
			success: false,
		};
	}

	revalidatePath(`/posts/${postId}`);
	return {
		success: true,
	};
}