22
33import { useSubscription } from "@/hooks/useSubscription" ;
44import { useRouter } from "next/navigation" ;
5- import { useEffect } from "react" ;
5+ import { useEffect , useState } from "react" ;
6+ import { useSession } from "next-auth/react" ;
7+ import type { Session } from "next-auth" ;
68
79export default function ProDashboardPage ( ) {
810 const { isPaidUser, isLoading } = useSubscription ( ) ;
911 const router = useRouter ( ) ;
12+ const { data : session } = useSession ( ) ;
13+ const [ error , setError ] = useState < string | null > ( null ) ;
14+ const [ isJoining , setIsJoining ] = useState ( false ) ;
1015
1116 useEffect ( ( ) => {
1217 if ( ! isLoading && ! isPaidUser ) {
1318 router . push ( "/pricing" ) ;
1419 }
1520 } , [ isPaidUser , isLoading , router ] ) ;
1621
22+ const handleJoinSlack = async ( ) => {
23+ if ( isJoining ) return ;
24+ setIsJoining ( true ) ;
25+ setError ( null ) ;
26+
27+ if ( ! session ?. user ) {
28+ setError ( "Please sign in to join the community" ) ;
29+ setIsJoining ( false ) ;
30+ return ;
31+ }
32+
33+ const accessToken = ( session as Session ) ?. accessToken ;
34+
35+ if ( ! accessToken ) {
36+ setError ( "Authentication token not found" ) ;
37+ setIsJoining ( false ) ;
38+ return ;
39+ }
40+
41+ try {
42+ const apiUrl = process . env . NEXT_PUBLIC_API_URL || "http://localhost:4000" ;
43+ const response = await fetch ( `${ apiUrl } /join-community` , {
44+ method : "GET" ,
45+ headers : {
46+ Authorization : `Bearer ${ accessToken } ` ,
47+ } ,
48+ } ) ;
49+
50+ if ( ! response . ok ) {
51+ const errorData = await response . json ( ) ;
52+ setError ( errorData . error || "Failed to join community" ) ;
53+ setIsJoining ( false ) ;
54+ return ;
55+ }
56+
57+ const { slackInviteUrl } = await response . json ( ) ;
58+ window . location . href = slackInviteUrl ;
59+ } catch ( err ) {
60+ console . error ( "Failed to join community:" , err ) ;
61+ setError ( "Failed to connect to server" ) ;
62+ setIsJoining ( false ) ;
63+ }
64+ } ;
65+
1766 if ( isLoading ) {
1867 return (
1968 < div className = "w-full h-full flex items-center justify-center bg-ox-content" >
@@ -33,6 +82,20 @@ export default function ProDashboardPage() {
3382 hi investors, ajeetunc is on the way to deliver the shareholder value.
3483 soon you'll see all the pro perks here. thanks for investing
3584 </ h1 >
85+ { isPaidUser && (
86+ < div className = "mt-6" >
87+ < button
88+ onClick = { handleJoinSlack }
89+ disabled = { isJoining }
90+ className = "px-4 py-2 bg-brand-purple hover:bg-brand-purple-light text-text-primary font-medium rounded-lg transition-colors duration-200 disabled:opacity-50 disabled:cursor-not-allowed text-sm"
91+ >
92+ { isJoining ? "Joining..." : "Join Slack" }
93+ </ button >
94+ { error && (
95+ < p className = "text-error-text text-sm mt-2" > { error } </ p >
96+ ) }
97+ </ div >
98+ ) }
3699 </ div >
37100 </ div >
38101 ) ;
0 commit comments