File tree 2 files changed +44
-3
lines changed
app/(logged-in)/dashboard
2 files changed +44
-3
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change @@ -33,15 +33,19 @@ export default async function Dashboard() {
33
33
if ( user ) {
34
34
//update the user_id in users table
35
35
userId = clerkUser ?. id ;
36
+ if ( ! clerkUser ?. id ) {
37
+ console . error ( "User ID is undefined" ) ;
38
+ return redirect ( "/sign-in" ) ;
39
+ }
36
40
if ( userId ) {
37
41
await updateUser ( sql , userId , email ) ;
38
42
}
39
43
40
44
priceId = user [ 0 ] . price_id ;
41
45
}
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
+
45
49
// -----------------------------------------------------------
46
50
const isStarterPlan = planTypeId === "starter" ;
47
51
const isBasicPlan = planTypeId === "basic" ;
You can’t perform that action at this time.
0 commit comments