|
| 1 | +//components |
| 2 | +import Post from "@components/containers/post"; |
| 3 | +import ProfileHeader from "@components/containers/ProfileHeader"; |
| 4 | +import CustomText from '@components/display/customText'; |
| 5 | +import NothingHere from "@components/display/nothing"; |
| 6 | +import RadioBtn from "@components/inputs/radioBtn"; |
| 7 | +import { FlatList, ListRenderItem, ListRenderItemInfo, RefreshControl, StyleSheet, View } from 'react-native'; |
| 8 | + |
| 9 | +//react |
| 10 | +import React, { useCallback, useEffect, useState } from "react"; |
| 11 | + |
| 12 | +//firestore |
| 13 | +import { auth, db } from "@auth/firebase"; |
| 14 | +import { collection, doc, getDoc, getDocs, limit, orderBy, query, where } from "firebase/firestore"; |
| 15 | + |
| 16 | +//typecasting |
| 17 | +import { UserData, post } from "@utils/types"; |
| 18 | +import { useLocalSearchParams } from 'expo-router'; |
| 19 | + |
| 20 | + |
| 21 | +export default function OtherProfileModal() { |
| 22 | + const { user_id } = useLocalSearchParams<{ user_id: string }>() |
| 23 | + |
| 24 | + const [refreshing, setRefreshing] = useState(false); |
| 25 | + const [currTab, setCurrTab] = useState("Logs"); |
| 26 | + |
| 27 | + const [ownPosts, setOwnPosts] = useState<post[]>([]); |
| 28 | + const [likedPosts, setUserOwnPosts] = useState<post[]>([]); |
| 29 | + const [userData, setUserData] = useState<UserData>(); |
| 30 | + const [sameUser, setSameUser] = useState(true); |
| 31 | + |
| 32 | + const currentUser = auth.currentUser; |
| 33 | + |
| 34 | + |
| 35 | + const [uid, setUid] = useState(currentUser ? (currentUser.uid) : ""); |
| 36 | + |
| 37 | + const onRefresh = React.useCallback(() => { |
| 38 | + setRefreshing(true); |
| 39 | + postWrapper(); |
| 40 | + setTimeout(() => { |
| 41 | + setRefreshing(false); |
| 42 | + }, 1000); |
| 43 | + }, []); |
| 44 | + |
| 45 | + //func |
| 46 | + const renderPost: ListRenderItem<post> = useCallback(({ item }: ListRenderItemInfo<post>) => |
| 47 | + ( |
| 48 | + <Post comment_count={item.num_comments} |
| 49 | + user_uid={currentUser ? currentUser.uid : ""} |
| 50 | + id={item.id} |
| 51 | + uid={item.uid} |
| 52 | + timestamp={item.timestamp} |
| 53 | + message={item.post_message} |
| 54 | + used_media={item.used_media} |
| 55 | + media={item.media} /> |
| 56 | + |
| 57 | + ), [currentUser]) |
| 58 | + |
| 59 | + async function showPosts(UID: string) { |
| 60 | + const c = collection(db, "posts"); |
| 61 | + const q = query(c, |
| 62 | + where("uid", "==", UID), |
| 63 | + orderBy("timestamp", "desc"), |
| 64 | + limit(5) |
| 65 | + ) |
| 66 | + const snap = await getDocs(q); |
| 67 | + |
| 68 | + return snap.docs.map(doc => ({ |
| 69 | + id: doc.id, |
| 70 | + ...(doc.data()) as Omit<post, "id"> |
| 71 | + })); |
| 72 | + } |
| 73 | + |
| 74 | + async function postWrapper() { |
| 75 | + if (!user_id) { |
| 76 | + const posts = await showPosts(uid); |
| 77 | + setOwnPosts(posts); |
| 78 | + |
| 79 | + const userSnap = await getDoc(doc(db, "users", uid)); |
| 80 | + setUserData({ |
| 81 | + uid: uid, |
| 82 | + ...(userSnap.data()) as Omit<UserData, "uid"> |
| 83 | + }); |
| 84 | + } else { |
| 85 | + const posts = await showPosts(user_id); |
| 86 | + setOwnPosts(posts); |
| 87 | + const userSnap = await getDoc(doc(db, "users", user_id)); |
| 88 | + setUserData({ |
| 89 | + uid: user_id, |
| 90 | + ...(userSnap.data()) as Omit<UserData, "uid"> |
| 91 | + }); |
| 92 | + |
| 93 | + } |
| 94 | + |
| 95 | + } |
| 96 | + |
| 97 | + //effects |
| 98 | + useEffect(() => { |
| 99 | + if (user_id) { |
| 100 | + if (user_id !== uid) { |
| 101 | + setSameUser(false); |
| 102 | + setUid(user_id); |
| 103 | + } |
| 104 | + } |
| 105 | + postWrapper(); |
| 106 | + }, []) |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + return ( |
| 111 | + |
| 112 | + <FlatList |
| 113 | + refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh} />} |
| 114 | + data={(currTab === "Logs") ? ownPosts : likedPosts} |
| 115 | + keyExtractor={item => item.id} |
| 116 | + renderItem={renderPost} |
| 117 | + removeClippedSubviews={true} |
| 118 | + ListEmptyComponent={ |
| 119 | + <NothingHere /> |
| 120 | + } |
| 121 | + ListHeaderComponent={ |
| 122 | + <View style={{ backgroundColor: "#17171d", flex: 1 }}> |
| 123 | + <ProfileHeader sameUser={sameUser} user_id={user_id} userData={userData} /> |
| 124 | + <RadioBtn |
| 125 | + options={["Logs", "Liked Logs"]} |
| 126 | + iconList={["post", "heart"]} |
| 127 | + selected={currTab} |
| 128 | + setSelected={setCurrTab} |
| 129 | + style={{ marginHorizontal: 10 }} |
| 130 | + /> |
| 131 | + |
| 132 | + <View style={{ backgroundColor: "#373d46ff", width: "100%", height: StyleSheet.hairlineWidth }} /> |
| 133 | + <CustomText |
| 134 | + style={{ |
| 135 | + color: "white", textAlign: "left", paddingLeft: 10, fontSize: 20, fontWeight: "bold", marginVertical: 20 |
| 136 | + }}> |
| 137 | + {(currTab === "Logs") ? (sameUser ? "Your Logs" : "Logs") : "Liked Logs"}</CustomText> |
| 138 | + </View> |
| 139 | + } |
| 140 | + ListFooterComponent={ |
| 141 | + <View style={{ paddingBottom: 100 }} /> |
| 142 | + } |
| 143 | + style={{ backgroundColor: "#17171d" }} |
| 144 | + /> |
| 145 | + ); |
| 146 | +} |
| 147 | + |
0 commit comments