Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

HomeScreen.jsx 4.0KB

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