|
@@ -1,5 +1,5 @@
|
|
|
import { StyleSheet, Text, View, ScrollView } from 'react-native'
|
|
|
-import React from 'react'
|
|
|
+import React, { useEffect } from 'react'
|
|
|
import fonts from '../../../theme/fonts'
|
|
|
import colors from '../../../theme/colors'
|
|
|
import { ToggleButton } from 'react-native-paper'
|
|
@@ -22,9 +22,9 @@ const CategorySection = () => {
|
|
|
};
|
|
|
|
|
|
|
|
|
+
|
|
|
const [categoryValue, setCategoryValue] = React.useState('');
|
|
|
const [categories, setCategories] = React.useState([])
|
|
|
-
|
|
|
const fetchCategories = () => {
|
|
|
|
|
|
fetch(
|
|
@@ -33,11 +33,7 @@ const CategorySection = () => {
|
|
|
.then((json) => {
|
|
|
let categoriesHeading = json.body.results.map((item) => item.heading.name)
|
|
|
setCategories(categoriesHeading)
|
|
|
- setNews(categoriesHeading.reduce((result, item) => {
|
|
|
- result[item] = []
|
|
|
- return result
|
|
|
-
|
|
|
- },{}))
|
|
|
+ setNews(categoriesHeading.reduce((result, key) => ({ ...result, [key]: []}), {})),
|
|
|
setCategoryValue(categoriesHeading[0])
|
|
|
return categoriesHeading
|
|
|
})
|
|
@@ -45,33 +41,41 @@ const CategorySection = () => {
|
|
|
.finally(() => console.log("Fetch Categories Executed"))
|
|
|
}
|
|
|
|
|
|
+
|
|
|
+
|
|
|
const fetchNews = (category) => {
|
|
|
fetch(`http://www.newscout.in/api/v1/article/search/?domain=newscout&category=${category}`)
|
|
|
- .then((response) => response.json())
|
|
|
+ .then(res => res.json())
|
|
|
.then((json) => {
|
|
|
- updateNewsByCategories(category,json.body.results)
|
|
|
+ const newsDataFetch = json.body.results
|
|
|
+ const finalNewsData = newsDataFetch.map((article) => ({
|
|
|
+ headline: article.title,
|
|
|
+ image: 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))
|
|
|
// categories.forEach((category) => {setNews((prev) => ({...prev,[category]:json.body.results}))})
|
|
|
- return json.body.results
|
|
|
+ return newsDataFetch
|
|
|
})
|
|
|
.catch((err) => console.log(err))
|
|
|
- .finally(() => console.log("Fetch News Executed"))
|
|
|
- // .then((json) => console.log(`on func ${news[category]}\n${json.results}`))
|
|
|
- // .catch((err) => console.log(err))
|
|
|
- // .finally(() => console.log("Fetch News Executed"))
|
|
|
-
|
|
|
-
|
|
|
+ .finally(() => console.log("Fetch Category News Executed"))
|
|
|
}
|
|
|
|
|
|
|
|
|
useConstructor( () => {
|
|
|
- fetchCategories()
|
|
|
- console.log(`construct ${categories}`)
|
|
|
- for (let category in categories) {
|
|
|
+ const cat_data = fetchCategories()
|
|
|
+ // console.log(`construct ${categories}`)
|
|
|
+ for (let category in cat_data) {
|
|
|
fetchNews(category)
|
|
|
+
|
|
|
}
|
|
|
-
|
|
|
})
|
|
|
|
|
|
+
|
|
|
return (
|
|
|
<View style={styles.categoriesContainer}>
|
|
|
<SectionHeader label={"Categories"} />
|
|
@@ -93,8 +97,8 @@ const CategorySection = () => {
|
|
|
</View>
|
|
|
<ScrollView showsVerticalScrollIndicator={false}>
|
|
|
<View style={styles.categoriesNewsContainer}>
|
|
|
- <Text>{console.log(`on html ${news[categoryValue]}`)}</Text>
|
|
|
- {/* {news[categoryValue].map((item) => <HorizontalNewsCardVariant key={news.indexOf(item)} style={{ marginBottom: fonts.getSize(20) }} onPress={true} />) } */}
|
|
|
+ <Text>{console.log(news)}</Text>
|
|
|
+ {/* {news['Regional Updates'].map((item) => <HorizontalNewsCardVariant />)} */}
|
|
|
</View>
|
|
|
</ScrollView>
|
|
|
</View>
|