import React, { useContext, useEffect, useState } from "react"; import { View, Text, SafeAreaView, ScrollView, ImageBackground, TextInput, TouchableOpacity, StyleSheet, } from "react-native"; import Feather from "@expo/vector-icons/Feather"; import { postsApi } from "../service/post"; import { AuthContext } from "../context/AuthContext"; import ListItem from "../components/ListItem/ListItem"; import filter from "lodash.filter"; import { globalStyles } from "../styles/global"; const HomeScreen = ({ navigation }) => { const [posts, setPosts] = useState([]); const { userInfo } = useContext(AuthContext); const [query, setQuery] = useState(""); const [filteredData, setFilteredData] = useState(posts); const contains = (name, search) => { if (name.includes(search)) { return true; } return false; }; const searchFilter = (text) => { const formatedText = text.toLowerCase(); const data = filter(posts, (item) => { return contains(item?.attributes.title.toLowerCase(), formatedText); }); setFilteredData(data); setQuery(formatedText); }; const fetchAll = () => { if (userInfo.jwt) { postsApi(userInfo.jwt) .then((res) => setPosts(res.data)) .catch((e) => console.log(e)); } }; useEffect(() => { fetchAll(); }, []); useEffect(() => { if (posts) { setFilteredData(posts); } }, [posts]) return ( Hello Diligent navigation.openDrawer()}> searchFilter(text)} autoCapitalize="none" autoCorrect={false} placeholder="Search" value={query} placeholderTextColor="#C6C6C6" /> {filteredData.length === 0 && ( No Results Found )} {query.length === 0 ? posts.map((post) => ( navigation.navigate("PostDetails", { title: post.attributes.title, id: post.id, }) } /> )) : filteredData.map((post) => ( navigation.navigate("PostDetails", { title: post.attributes.title, id: post.id, }) } /> ))} ); }; const styles = StyleSheet.create({ wrapper: { flexDirection: "row", justifyContent: "space-between", marginBottom: 20, }, imageBackground: { width: 35, height: 35, }, search: { flexDirection: "row", borderColor: "#C6C6C6", borderWidth: 1, borderRadius: 8, paddingHorizontal: 10, paddingVertical: 8, marginBottom: 20, }, }); export default HomeScreen;