CategorySection.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import { StyleSheet, Text, View, ScrollView } from 'react-native'
  2. import React, { useEffect } from 'react'
  3. import fonts from '../../../theme/fonts'
  4. import colors from '../../../theme/colors'
  5. import { ToggleButton } from 'react-native-paper'
  6. import SectionHeader from '../Headers/SectionHeader'
  7. import HorizontalNewsCardVariant from '../Cards/HorizontalNewsCardVariant'
  8. import {CATEGORY_URL,ARTICLE_URL, PAGINATE_BY} from '../../../utils/Constants/constants'
  9. import { useConstructor } from '../../../utils/Constants/functions'
  10. // * API Handling
  11. const CategorySection = () => {
  12. const [news,setNews] = React.useState({'all':[]})
  13. const updateNewsByCategories = (key, value) => {
  14. setNews((prevObject) => ({
  15. ...prevObject,
  16. [key]: value,
  17. }));
  18. };
  19. const [categoryValue, setCategoryValue] = React.useState('');
  20. const [categories, setCategories] = React.useState([])
  21. const fetchCategories = () => {
  22. fetch(
  23. `http://www.newscout.in/api/v1/menus/?domain=newscout`)
  24. .then((res) => res.json())
  25. .then((json) => {
  26. let categoriesHeading = json.body.results.map((item) => item.heading.name)
  27. setCategories(categoriesHeading)
  28. setNews({})
  29. setNews(categoriesHeading.reduce((result, key) => ({ ...result, [key]: []}), {})),
  30. setCategoryValue(categoriesHeading[0])
  31. return categoriesHeading
  32. }).
  33. then((cats) => {
  34. cats.forEach(cat=> {
  35. fetchNews(cat)
  36. });
  37. return cats
  38. })
  39. .catch((err) => console.log(err))
  40. .finally(() => console.log("Fetch Categories Executed"))
  41. }
  42. const fetchNews = (category) => {
  43. fetch(`http://www.newscout.in/api/v1/article/search/?domain=newscout&category=${category}`)
  44. .then(res => res.json())
  45. .then((data) => {
  46. const newsDataFetch = data.body.results
  47. const finalNewsData = newsDataFetch.slice(0,PAGINATE_BY).map((article) => ({
  48. headline: article.title,
  49. image: {uri:article.cover_image},
  50. category: article.category,
  51. root_category: article.root_category,
  52. timestamp: article.published_on,
  53. tagline: "Bruh Momento Oi Lorem Ipsum di rubi rabbi",
  54. id:article.id
  55. }))
  56. updateNewsByCategories(category,finalNewsData)
  57. // categories.forEach((category) => {setNews((prev) => ({...prev,[category]:json.body.results}))})
  58. return finalNewsData
  59. })
  60. .catch((err) => console.log(err))
  61. .finally(() => console.log("Fetch Category News Executed"))
  62. }
  63. useConstructor( () => {
  64. const cat_data = fetchCategories()
  65. //console.log(`construct ${categories}`)
  66. // for (let category in cat_data) {
  67. // updateNewsByCategories(category,fetchNews(category))
  68. // }
  69. })
  70. // useEffect(() => {
  71. // for (let category in cat_data) {
  72. // fetchNews(category)
  73. // }
  74. // })
  75. return (
  76. <View style={styles.categoriesContainer}>
  77. <SectionHeader label={"Categories"} />
  78. <View style={styles.categoriesPillContainer}>
  79. <ScrollView horizontal showsHorizontalScrollIndicator={false}>
  80. <ToggleButton.Group onValueChange={value => { setCategoryValue(value) }}>
  81. {
  82. categories.map((category) =>
  83. <ToggleButton
  84. key={category}
  85. icon={() => <Text style={[category === categoryValue ? styles.selectedPillText : styles.pillText]}>{category}</Text>}
  86. style={[category === categoryValue ? styles.selectedContainer : styles.container, { marginHorizontal: fonts.getSize(4) }]}
  87. value={category}
  88. />)
  89. }
  90. </ToggleButton.Group>
  91. </ScrollView>
  92. </View>
  93. <ScrollView showsVerticalScrollIndicator={false}>
  94. <View style={styles.categoriesNewsContainer}>
  95. <Text>{console.log(news)}</Text>
  96. {news[categoryValue] !== undefined
  97. ? news[categoryValue].map((item) =>
  98. <HorizontalNewsCardVariant
  99. headline={item.headline}
  100. image={item.image}
  101. timestamp={item.timestamp}
  102. tagline={item.tagline}
  103. />)
  104. : <Text> News Not Present</Text>
  105. }
  106. </View>
  107. </ScrollView>
  108. </View>
  109. )
  110. }
  111. export default CategorySection
  112. const styles = StyleSheet.create({
  113. categoriesTitle: {
  114. flexDirection: 'row',
  115. paddingTop: fonts.getSize(20),
  116. paddingHorizontal: fonts.getSize(20)
  117. },
  118. categoriesTitleText: {
  119. flex: 1,
  120. fontFamily: fonts.type.semibold,
  121. fontSize: fonts.getSize(16),
  122. color: colors.black
  123. },
  124. categoriesPillContainer: {
  125. paddingBottom: fonts.getSize(8),
  126. flexDirection: 'row',
  127. alignItems: 'space-between',
  128. paddingHorizontal: fonts.getSize(16)
  129. },
  130. container: {
  131. borderRadius: fonts.getSize(18),
  132. paddingVertical: fonts.getSize(8),
  133. paddingHorizontal: fonts.getSize(16),
  134. backgroundColor: colors.white,
  135. width: 'auto'
  136. },
  137. selectedContainer: {
  138. paddingVertical: fonts.getSize(8),
  139. paddingHorizontal: fonts.getSize(16),
  140. backgroundColor: colors.topVariantColor,
  141. borderRadius: fonts.getSize(18),
  142. width: 'auto'
  143. },
  144. selectedPillText: {
  145. fontFamily: fonts.type.semibold,
  146. fontSize: fonts.getSize(12),
  147. color: colors.white
  148. },
  149. pillText: {
  150. fontFamily: fonts.type.semibold,
  151. fontSize: fonts.getSize(12),
  152. color: colors.quaternaryColor
  153. },
  154. categoriesNewsContainer: {
  155. paddingHorizontal: fonts.getSize(16),
  156. paddingTop: fonts.getSize(10),
  157. gap: 8
  158. }
  159. })