RecentNewsSection.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { StyleSheet, Text, TouchableOpacity, View } from 'react-native'
  2. import React from 'react'
  3. import fonts from '../../../theme/fonts'
  4. import colors from '../../../theme/colors'
  5. import ImageBGCard from '../Cards/ImageBGCard'
  6. import images from '../../../assets/images/images'
  7. import SectionHeader from '../Headers/SectionHeader'
  8. import metrics from '../../../theme/metrics'
  9. import Carousel from 'react-native-reanimated-carousel';
  10. import MaterialComIcon from 'react-native-vector-icons/MaterialCommunityIcons'
  11. import { useConstructor } from '../../../utils/Constants/functions'
  12. // import {Q} from '@nozbe/watermelondb';
  13. // import withObservables from '@nozbe/with-observables';
  14. const ITEM_WIDTH = Math.round(metrics.screenWidth * 0.9);
  15. const ITEM_HEIGHT = Math.round(ITEM_WIDTH * 3 / 4);
  16. /*
  17. Enhanced News Card
  18. */
  19. // const EnhancedImageBGCard = withObservables(['recent_news'], ({news}) => ({
  20. // news: news.observe(),
  21. // }))(ImageBGCard);
  22. const RecentNewsSection = () => {
  23. const [currentCardIndex, setCurrentCardIndex] = React.useState(0)
  24. const updateCurrentIndex = (index) => {
  25. console.log('current index:', index)
  26. setCurrentCardIndex(index)
  27. }
  28. const [posts,setPosts] = React.useState([{},{},{},{},{}])
  29. const fetchPosts = () => {
  30. fetch("http://www.newscout.in/api/v1/article/search/?domain=newscout&category=Sector%20Updates")
  31. .then((res) => res.json())
  32. .then((json) => {
  33. const postData = json.body.results
  34. const finalPostData = postData.slice(0,5).map((item)=> ({
  35. image:item.cover_image,
  36. author: item.author,
  37. headline: item.title,
  38. id: item.id,
  39. }))
  40. setPosts(finalPostData)
  41. return finalPostData
  42. })
  43. .catch((err) => console.log(err))
  44. .finally(() => console.log("Fetch Posts Executed"))
  45. }
  46. useConstructor(function(){
  47. fetchPosts()
  48. })
  49. return (
  50. <View>
  51. <SectionHeader label={"Recent News"} />
  52. <View style={styles.recentCardContainer}>
  53. <Carousel
  54. loop
  55. width={metrics.screenWidth}
  56. height={ITEM_HEIGHT}
  57. style={styles.recentCardContainer}
  58. autoPlay={true}
  59. data={posts}
  60. scrollAnimationDuration={1000}
  61. mode="parallax"
  62. modeConfig={{
  63. parallaxScrollingScale: 1,
  64. parallaxScrollingOffset: 1,
  65. }}
  66. onSnapToItem={(index) => updateCurrentIndex(index)}
  67. renderItem={({index,item}) => (
  68. <View style={{ paddingHorizontal: 16 }}>
  69. <ImageBGCard
  70. image={{uri: item.image} ?? images.imageCard}
  71. author={ item.author ?? "Anonymous Author"}
  72. headline={item.headline ?? `Bruh Moment ${index}`}
  73. />
  74. </View>
  75. )}
  76. />
  77. </View>
  78. <View style={styles.paginationContainer}>
  79. {posts.map((item, index) => {
  80. return <MaterialComIcon key={`dot_${index}`} name={"checkbox-blank-circle"} color={currentCardIndex === index ? colors.tertiaryColor: colors.lightGray} size={12} />
  81. })}
  82. </View>
  83. </View>
  84. )
  85. }
  86. export default RecentNewsSection
  87. const styles = StyleSheet.create({
  88. recentHeader: {
  89. paddingHorizontal: 16,
  90. },
  91. recentHeaderText: {
  92. fontFamily: fonts.type.semibold,
  93. fontSize: fonts.getSize(16),
  94. color: colors.black
  95. },
  96. seeAllText: {
  97. fontFamily: fonts.type.semibold,
  98. color: colors.topColor,
  99. paddingRight: 16,
  100. alignSelf: 'stretch'
  101. },
  102. recentHeaderIcon: {
  103. },
  104. recentCardContainer: {
  105. alignItems: 'center',
  106. paddingHorizontal: 8,
  107. width: metrics.screenWidth
  108. },
  109. paginationContainer:{
  110. justifyContent:'flex-end',
  111. alignItems:'center',
  112. paddingHorizontal: 16,
  113. paddingTop:8,
  114. paddingBottom:4,
  115. flexDirection:'row',
  116. gap:2}
  117. })