|
@@ -3,39 +3,100 @@ import React from 'react'
|
|
|
import fonts from '../../../theme/fonts'
|
|
|
import colors from '../../../theme/colors'
|
|
|
import { ToggleButton } from 'react-native-paper'
|
|
|
-import HorizontalNewsCard from '../Cards/HorizontalNewsCard'
|
|
|
import SectionHeader from '../Headers/SectionHeader'
|
|
|
+import HorizontalNewsCardVariant from '../Cards/HorizontalNewsCardVariant'
|
|
|
+import {CATEGORY_URL,ARTICLE_URL} from '../../../utils/Constants/urls'
|
|
|
+import { useConstructor } from '../../../utils/Constants/functions'
|
|
|
+
|
|
|
+
|
|
|
+// * API Handling
|
|
|
|
|
|
const CategorySection = () => {
|
|
|
- const categories = [ "Sector Update","Finance", "Sports", "Regional Update", "Entertainment"]
|
|
|
- const news = [{},{},{},{},{}]
|
|
|
- const [categoryValue, setCategoryValue] = React.useState(categories[0].toLowerCase());
|
|
|
+
|
|
|
+ const [news,setNews] = React.useState({})
|
|
|
+ const updateNewsByCategories = (key, value) => {
|
|
|
+ setNews((prevObject) => ({
|
|
|
+ ...prevObject,
|
|
|
+ [key]: value,
|
|
|
+ }));
|
|
|
+ };
|
|
|
+
|
|
|
+
|
|
|
+ const [categoryValue, setCategoryValue] = React.useState('');
|
|
|
+ const [categories, setCategories] = React.useState([])
|
|
|
+
|
|
|
+ const fetchCategories = () => {
|
|
|
+
|
|
|
+ fetch(
|
|
|
+ `http://www.newscout.in/api/v1/menus/?domain=newscout`)
|
|
|
+ .then((res) => res.json())
|
|
|
+ .then((json) => {
|
|
|
+ let categoriesHeading = json.body.results.map((item) => item.heading.name)
|
|
|
+ setCategories(categoriesHeading)
|
|
|
+ setNews(categoriesHeading.reduce((result, item) => {
|
|
|
+ result[item] = []
|
|
|
+ return result
|
|
|
+
|
|
|
+ },{}))
|
|
|
+ setCategoryValue(categoriesHeading[0])
|
|
|
+ return categoriesHeading
|
|
|
+ })
|
|
|
+ .catch((err) => console.log(err))
|
|
|
+ .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((json) => {
|
|
|
+ updateNewsByCategories(category,json.body.results)
|
|
|
+ // categories.forEach((category) => {setNews((prev) => ({...prev,[category]:json.body.results}))})
|
|
|
+ return json.body.results
|
|
|
+ })
|
|
|
+ .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"))
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
|
|
|
|
|
|
+ useConstructor( () => {
|
|
|
+ fetchCategories()
|
|
|
+ console.log(`construct ${categories}`)
|
|
|
+ for (let category in categories) {
|
|
|
+ fetchNews(category)
|
|
|
+ }
|
|
|
+
|
|
|
+ })
|
|
|
|
|
|
return (
|
|
|
<View style={styles.categoriesContainer}>
|
|
|
- <SectionHeader label={"Categories"}/>
|
|
|
+ <SectionHeader label={"Categories"} />
|
|
|
<View style={styles.categoriesPillContainer}>
|
|
|
<ScrollView horizontal showsHorizontalScrollIndicator={false}>
|
|
|
- <ToggleButton.Group onValueChange={value => {setCategoryValue(value) }}>
|
|
|
+ <ToggleButton.Group onValueChange={value => { setCategoryValue(value) }}>
|
|
|
{
|
|
|
- categories.map((category) =>
|
|
|
- <ToggleButton
|
|
|
- icon={() => <Text style={[category.toLowerCase() === categoryValue ? styles.selectedPillText: styles.pillText]}>{category}</Text>}
|
|
|
- style={[category.toLowerCase() === categoryValue ? styles.selectedContainer : styles.container, {marginHorizontal: fonts.getSize(4)}]}
|
|
|
- value={category.toLowerCase()}
|
|
|
-
|
|
|
- />)
|
|
|
+ categories.map((category) =>
|
|
|
+ <ToggleButton
|
|
|
+ key={category}
|
|
|
+ icon={() => <Text style={[category === categoryValue ? styles.selectedPillText : styles.pillText]}>{category}</Text>}
|
|
|
+ style={[category === categoryValue ? styles.selectedContainer : styles.container, { marginHorizontal: fonts.getSize(4) }]}
|
|
|
+ value={category}
|
|
|
+
|
|
|
+ />)
|
|
|
}
|
|
|
</ToggleButton.Group>
|
|
|
</ScrollView>
|
|
|
</View>
|
|
|
- <View style={styles.categoriesNewsContainer}>
|
|
|
- <ScrollView showsVerticalScrollIndicator={false}>
|
|
|
- {news.map((item) => <HorizontalNewsCard key={news.indexOf(item)} style={{marginBottom: fonts.getSize(20)}} onPress={true}/>)}
|
|
|
- </ScrollView>
|
|
|
- </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} />) } */}
|
|
|
+ </View>
|
|
|
+ </ScrollView>
|
|
|
</View>
|
|
|
|
|
|
)
|
|
@@ -85,9 +146,10 @@ const styles = StyleSheet.create({
|
|
|
fontSize: fonts.getSize(12),
|
|
|
color: colors.quaternaryColor
|
|
|
},
|
|
|
- categoriesNewsContainer:{
|
|
|
+ categoriesNewsContainer: {
|
|
|
paddingHorizontal: fonts.getSize(16),
|
|
|
paddingTop: fonts.getSize(10),
|
|
|
-
|
|
|
+ gap: 8
|
|
|
+
|
|
|
}
|
|
|
})
|