|
@@ -5,7 +5,7 @@ import colors from '../../../theme/colors'
|
|
|
import { ToggleButton } from 'react-native-paper'
|
|
|
import SectionHeader from '../Headers/SectionHeader'
|
|
|
import HorizontalNewsCardVariant from '../Cards/HorizontalNewsCardVariant'
|
|
|
-import {CATEGORY_URL,ARTICLE_URL} from '../../../utils/Constants/urls'
|
|
|
+import {CATEGORY_URL,ARTICLE_URL, PAGINATE_BY} from '../../../utils/Constants/constants'
|
|
|
import { useConstructor } from '../../../utils/Constants/functions'
|
|
|
|
|
|
|
|
@@ -13,7 +13,7 @@ import { useConstructor } from '../../../utils/Constants/functions'
|
|
|
|
|
|
const CategorySection = () => {
|
|
|
|
|
|
- const [news,setNews] = React.useState({})
|
|
|
+ const [news,setNews] = React.useState({'all':[]})
|
|
|
const updateNewsByCategories = (key, value) => {
|
|
|
setNews((prevObject) => ({
|
|
|
...prevObject,
|
|
@@ -33,9 +33,16 @@ const CategorySection = () => {
|
|
|
.then((json) => {
|
|
|
let categoriesHeading = json.body.results.map((item) => item.heading.name)
|
|
|
setCategories(categoriesHeading)
|
|
|
+ setNews({})
|
|
|
setNews(categoriesHeading.reduce((result, key) => ({ ...result, [key]: []}), {})),
|
|
|
setCategoryValue(categoriesHeading[0])
|
|
|
return categoriesHeading
|
|
|
+ }).
|
|
|
+ then((cats) => {
|
|
|
+ cats.forEach(cat=> {
|
|
|
+ fetchNews(cat)
|
|
|
+ });
|
|
|
+ return cats
|
|
|
})
|
|
|
.catch((err) => console.log(err))
|
|
|
.finally(() => console.log("Fetch Categories Executed"))
|
|
@@ -46,20 +53,20 @@ const CategorySection = () => {
|
|
|
const fetchNews = (category) => {
|
|
|
fetch(`http://www.newscout.in/api/v1/article/search/?domain=newscout&category=${category}`)
|
|
|
.then(res => res.json())
|
|
|
- .then((json) => {
|
|
|
- const newsDataFetch = json.body.results
|
|
|
- const finalNewsData = newsDataFetch.map((article) => ({
|
|
|
+ .then((data) => {
|
|
|
+ const newsDataFetch = data.body.results
|
|
|
+ const finalNewsData = newsDataFetch.slice(0,PAGINATE_BY).map((article) => ({
|
|
|
headline: article.title,
|
|
|
- image: article.cover_image,
|
|
|
+ image: {uri:article.cover_image},
|
|
|
category: article.category,
|
|
|
root_category: article.root_category,
|
|
|
timestamp: article.published_on,
|
|
|
tagline: "Bruh Momento Oi Lorem Ipsum di rubi rabbi",
|
|
|
id:article.id
|
|
|
- }),
|
|
|
- updateNewsByCategories(category,finalNewsData))
|
|
|
+ }))
|
|
|
+ updateNewsByCategories(category,finalNewsData)
|
|
|
// categories.forEach((category) => {setNews((prev) => ({...prev,[category]:json.body.results}))})
|
|
|
- return newsDataFetch
|
|
|
+ return finalNewsData
|
|
|
})
|
|
|
.catch((err) => console.log(err))
|
|
|
.finally(() => console.log("Fetch Category News Executed"))
|
|
@@ -68,13 +75,19 @@ const CategorySection = () => {
|
|
|
|
|
|
useConstructor( () => {
|
|
|
const cat_data = fetchCategories()
|
|
|
- // console.log(`construct ${categories}`)
|
|
|
- for (let category in cat_data) {
|
|
|
- fetchNews(category)
|
|
|
+ //console.log(`construct ${categories}`)
|
|
|
+ // for (let category in cat_data) {
|
|
|
+ // updateNewsByCategories(category,fetchNews(category))
|
|
|
|
|
|
- }
|
|
|
+ // }
|
|
|
})
|
|
|
|
|
|
+ // useEffect(() => {
|
|
|
+ // for (let category in cat_data) {
|
|
|
+ // fetchNews(category)
|
|
|
+
|
|
|
+ // }
|
|
|
+ // })
|
|
|
|
|
|
return (
|
|
|
<View style={styles.categoriesContainer}>
|
|
@@ -98,7 +111,16 @@ const CategorySection = () => {
|
|
|
<ScrollView showsVerticalScrollIndicator={false}>
|
|
|
<View style={styles.categoriesNewsContainer}>
|
|
|
<Text>{console.log(news)}</Text>
|
|
|
- {/* {news['Regional Updates'].map((item) => <HorizontalNewsCardVariant />)} */}
|
|
|
+ {news[categoryValue] !== undefined
|
|
|
+ ? news[categoryValue].map((item) =>
|
|
|
+ <HorizontalNewsCardVariant
|
|
|
+ headline={item.headline}
|
|
|
+ image={item.image}
|
|
|
+ timestamp={item.timestamp}
|
|
|
+ tagline={item.tagline}
|
|
|
+ />)
|
|
|
+ : <Text> News Not Present</Text>
|
|
|
+ }
|
|
|
</View>
|
|
|
</ScrollView>
|
|
|
</View>
|