Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

HomeScreen.jsx 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. <View style={styles.wrapper}>
  56. <Text style={{ fontSize: 18, color: colors.textPrimary }}>
  57. {t("common.hello")}, Diligent
  58. </Text>
  59. <TouchableOpacity onPress={() => navigation.openDrawer()}>
  60. <ImageBackground
  61. source={require("../assets/images/diligent-purple.png")}
  62. style={styles.imageBackground}
  63. imageStyle={{ borderRadius: 25 }}
  64. />
  65. </TouchableOpacity>
  66. </View>
  67. <View style={styles.search}>
  68. <Feather
  69. name="search"
  70. size={20}
  71. color="#C6C6C6"
  72. style={{ marginRight: 5 }}
  73. />
  74. <TextInput
  75. onChangeText={(text) => searchFilter(text)}
  76. autoCapitalize="none"
  77. autoCorrect={false}
  78. placeholder={t('common.search')}
  79. value={query}
  80. placeholderTextColor="#C6C6C6"
  81. style={{ flex: 1, color: colors.textPrimary }}
  82. />
  83. </View>
  84. <Text>
  85. {filteredData.length === 0 && (
  86. <View>
  87. <Text
  88. style={[globalStyles.boldText, { color: colors.textPrimary }]}
  89. >
  90. {t('common.noResults')}
  91. </Text>
  92. </View>
  93. )}
  94. </Text>
  95. <ScrollView style={{flex: 1,paddingHorizontal: 18}}>
  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. padding: 18
  135. },
  136. imageBackground: {
  137. width: 35,
  138. height: 35,
  139. },
  140. search: {
  141. flexDirection: "row",
  142. borderColor: "#C6C6C6",
  143. borderWidth: 1,
  144. borderRadius: 8,
  145. paddingHorizontal: 10,
  146. paddingVertical: 8,
  147. marginBottom: 20,
  148. marginHorizontal: 18
  149. },
  150. });
  151. export default HomeScreen;