| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- 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 (
- <SafeAreaView style={{ flex: 1, backgroundColor: "#fff" }}>
- <ScrollView style={{ padding: 20 }}>
- <View style={styles.wrapper}>
- <Text style={{ fontSize: 18 }}>Hello Diligent</Text>
- <TouchableOpacity onPress={() => navigation.openDrawer()}>
- <ImageBackground
- source={require("../assets/images/diligent-purple.png")}
- style={styles.imageBackground}
- imageStyle={{ borderRadius: 25 }}
- />
- </TouchableOpacity>
- </View>
- <View style={styles.search}>
- <Feather
- name="search"
- size={20}
- color="#C6C6C6"
- style={{ marginRight: 5 }}
- />
- <TextInput
- onChangeText={(text) => searchFilter(text)}
- autoCapitalize="none"
- autoCorrect={false}
- placeholder="Search"
- value={query}
- placeholderTextColor="#C6C6C6"
- />
- </View>
- <Text>
- {filteredData.length === 0 && (
- <View>
- <Text style={globalStyles.boldText}>No Results Found</Text>
- </View>
- )}
- </Text>
- {query.length === 0
- ? posts.map((post) => (
- <ListItem
- key={post.id}
- title={post.attributes.title}
- photo={post.attributes.profileImage.data.attributes.formats}
- publishedAt={post.attributes.publishedAt}
- onPress={() =>
- navigation.navigate("PostDetails", {
- title: post.attributes.title,
- id: post.id,
- })
- }
- />
- ))
- : filteredData.map((post) => (
- <ListItem
- key={post.id}
- title={post.attributes.title}
- photo={post.attributes.profileImage.data.attributes.formats}
- publishedAt={post.attributes.publishedAt}
- onPress={() =>
- navigation.navigate("PostDetails", {
- title: post.attributes.title,
- id: post.id,
- })
- }
- />
- ))}
- </ScrollView>
- </SafeAreaView>
- );
- };
-
- 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;
|