You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import React, { useEffect, useState } from "react";
  2. import {
  3. View,
  4. Text,
  5. SafeAreaView,
  6. ScrollView,
  7. ImageBackground,
  8. TextInput,
  9. TouchableOpacity,
  10. StyleSheet,
  11. } from "react-native";
  12. import Feather from "@expo/vector-icons/Feather";
  13. import ListItem from "../components/ListItem/ListItem";
  14. import filter from "lodash.filter";
  15. import { globalStyles } from "../styles/global";
  16. import { getRequest } from "../request";
  17. const HomeScreen = ({ navigation }) => {
  18. const [posts, setPosts] = useState([]);
  19. const [query, setQuery] = useState("");
  20. const [filteredData, setFilteredData] = useState(posts);
  21. const contains = (name, search) => {
  22. if (name.includes(search)) {
  23. return true;
  24. }
  25. return false;
  26. };
  27. const searchFilter = (text) => {
  28. const formatedText = text.toLowerCase();
  29. const data = filter(posts, (item) => {
  30. return contains(item?.attributes.title.toLowerCase(), formatedText);
  31. });
  32. setFilteredData(data);
  33. setQuery(formatedText);
  34. };
  35. const fetchAll = async () => {
  36. const { data } = await getRequest("api/posts?populate=*");
  37. if (data.data) {
  38. setPosts(data.data);
  39. }
  40. };
  41. useEffect(() => {
  42. fetchAll();
  43. }, []);
  44. useEffect(() => {
  45. if (posts) {
  46. setFilteredData(posts);
  47. }
  48. }, [posts]);
  49. return (
  50. <SafeAreaView style={{ flex: 1, backgroundColor: "#fff" }}>
  51. <ScrollView style={{ padding: 20 }}>
  52. <View style={styles.wrapper}>
  53. <Text style={{ fontSize: 18 }}>Hello Diligent</Text>
  54. <TouchableOpacity onPress={() => navigation.openDrawer()}>
  55. <ImageBackground
  56. source={require("../assets/images/diligent-purple.png")}
  57. style={styles.imageBackground}
  58. imageStyle={{ borderRadius: 25 }}
  59. />
  60. </TouchableOpacity>
  61. </View>
  62. <View style={styles.search}>
  63. <Feather
  64. name="search"
  65. size={20}
  66. color="#C6C6C6"
  67. style={{ marginRight: 5 }}
  68. />
  69. <TextInput
  70. onChangeText={(text) => searchFilter(text)}
  71. autoCapitalize="none"
  72. autoCorrect={false}
  73. placeholder="Search"
  74. value={query}
  75. placeholderTextColor="#C6C6C6"
  76. />
  77. </View>
  78. <Text>
  79. {filteredData.length === 0 && (
  80. <View>
  81. <Text style={globalStyles.boldText}>No Results Found</Text>
  82. </View>
  83. )}
  84. </Text>
  85. {query.length === 0
  86. ? posts.map((post) => (
  87. <ListItem
  88. key={post.id}
  89. title={post.attributes.title}
  90. photo={post.attributes.profileImage.data.attributes.formats}
  91. publishedAt={post.attributes.publishedAt}
  92. onPress={() =>
  93. navigation.navigate("PostDetails", {
  94. title: post.attributes.title,
  95. id: post.id,
  96. })
  97. }
  98. />
  99. ))
  100. : filteredData.map((post) => (
  101. <ListItem
  102. key={post.id}
  103. title={post.attributes.title}
  104. photo={post.attributes.profileImage.data.attributes.formats}
  105. publishedAt={post.attributes.publishedAt}
  106. onPress={() =>
  107. navigation.navigate("PostDetails", {
  108. title: post.attributes.title,
  109. id: post.id,
  110. })
  111. }
  112. />
  113. ))}
  114. </ScrollView>
  115. </SafeAreaView>
  116. );
  117. };
  118. const styles = StyleSheet.create({
  119. wrapper: {
  120. flexDirection: "row",
  121. justifyContent: "space-between",
  122. marginBottom: 20,
  123. },
  124. imageBackground: {
  125. width: 35,
  126. height: 35,
  127. },
  128. search: {
  129. flexDirection: "row",
  130. borderColor: "#C6C6C6",
  131. borderWidth: 1,
  132. borderRadius: 8,
  133. paddingHorizontal: 10,
  134. paddingVertical: 8,
  135. marginBottom: 20,
  136. },
  137. });
  138. export default HomeScreen;