選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

HomeScreen.jsx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. <Text>
  84. {filteredData?.length === 0 && (
  85. <View style={{paddingHorizontal: 18}}>
  86. <Text
  87. style={[globalStyles.boldText, { color: colors.textPrimary }]}
  88. >
  89. {t('common.noResults')}
  90. </Text>
  91. </View>
  92. )}
  93. {!isLoading && (
  94. <ScrollView style={{ flex: 1, paddingHorizontal: 18 }}>
  95. {query.length === 0
  96. ? posts?.map((post) => (
  97. <ListItem
  98. key={post?.id}
  99. title={post?.attributes?.title}
  100. photo={
  101. post?.attributes?.profileImage?.data?.attributes?.formats
  102. }
  103. publishedAt={post?.attributes?.publishedAt}
  104. onPress={() =>
  105. navigation.navigate("PostDetails", {
  106. title: post?.attributes?.title,
  107. id: post?.id,
  108. })
  109. }
  110. />
  111. ))
  112. : filteredData.map((post) => (
  113. <ListItem
  114. key={post?.id}
  115. title={post?.attributes?.title}
  116. photo={
  117. post?.attributes?.profileImage?.data?.attributes?.formats
  118. }
  119. publishedAt={post?.attributes?.publishedAt}
  120. onPress={() =>
  121. navigation.navigate("PostDetails", {
  122. title: post?.attributes?.title,
  123. id: post?.id,
  124. })
  125. }
  126. />
  127. ))}
  128. </ScrollView>
  129. )}
  130. </Text>
  131. </Layout>
  132. );
  133. };
  134. const styles = StyleSheet.create({
  135. wrapper: {
  136. flexDirection: "row",
  137. justifyContent: "space-between",
  138. marginBottom: 20,
  139. padding: 18,
  140. },
  141. imageBackground: {
  142. width: 35,
  143. height: 35,
  144. },
  145. search: {
  146. flexDirection: "row",
  147. borderColor: "#C6C6C6",
  148. borderWidth: 1,
  149. borderRadius: 8,
  150. paddingHorizontal: 10,
  151. paddingVertical: 8,
  152. marginBottom: 20,
  153. marginHorizontal: 18,
  154. },
  155. });
  156. export default HomeScreen;