1+ import  React ,  {  useState  }  from  'react' ; 
2+ 
3+ import  {  View ,  Image ,  Text ,  Linking  }  from  'react-native' ; 
4+ 
5+ import  {  RectButton  }  from  'react-native-gesture-handler' ; 
6+ 
7+ import  heartOutlineIcon  from  '../../assets/images/icons/heart-outline.png' ; 
8+ 
9+ import  AsyncStorage  from  '@react-native-community/async-storage' ; 
10+ 
11+ import  unfavoriteIcon  from  '../../assets/images/icons/unfavorite.png' ; 
12+ 
13+ import  whatsappIcon  from  '../../assets/images/icons/whatsapp.png' ; 
14+ 
15+ import  styles  from  './styles' ; 
16+ import  Favorites  from  '../../pages/Favorites' ; 
17+ import  {  api  }  from  '../../services/api' ; 
18+ 
19+ export  interface  Teacher  { 
20+     id : number ; 
21+     avatar : string ; 
22+     bio : string ; 
23+     cost : number ; 
24+     name : string ; 
25+     subject : string ; 
26+     whatsapp : string ; 
27+ } 
28+ 
29+ interface  TeacherItemProps  { 
30+     teacher : Teacher ; 
31+     favorited : boolean ; 
32+ } 
33+ 
34+ 
35+ 
36+ const  TeatcherItem : React . FC < TeacherItemProps >  =  (  {  teacher,  favorited }  )  =>   { 
37+ 
38+     const  [ isFavorited ,  setIsFavorited ]  =  useState ( favorited ) ; 
39+ 
40+     // whatsapp deep-link 
41+     function  handleLinkToWhatsapp  ( )  { 
42+         
43+         api . post ( 'connections' ,  { 
44+             user_id : teacher . id , 
45+         } ) ; 
46+ 
47+         Linking . openURL ( `whatsapp://send?phone=${ teacher . whatsapp }  ) 
48+     } 
49+ 
50+     async  function  handleToggleFavorite ( )  { 
51+ 
52+         const  favorites  =  await  AsyncStorage . getItem ( 'favorites' ) ; 
53+ 
54+         let  favoritesArray  =  [ ] ; 
55+ 
56+         if  ( favorites )  { 
57+             favoritesArray  =  JSON . parse ( favorites ) ; 
58+         } 
59+ 
60+         if ( isFavorited )  { 
61+             // remover dos favoritos 
62+             const  favoriteIndex  =  favoritesArray . findIndex ( ( teacherItem : Teacher )  =>  { 
63+                 return  teacherItem . id  ===  teacher . id ; 
64+             } ) 
65+                
66+             // indidce, e quantas posições remover 
67+             favoritesArray . splice ( favoriteIndex  ,  1 ) ; 
68+ 
69+             setIsFavorited ( false ) ; 
70+         }  else  { 
71+             // adidionar aos favoritos             
72+             
73+ 
74+             favoritesArray . push ( teacher ) ; 
75+ 
76+             setIsFavorited ( true ) ; 
77+         } 
78+ 
79+         await  AsyncStorage . setItem ( 'favorites' ,  JSON . stringify ( favoritesArray ) ) ; 
80+     } 
81+ 
82+     return  ( 
83+         < View  style = { styles . container } > 
84+             < View  style = { styles . profile } > 
85+                 < Image  
86+                     style = { styles . avatar } 
87+                     source = { {  uri :  teacher . avatar   } } 
88+                 /> 
89+ 
90+                 < View  style = { styles . profileInfo } > 
91+                 < Text  style = { styles . name } > { teacher . name } </ Text > 
92+                 < Text  style = { styles . subject } > { teacher . subject } </ Text > 
93+                 </ View > 
94+             </ View > 
95+ 
96+             < Text  style = { styles . bio } > 
97+                 { teacher . bio } 
98+             </ Text > 
99+ 
100+             < View  style = { styles . footer } > 
101+                 < Text  style = { styles . price } > 
102+                     Preço/hora { '   ' } 
103+                     < Text  style = { styles . priceValue } > 
104+                         R$ { teacher . cost } 
105+                     </ Text > 
106+                 </ Text > 
107+ 
108+                 < View  style = { styles . buttonsContainer } > 
109+                     < RectButton  
110+                         onPress = { handleToggleFavorite } 
111+                         style = { [ 
112+                             styles . favoriteButton ,  
113+                             isFavorited  ? styles . favorited  : { } ] }  > 
114+                         {  isFavorited 
115+                              ? < Image  source = { unfavoriteIcon }  /> 
116+                              : < Image  source = { heartOutlineIcon }  /> } 
117+                     </ RectButton > 
118+ 
119+                     < RectButton  onPress = { handleLinkToWhatsapp }  style = { styles . contactButton }  > 
120+                         < Image  source = { whatsappIcon }  /> 
121+                         < Text  style = { styles . contactButtonText } > Entrar em contato</ Text > 
122+                     </ RectButton > 
123+                 </ View >         
124+             </ View > 
125+         </ View > 
126+     ) 
127+ } ; 
128+ 
129+ export  default  TeatcherItem ; 
0 commit comments