Skip to content

Instantly share code, notes, and snippets.

@aleksejkozin
Last active January 13, 2021 17:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aleksejkozin/98ebb0c3fc826c23bbd9afc30cfda24f to your computer and use it in GitHub Desktop.
Save aleksejkozin/98ebb0c3fc826c23bbd9afc30cfda24f to your computer and use it in GitHub Desktop.
import React, {useEffect, useState} from 'react'
import {FlatList, StyleSheet, View} from 'react-native'
import AddModal from '../components/AddModal'
import LoadingIndicator from '../components/LoadingIndicator'
import BrowserItem from '../components/BrowserItem'
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
alignItems: 'center',
},
})
function useBrowsers(url) {
const [loading, setLoading] = useState(true)
const [browsers, setBrowsers] = useState([])
const [modalVisible, setModalVisible] = useState(false)
const [description, setDescription] = useState('')
const changeDescription = (description) => {
setDescription(description)
setModalVisible(!modalVisible)
}
const changeOpacity = () => {
setModalVisible(!modalVisible)
console.log('changeOpacity')
}
useEffect(() => {
fetch(URL)
.then((response) => response.json())
.then((responseJson) => {
return responseJson.Browsers
})
.then((browsers) => {
setBrowsers(browsers)
// console.log(browsers)
setLoading(false)
})
.catch((error) => {
console.log(error)
})
.finally(() => setLoading(false))
}, [])
return {
loading,
browsers,
modalVisible,
description,
changeDescription,
changeOpacity,
}
}
function BrowsersList({
loading,
browsers,
modalVisible,
description,
changeDescription,
changeOpacity,
}) {
return (
<View style={styles.container}>
{loading ? (
<LoadingIndicator />
) : (
<View>
<AddModal
modalVisible={modalVisible}
changeOpacity={() => changeOpacity()}
description={description}
/>
<FlatList
data={browsers}
keyExtractor={(browser) => browser.fullname}
renderItem={({item}) => (
<BrowserItem
fullname={item.fullname}
image={item.image}
linkToBrowser={item.linkToBrowser}
minMemory={item.minMemory}
currentVersion={item.currentVersion}
minimumRAM={item.minimumRAM}
description={item.description}
windows={item.windows}
mac={item.mac}
linux={item.linux}
ubuntu={item.ubuntu}
fedora={item.fedora}
stars={item.stars}
changeDescription={() => changeDescription(item.description)}
/>
)}
/>
</View>
)}
</View>
)
}
function Browsers() {
return <BrowsersList {...useBrowsers('https://google.com/myData.json')} />
}
export default Browsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment