TrendingSection.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import { StyleSheet, Text, TouchableOpacity, View } from 'react-native';
  2. import React, { useContext } from 'react';
  3. import Carousel from 'react-native-reanimated-carousel';
  4. import MaterialComIcon from 'react-native-vector-icons/MaterialCommunityIcons';
  5. import { navigateToArticle, navigateToListViewPage, useConstructor } from '../../../constants/functions';
  6. import fonts from '../../../constants/fonts';
  7. import colors from '../../../constants/colors';
  8. import ImageBGCard from '../../molecules/Cards/ImageBGCard';
  9. import {
  10. moderateScale,
  11. screenHeight,
  12. screenWidth,
  13. } from '../../../constants/metrics';
  14. import SectionHeader from '../../molecules/Header/SectionHeader';
  15. import { ThemeContext } from '../../../context/theme.context';
  16. import { getTrendingNews } from '../../../api/data';
  17. import LoadingScreen from './LoadingScreen';
  18. import { PAGINATE_BY } from '../../../api/urls';
  19. const TrendingSection = props => {
  20. const ITEM_WIDTH = Math.round(screenWidth * 1);
  21. const ITEM_HEIGHT = Math.round(screenHeight * (1 / 3.25));
  22. const {
  23. navigation
  24. } = props
  25. const theme = useContext(ThemeContext);
  26. const currentTheme = theme.state.theme;
  27. const [currentCardIndex, setCurrentCardIndex] = React.useState(0);
  28. const updateCurrentIndex = index => {
  29. setCurrentCardIndex(index);
  30. console.log('current index:', index);
  31. };
  32. const [posts, setPosts] = React.useState([]);
  33. const [isLoading, setLoading] = React.useState(true);
  34. const fetchPosts = () => {
  35. getTrendingNews()
  36. .then(res => {
  37. console.log(res.data)
  38. setPosts(res.data.body.results.slice(0, PAGINATE_BY));
  39. setLoading(false)
  40. })
  41. .catch(error => console.log(error));
  42. };
  43. useConstructor(function () {
  44. fetchPosts();
  45. });
  46. const styles = StyleSheet.create({
  47. recentHeader: {
  48. paddingHorizontal: 16,
  49. },
  50. recentHeaderText: {
  51. fontFamily: fonts.type.semibold,
  52. fontSize: moderateScale(16),
  53. color: colors().recessive,
  54. },
  55. seeAllText: {
  56. fontFamily: fonts.type.semibold,
  57. color: colors().primaryColor,
  58. paddingRight: 16,
  59. alignSelf: 'stretch',
  60. },
  61. recentHeaderIcon: {},
  62. recentCardContainer: {
  63. alignItems: 'center',
  64. paddingHorizontal: 8,
  65. minWidth: screenHeight,
  66. width: '100%',
  67. backgroundColor: colors().dominant,
  68. },
  69. paginationContainer: {
  70. justifyContent: 'center',
  71. alignItems: 'center',
  72. paddingHorizontal: 16,
  73. paddingTop: 8,
  74. paddingBottom: 8,
  75. flexDirection: 'row',
  76. gap: 2,
  77. backgroundColor: colors().dominant,
  78. },
  79. });
  80. return (
  81. <View>
  82. <SectionHeader label={'Trending'} onPress={() => navigateToListViewPage(navigation, "normal", "Trending")} />
  83. <View style={styles.recentCardContainer}>
  84. {isLoading === true ? (
  85. <LoadingScreen containerHeight={ITEM_HEIGHT} containerWidth={ITEM_WIDTH} />
  86. ) : (
  87. <Carousel
  88. loop
  89. width={ITEM_WIDTH}
  90. height={ITEM_HEIGHT}
  91. style={styles.recentCardContainer}
  92. autoPlay={true}
  93. data={posts}
  94. scrollAnimationDuration={1500}
  95. mode="parallax"
  96. modeConfig={{
  97. parallaxScrollingScale: 1,
  98. parallaxScrollingOffset: 1,
  99. }}
  100. panGestureHandlerProps={{
  101. activeOffsetX: [0, 200],
  102. }}
  103. pagingEnabled
  104. onSnapToItem={index => updateCurrentIndex(index)}
  105. renderItem={({ index, item }) => (
  106. <View style={{ paddingHorizontal: 16 }}>
  107. <ImageBGCard
  108. image={{ uri: item.articles[0].cover_image }}
  109. author={item.articles[0].author}
  110. headline={item.articles[0].title}
  111. onPress={() => navigateToArticle(navigation, item.articles[0].id, item.articles[0].slug)}
  112. />
  113. </View>
  114. )}
  115. />
  116. )}
  117. </View>
  118. <View style={styles.paginationContainer}>
  119. {isLoading === true ? <></> : posts.map((item, index) => {
  120. return (
  121. <MaterialComIcon
  122. key={`dot_${index}`}
  123. name={'checkbox-blank-circle'}
  124. color={
  125. currentCardIndex === index
  126. ? colors().secondaryColor
  127. : colors().white_variant
  128. }
  129. size={8}
  130. />
  131. );
  132. })}
  133. </View>
  134. </View>
  135. );
  136. };
  137. export default TrendingSection;