@@ -3,21 +3,11 @@ import MaterialDesignIcons from "@react-native-vector-icons/material-design-icon
33import { Image , Pressable , StyleSheet , View } from "react-native" ;
44import CarouselComponent from "../display/carousel" ;
55import CustomText from "../display/customText" ;
6- import InputBox from "../inputs/inptField" ;
76
87//firebase
9- import { db , getUserData } from "@auth/firebase" ;
8+ import { getUserData } from "@auth/firebase" ;
109import {
11- addDoc ,
12- collection ,
13- deleteDoc ,
14- doc ,
15- getDoc ,
16- increment ,
17- serverTimestamp ,
18- setDoc ,
19- Timestamp ,
20- updateDoc
10+ Timestamp
2111} from "firebase/firestore" ;
2212
2313//react and expo
@@ -26,6 +16,9 @@ import { useRouter } from 'expo-router';
2616import { memo , useEffect , useState } from "react" ;
2717
2818//func
19+ // import { checkUserLiked, dislikePost, likePost } from "@utils/otherUtils";
20+ import CommentBox from "@components/inputs/commentBox" ;
21+ import LikeButton from "@components/inputs/likeButton" ;
2922import { extractTime } from "@utils/stringTimeUtils" ;
3023
3124
@@ -44,12 +37,8 @@ interface Prop {
4437
4538const Post = memo ( function Post ( { id, user_uid, media, used_media, message, uid, timestamp, like_count, comment_count } : Prop ) {
4639 const router = useRouter ( ) ;
47- const [ liked , setLiked ] = useState ( false ) ;
48- const [ likeCount , setLikeCount ] = useState ( like_count ) ;
4940 const [ commentCount , setCommentCount ] = useState ( comment_count ) ;
5041
51-
52- const [ comment , setComment ] = useState ( "" ) ;
5342 const [ userPfp , setUserPfp ] = useState ( "https://i.pinimg.com/736x/15/0f/a8/150fa8800b0a0d5633abc1d1c4db3d87.jpg" ) ;
5443 const [ OPName , setOPName ] = useState ( "Your Name" ) ;
5544
@@ -62,78 +51,9 @@ const Post = memo(function Post({ id, user_uid, media, used_media, message, uid,
6251
6352 useEffect ( ( ) => {
6453 getOP ( ) ;
54+ console . log ( user_uid , message , "OP: " , uid ) ;
6555 } , [ uid ] )
6656
67- useEffect ( ( ) => {
68- checkIfUserLiked ( ) ;
69- console . log ( "rendering: " , id ) ;
70- } , [ ] )
71-
72-
73- async function checkIfUserLiked ( ) {
74- const likeRef = doc ( db , "posts" , id , "likes" , user_uid ) ;
75- try {
76-
77- const likeSnap = await getDoc ( likeRef ) ;
78- const liked = likeSnap . exists ( ) ;
79- setLiked ( liked ) ;
80- }
81- catch ( e ) {
82- console . log ( e , " there's an error..." ) ;
83- }
84- }
85-
86- async function addComment ( ) {
87- try {
88- await addDoc ( collection ( db , "posts" , id , "comments" ) , {
89- uid : user_uid ,
90- message : comment ,
91- timestamp : serverTimestamp ( ) ,
92- likes : 0
93- } )
94- await updateDoc ( doc ( db , "posts" , id ) ,
95- {
96- num_comments : increment ( 1 )
97- } )
98- setCommentCount ( commentCount + 1 ) ;
99- } catch ( e ) {
100- console . log ( e ) ;
101- }
102- }
103-
104- async function likePost ( ) {
105- try {
106- await setDoc ( doc ( db , "posts" , id , "likes" , user_uid ) ,
107- {
108- createdAt : new Date ( )
109- }
110- )
111- await updateDoc ( doc ( db , "posts" , id ) ,
112- {
113- likes : increment ( 1 )
114- } ) . then ( ( ) => {
115- setLikeCount ( likeCount + 1 ) ;
116- } )
117-
118- } catch ( e ) {
119- console . log ( e ) ;
120- }
121- }
122-
123- async function dislikePost ( ) {
124- try {
125- await deleteDoc ( doc ( db , "posts" , id , "likes" , user_uid ) ) ;
126- updateDoc ( doc ( db , "posts" , id ) ,
127- {
128- likes : increment ( - 1 )
129- } ) . then ( ( ) => {
130- setLikeCount ( likeCount - 1 ) ;
131- }
132- )
133- } catch ( e ) {
134- console . log ( e ) ;
135- }
136- }
13757
13858 async function getOP ( ) {
13959 await getUserData ( "users" , uid ) . then (
@@ -183,10 +103,7 @@ const Post = memo(function Post({ id, user_uid, media, used_media, message, uid,
183103
184104 { /* Buttons */ }
185105 < View style = { { flexDirection : "row" , paddingHorizontal : 20 , justifyContent : "flex-start" , alignItems : "center" , width : "auto" } } >
186- < Pressable style = { { padding : 8 , flexDirection : "row" } } onPress = { ( ) => { setLiked ( ! liked ) ; liked ? dislikePost ( ) : likePost ( ) } } >
187- < MaterialDesignIcons name = { liked ? "heart" : "heart-outline" } color = { liked ? "#ec3750" : "#5f6878" } size = { 25 } />
188- < CustomText style = { { color : "#8492a6" , marginLeft : 5 } } > { likeCount } </ CustomText >
189- </ Pressable >
106+ < LikeButton likeCount = { like_count } userId = { user_uid } postId = { id } />
190107
191108 < Pressable style = { { padding : 8 , flexDirection : "row" } } onPress = { ( ) => { router . push ( `/comments/${ id } ` ) } } >
192109 < MaterialDesignIcons name = "comment" color = "#5f6878" size = { 25 } />
@@ -197,15 +114,7 @@ const Post = memo(function Post({ id, user_uid, media, used_media, message, uid,
197114 < MaterialDesignIcons name = "share" color = "#5f6878" size = { 25 } />
198115 </ Pressable >
199116 </ View >
200-
201- { /* add a comment*/ }
202- < View style = { { flexDirection : "row" , alignItems : "center" , width : "100%" } } >
203- < InputBox secure = { false } value = { comment } valueFn = { setComment } color = "#8492a6" icon = "comment" type = "none" placeholder = "Comment here" />
204- < Pressable style = { { padding : 8 } } onPress = { addComment } >
205- < MaterialDesignIcons name = "send" color = "#5f6878" size = { 25 } />
206- </ Pressable >
207- </ View >
208-
117+ < CommentBox userId = { user_uid } postId = { id } />
209118 </ View >
210119 ) ;
211120} )
0 commit comments