Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

HomeScreen.jsx 4.3KB

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