@@ -13,60 +13,46 @@ function Ad() {
1313  const  adsPath  = 
1414    `${ process . env . NEXT_PUBLIC_ADS_URL_FULL }  `  || 
1515    `${ process . env . NEXT_PUBLIC_ADS_ROUTE }  :${ process . env . NEXT_PUBLIC_ADS_PORT }  ` 
16-   const  [ codeFlag ,  setCodeFlag ]  =  useState < boolean > ( false ) 
1716
1817  const  getRandomArbitrary  =  useCallback ( ( min : number ,  max : number )  =>  { 
1918    return  Math . floor ( Math . random ( )  *  ( max  -  min )  +  min ) 
2019  } ,  [ ] ) 
2120
22-   const  fetchAd  =  useCallback ( 
23-     ( flag : boolean )  =>  { 
24-       const  headers  =  { 
25-         'X-Throw-Error' : `${ flag }  ` , 
26-         'X-Error-Rate' : process . env . NEXT_PUBLIC_ADS_ERROR_RATE  ||  '0.25' , 
27-       } 
28-       fetch ( `${ adsPath }  /ads` ,  {  headers } ) 
29-         . then ( ( res )  =>  { 
30-           if  ( ! res . ok )  { 
31-             throw  new  Error ( 'Error fetching ad' ) 
32-           } 
33-           return  res 
34-         } ) 
35-         . then ( ( res )  =>  res . json ( ) ) 
36-         . then ( ( data )  =>  { 
37-           const  index  =  getRandomArbitrary ( 0 ,  data . length ) 
38-           setData ( data [ index ] ) 
39-         } ) 
40-         . catch ( ( e )  =>  console . error ( e . message ) ) 
41-         . finally ( ( )  =>  { 
42-           setLoading ( false ) 
43-         } ) 
44-     } , 
45-     [ adsPath ,  getRandomArbitrary ,  setData ,  setLoading ] 
46-   ) 
47- 
48-   useEffect ( ( )  =>  { 
21+   const  fetchAd  =  useCallback ( async  ( )  =>  { 
4922    setLoading ( true ) 
50-     //  check for config file, then grab feature flags 
51-     if  ( config )  { 
52-       codeStash ( 'error-tracking' ,  {  file : config  } ) 
53-         . then ( ( r : boolean )  =>  { 
54-           setCodeFlag ( r ) 
55-         } ) 
56-         . catch ( ( e : Error )  =>  console . log ( e ) ) 
23+     const  flag  =  ( await  codeStash ( 'error-tracking' ,  {  file : config  } ) )  ||  false 
24+ 
25+     const  headers  =  { 
26+       'X-Throw-Error' : `${ flag }  ` , 
27+       'X-Error-Rate' : process . env . NEXT_PUBLIC_ADS_ERROR_RATE  ||  '0.25' , 
5728    } 
5829
59-     // Fetch ad with error 
60-     codeFlag  &&  fetchAd ( true ) 
30+     try  { 
31+       const  res  =  await  fetch ( `${ adsPath }  /ads` ,  {  headers } ) 
32+       if  ( ! res . ok )  { 
33+         throw  new  Error ( 'Error fetching ad' ) 
34+       } 
35+       const  data  =  await  res . json ( ) 
36+       console . log ( data ) 
37+       const  index  =  getRandomArbitrary ( 0 ,  data . length ) 
38+       setData ( data [ index ] ) 
39+       setLoading ( false ) 
40+     }  catch  ( e )  { 
41+       console . error ( e ) 
42+       setLoading ( false ) 
43+     } 
44+   } ,  [ adsPath ,  getRandomArbitrary ,  setData ,  setLoading ] ) 
6145
62-      // Fetch normal ad 
63-     ! codeFlag   &&   fetchAd ( false ) 
64-   } ,  [ codeFlag ,  fetchAd ] ) 
46+   useEffect ( ( )   =>   { 
47+     if   ( ! data )   fetchAd ( ) 
48+   } ,  [ data ,  fetchAd ] ) 
6549
6650  if  ( isLoading ) 
6751    return  < div  className = "flex flex-row justify-center h-10" > AD HERE</ div > 
6852  if  ( ! data ) 
69-     return  < div  className = "flex flex-row justify-center h-10" > AD HERE</ div > 
53+     return  ( 
54+       < div  className = "flex flex-row justify-center h-10" > AD DIDN'T LOAD</ div > 
55+     ) 
7056
7157  return  ( 
7258    < div  className = "flex flex-row justify-center py-4" > 
0 commit comments