RecentPostsSection.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import { ScrollView, StyleSheet, Text, View } from 'react-native'
  2. import React from 'react'
  3. import SectionHeader from '../Headers/SectionHeader'
  4. import fonts from '../../../theme/fonts'
  5. import VerticalNewsCard from '../Cards/VerticalNewsCard'
  6. import { useConstructor } from '../../../utils/Constants/functions'
  7. const RecentPostsSection = () => {
  8. const [posts,setPosts] = React.useState([{},{},{},{},{}])
  9. const fetchPosts = () => {
  10. fetch("http://www.newscout.in/api/v1/article/search/?domain=newscout&category=Sector%20Updates")
  11. .then((res) => res.json())
  12. .then((json) => {
  13. const postsFetched = json.body.results
  14. const finalPosts = postsFetched.map((item,index) => ({
  15. key: index,
  16. id: item.id,
  17. image: {uri:item.cover_image},
  18. author: item.author,
  19. headline: item.title
  20. }))
  21. setPosts(finalPosts)
  22. return postsFetched
  23. })
  24. .then((data) => {
  25. console.log(data[0].keys())
  26. return data
  27. })
  28. .catch((err) => console.log(err))
  29. .finally(() => console.log("Fetch Posts Executed"))
  30. }
  31. useConstructor(function(){
  32. const fetchPost = fetchPosts();
  33. console.log(fetchPost)
  34. })
  35. return (
  36. <View style={styles.container}>
  37. <SectionHeader label={"Recents Posts"}/>
  38. <View style={styles.recentPostsContainer}>
  39. <ScrollView horizontal showsHorizontalScrollIndicator={false}>
  40. {posts.map((item) => <VerticalNewsCard
  41. key={item.key}
  42. id={item.id}
  43. image={item.image}
  44. author={item.author}
  45. headline={item.headline}
  46. />)}
  47. </ScrollView>
  48. </View>
  49. </View>
  50. )
  51. }
  52. export default RecentPostsSection
  53. const styles = StyleSheet.create({
  54. recentPostsContainer:{
  55. paddingHorizontal: fonts.getSize(16),
  56. gap: fonts.getSize(4),
  57. paddingBottom: fonts.getSize(16)
  58. }
  59. })