@@ -6,56 +6,106 @@ interface ReferralHandlerProps {
66 children ?: React . ReactNode ;
77}
88
9- export default function ReferralHandler ( { children } : ReferralHandlerProps ) {
10- useEffect ( ( ) => {
11- async function handleReferralCode ( ) {
12- try {
13- // Check for ref parameter in URL
14- const urlParams = new URLSearchParams ( window . location . search ) ;
15- const refCode = urlParams . get ( 'ref' ) ;
9+ async function storeReferralCodeFromUrl ( ) : Promise < void > {
10+ const urlParams = new URLSearchParams ( window . location . search ) ;
11+ const refCode = urlParams . get ( 'ref' ) ;
12+
13+ if ( refCode ) {
14+ localStorage . setItem ( 'ref_code' , refCode ) ;
15+ }
16+ }
1617
17- if ( refCode ) {
18- // Store referral code in localStorage
19- localStorage . setItem ( 'ref_code' , refCode ) ;
20- }
18+ async function checkIfUserAlreadyClaimed ( refCode : string , userId : string ) : Promise < boolean > {
19+ const checkUrl = `${ EXTERNAL . directus_url } /items/referral_code?filter[code][_eq]=${ encodeURIComponent ( refCode ) } &filter[user_claimed][directus_users_id][_eq]=${ userId } &limit=1` ;
20+
21+ const response = await fetch ( checkUrl , {
22+ credentials : 'include' ,
23+ headers : { 'Accept' : 'application/json' }
24+ } ) ;
2125
26+ if ( ! response . ok ) {
27+ throw new Error ( 'Failed to check existing claims' ) ;
28+ }
2229
23- // Check if user is logged in and handle claimed referral codes
24- const user = await getUserProfile ( EXTERNAL . directus_url ) ;
30+ const result = await response . json ( ) ;
31+ return result . data && result . data . length > 0 ;
32+ }
33+
34+ async function getReferralCodeDetails ( refCode : string ) : Promise < any > {
35+ const url = `${ EXTERNAL . directus_url } /items/referral_code?filter[code][_eq]=${ encodeURIComponent ( refCode ) } &limit=1` ;
36+
37+ const response = await fetch ( url , {
38+ credentials : 'include' ,
39+ headers : { 'Accept' : 'application/json' }
40+ } ) ;
41+
42+ if ( ! response . ok ) {
43+ throw new Error ( 'Failed to get referral code details' ) ;
44+ }
2545
26- if ( user ) {
27- const storedRefCode = localStorage . getItem ( 'ref_code' ) ;
46+ const result = await response . json ( ) ;
47+ return result . data && result . data . length > 0 ? result . data [ 0 ] : null ;
48+ }
2849
29- if ( storedRefCode ) {
30- console . log ( 'Referral code handling initiated.' ) ;
50+ async function claimReferralCode ( referralCodeId : string , userId : string ) : Promise < void > {
51+ const url = `${ EXTERNAL . directus_url } /items/referral_code_directus_users` ;
52+
53+ const response = await fetch ( url , {
54+ method : 'POST' ,
55+ credentials : 'include' ,
56+ headers : {
57+ 'Content-Type' : 'application/json' ,
58+ 'Accept' : 'application/json'
59+ } ,
60+ body : JSON . stringify ( {
61+ referral_code_id : referralCodeId ,
62+ directus_users_id : userId
63+ } )
64+ } ) ;
3165
32- // get data if currently passed ref code
33- const checkUrl = `${ EXTERNAL . directus_url } /items/referral_code?filter[code][_eq]=${ encodeURIComponent ( storedRefCode ) } &fields=*,user_claimed&limit=1` ;
66+ if ( ! response . ok ) {
67+ const errorData = await response . json ( ) . catch ( ( ) => null ) ;
68+ throw new Error ( `Failed to claim referral code: ${ response . status } ${ response . statusText } ${ JSON . stringify ( errorData ) } ` ) ;
69+ }
70+ }
3471
35- const response = await fetch ( checkUrl , {
36- credentials : 'include' ,
37- headers : { 'Accept' : 'application/json' }
38- } ) ;
72+ async function processReferralCode ( refCode : string , userId : string ) : Promise < void > {
73+ const alreadyClaimed = await checkIfUserAlreadyClaimed ( refCode , userId ) ;
74+
75+ if ( alreadyClaimed ) {
76+ console . log ( 'user already claimed ref code before' ) ;
77+ localStorage . removeItem ( 'ref_code' ) ;
78+ return ;
79+ }
3980
40- if ( response . ok ) {
41- const result = await response . json ( ) ;
42- console . log ( result ) ;
81+ const referralCode = await getReferralCodeDetails ( refCode ) ;
82+
83+ if ( ! referralCode ) {
84+ console . log ( 'referral code not found' ) ;
85+ localStorage . removeItem ( 'ref_code' ) ;
86+ return ;
87+ }
4388
44- // Check if the current user is in the user_claimed many-to-many relationship
45- if ( result . data && result . data . length > 0 ) {
46- const referralCode = result . data [ 0 ] ;
47- const isAlreadyClaimed = referralCode . user_claimed ?. some ( ( userId : string ) => userId === user . id ) ;
89+ await claimReferralCode ( referralCode . id , userId ) ;
90+ console . log ( 'User successfully added to referral code claims' ) ;
91+ localStorage . removeItem ( 'ref_code' ) ;
92+ }
93+
94+ export default function ReferralHandler ( { children } : ReferralHandlerProps ) {
95+ useEffect ( ( ) => {
96+ async function handleReferralCode ( ) {
97+ try {
98+ await storeReferralCodeFromUrl ( ) ;
99+
100+ const user = await getUserProfile ( EXTERNAL . directus_url ) ;
101+ if ( ! user ) return ;
48102
49- if ( isAlreadyClaimed ) {
50- console . log ( 'user already claimed ref code before' ) ;
103+ const storedRefCode = localStorage . getItem ( 'ref_code' ) ;
104+ if ( ! storedRefCode ) return ;
51105
52- localStorage . removeItem ( 'ref_code' ) ;
53- }
54- // TODO
55- }
56- }
57- }
58- }
106+ console . log ( 'Referral code handling initiated.' ) ;
107+ await processReferralCode ( storedRefCode , user . id ) ;
108+
59109 } catch ( error ) {
60110 console . error ( 'Error handling referral code:' , error ) ;
61111 }
@@ -64,6 +114,5 @@ export default function ReferralHandler({ children }: ReferralHandlerProps) {
64114 handleReferralCode ( ) ;
65115 } , [ ] ) ;
66116
67- // Just render children if provided, otherwise render nothing
68117 return children ? < > { children } </ > : null ;
69118}
0 commit comments