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.

HomeScreen.jsx 4.6KB

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