Skip to content

Commit 1d8efc4

Browse files
committed
dashboard render fix1
1 parent 5c10f77 commit 1d8efc4

File tree

2 files changed

+44
-3
lines changed

2 files changed

+44
-3
lines changed

actions/edit-actions.ts

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"use server";
2+
3+
import getDbConnection from "@/lib/db";
4+
import { currentUser } from "@clerk/nextjs/server";
5+
import { revalidatePath } from "next/cache";
6+
import { redirect } from "next/navigation";
7+
8+
export async function updatePostAction(data: {
9+
postId: string;
10+
content: string;
11+
}) {
12+
const { postId, content } = data;
13+
14+
const user = await currentUser();
15+
if (!user) {
16+
redirect("/sign-in");
17+
}
18+
19+
try {
20+
const sql = await getDbConnection();
21+
22+
const [title, ...contentParts] = content?.split("\n\n") || [];
23+
const updatedTitle = title.split("#")[1].trim();
24+
25+
await sql`UPDATE posts SET content = ${content}, title = ${updatedTitle} where id = ${postId}`;
26+
} catch (error) {
27+
console.error("Error occurred in updating the post", postId);
28+
return {
29+
success: false,
30+
};
31+
}
32+
33+
revalidatePath(`/posts/${postId}`);
34+
return {
35+
success: true,
36+
};
37+
}

app/(logged-in)/dashboard/page.tsx

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ export default async function Dashboard() {
3333
if (user) {
3434
//update the user_id in users table
3535
userId = clerkUser?.id;
36+
if (!clerkUser?.id) {
37+
console.error("User ID is undefined");
38+
return redirect("/sign-in");
39+
}
3640
if (userId) {
3741
await updateUser(sql, userId, email);
3842
}
3943

4044
priceId = user[0].price_id;
4145
}
42-
43-
const { id: planTypeId = "starter", name: planTypeName } =
44-
getPlanType(priceId);
46+
const planType = getPlanType(priceId) || {};
47+
const { id: planTypeId = "starter", name: planTypeName = "Starter" } = planType;
48+
4549
// -----------------------------------------------------------
4650
const isStarterPlan=planTypeId==="starter";
4751
const isBasicPlan = planTypeId === "basic";

0 commit comments

Comments
 (0)