blazerapp/app/Clubs.js

102 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-08-10 15:25:00 -04:00
import React, { useState } from 'react';
2020-08-09 17:01:25 -04:00
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
2020-08-10 00:51:06 -04:00
ActivityIndicator,
FlatList,
2020-08-10 15:25:00 -04:00
TouchableOpacity
2020-08-09 17:01:25 -04:00
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
2020-08-10 09:14:23 -04:00
import { SearchBar } from 'react-native-elements';
2020-08-10 17:41:10 -04:00
import styles from './styles/liststyles'
import { url } from './resources/fetchInfo.json'
2020-08-09 17:01:25 -04:00
2020-08-10 00:51:06 -04:00
const ClubElement = ({item}) => {
2020-08-10 15:25:00 -04:00
const [visible, setVisible] = useState(0)
const extra = visible?(<Text>{'\n'}Location: {item.item.loc} {"\n\n"}Sponsor: {item.item.sponsor}</Text>):(<></>);
2020-08-10 00:51:06 -04:00
return(
2020-08-10 15:25:00 -04:00
<View>
2020-08-10 18:09:49 -04:00
<TouchableOpacity style={styles.item} onPress={()=>setVisible(!visible)} activeOpacity={0.8}>
2020-08-10 15:25:00 -04:00
<Text style={styles.title}>{item.item.name}</Text>
{extra}
</TouchableOpacity>
</View>
2020-08-10 00:51:06 -04:00
)
2020-08-09 17:01:25 -04:00
}
2020-08-10 00:51:06 -04:00
class Clubs extends React.Component {
2020-08-10 09:14:23 -04:00
2020-08-10 00:51:06 -04:00
constructor(props) {
super(props);
this.state = {
data: [],
2020-08-10 09:14:23 -04:00
dataSearch:[],
isLoading: true,
search:""
2020-08-10 00:51:06 -04:00
};
}
2020-08-09 17:01:25 -04:00
2020-08-10 00:51:06 -04:00
componentDidMount() {
2020-08-10 09:14:23 -04:00
fetch(`${url}/api/en/clubs`,{
2020-08-10 00:51:06 -04:00
headers: {
'Cache-Control': 'no-cache'
} })
.then((response) => {
return response.text();
})
.then((json) => {
2020-08-10 10:48:27 -04:00
this.setState({data: JSON.parse(json).clubs});
this.setState({dataSearch:JSON.parse(json).clubs });
2020-08-10 00:51:06 -04:00
})
.catch((error) => console.error(error))
.finally(() => {
this.setState({ isLoading: false });
});
2020-08-10 09:14:23 -04:00
}
updateSearch = (search) => {
this.setState({ search:search });
2020-08-10 15:25:00 -04:00
const searchPool = search.startsWith(this.state.search)?this.state.dataSearch:this.state.data;
const ds = searchPool.filter((thing)=>{return thing.name.toLowerCase().startsWith(search.toLowerCase())})
2020-08-10 09:14:23 -04:00
this.setState({dataSearch: ds})
};
clearSearch = (search)=>{
2020-08-10 15:25:00 -04:00
const ds = this.state.data;
2020-08-10 09:14:23 -04:00
this.setState({dataSearch:ds})
2020-08-10 00:51:06 -04:00
}
render() {
2020-08-10 09:14:23 -04:00
const { data , dataSearch, isLoading,search} = this.state;
2020-08-10 00:51:06 -04:00
return (
<SafeAreaView style={styles.container}>
2020-08-10 09:14:23 -04:00
<SearchBar
lightTheme
2020-08-10 15:25:00 -04:00
placeholder="Search Clubs"
2020-08-10 09:14:23 -04:00
onChangeText={this.updateSearch}
2020-08-10 09:21:05 -04:00
onCancel={this.clearSearch}
2020-08-10 09:14:23 -04:00
onClear={this.clearSearch}
value={this.state.search}/>
2020-08-10 00:51:06 -04:00
<FlatList
2020-08-10 09:14:23 -04:00
data={dataSearch}
2020-08-10 00:51:06 -04:00
renderItem={item => <ClubElement item={item}/>}
keyExtractor={item => JSON.stringify(item)}
/>
</SafeAreaView>
);
}
}
2020-08-10 17:41:10 -04:00
2020-08-09 17:01:25 -04:00
export default Clubs;