|
1 |
| -import { FlatList, View, Dimensions, ViewToken, RefreshControl } from "react-native"; |
| 1 | +import { FlatList, View, Dimensions, ViewToken, RefreshControl, TouchableOpacity, Text } from "react-native"; |
2 | 2 | import styles from "./styles";
|
3 | 3 | import PostSingle, { PostSingleHandles } from "../../components/general/post";
|
4 | 4 | import { useContext, useEffect, useRef, useState, useCallback } from "react";
|
5 |
| -import { getFeed, getPostsByUserId } from "../../services/posts"; |
| 5 | +import { getFeed, getFollowingFeed, getPostsByUserId } from "../../services/posts"; |
6 | 6 | import { Post } from "../../../types";
|
7 | 7 | import { RouteProp } from "@react-navigation/native";
|
8 | 8 | import { RootStackParamList } from "../../navigation/main";
|
@@ -41,17 +41,21 @@ export default function FeedScreen({ route }: { route: FeedScreenRouteProp }) {
|
41 | 41 |
|
42 | 42 | const [posts, setPosts] = useState<Post[]>([]);
|
43 | 43 | const [refreshing, setRefreshing] = useState(false);
|
| 44 | + const [selectedFeed, setSelectedFeed] = useState<"ForYou" | "Following">("ForYou"); |
44 | 45 | const mediaRefs = useRef<Record<string, PostSingleHandles | null>>({});
|
45 | 46 |
|
46 | 47 | const fetchPosts = useCallback(async () => {
|
47 | 48 | if (profile && creator) {
|
48 | 49 | const posts = await getPostsByUserId(creator);
|
49 | 50 | setPosts(posts);
|
50 |
| - } else { |
| 51 | + } else if (selectedFeed === "ForYou") { |
51 | 52 | const posts = await getFeed();
|
52 | 53 | setPosts(posts);
|
| 54 | + } else { |
| 55 | + const posts = await getFollowingFeed(); |
| 56 | + setPosts(posts); |
53 | 57 | }
|
54 |
| - }, [profile, creator]); |
| 58 | + }, [profile, creator, selectedFeed]); |
55 | 59 |
|
56 | 60 | useEffect(() => {
|
57 | 61 | fetchPosts();
|
@@ -114,6 +118,26 @@ export default function FeedScreen({ route }: { route: FeedScreenRouteProp }) {
|
114 | 118 |
|
115 | 119 | return (
|
116 | 120 | <View style={styles.container}>
|
| 121 | + <View style={styles.toggleContainer}> |
| 122 | + <TouchableOpacity |
| 123 | + style={[ |
| 124 | + styles.toggleButton, |
| 125 | + selectedFeed === "ForYou" && styles.selectedButton, |
| 126 | + ]} |
| 127 | + onPress={() => setSelectedFeed("ForYou")} |
| 128 | + > |
| 129 | + <Text style={styles.toggleButtonText}>For You</Text> |
| 130 | + </TouchableOpacity> |
| 131 | + <TouchableOpacity |
| 132 | + style={[ |
| 133 | + styles.toggleButton, |
| 134 | + selectedFeed === "Following" && styles.selectedButton, |
| 135 | + ]} |
| 136 | + onPress={() => setSelectedFeed("Following")} |
| 137 | + > |
| 138 | + <Text style={styles.toggleButtonText}>Following</Text> |
| 139 | + </TouchableOpacity> |
| 140 | + </View> |
117 | 141 | <FlatList
|
118 | 142 | data={posts}
|
119 | 143 | windowSize={4}
|
|
0 commit comments