blazerapp/app/Staff.js

105 lines
2.7 KiB
JavaScript
Raw Normal View History

2020-08-18 21:29:44 -04:00
import React, { useState } from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
2020-09-12 19:22:17 -04:00
ActivityIndicator,
2020-08-18 21:29:44 -04:00
FlatList,
2020-10-20 23:12:33 -04:00
TouchableOpacity,
Image
2020-08-18 21:29:44 -04:00
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
2020-09-12 19:22:17 -04:00
import { SearchBar } from 'react-native-elements';
2020-08-18 21:29:44 -04:00
import styles from './styles/liststyles'
2020-09-12 19:22:17 -04:00
import { url } from './resources/fetchInfo.json'
2020-08-18 21:29:44 -04:00
2020-09-12 19:22:17 -04:00
const StaffElement = ({item}) => {
2020-08-18 21:29:44 -04:00
const [visible, setVisible] = useState(0)
2020-10-20 23:12:33 -04:00
const extra = [...item.item.emails.map(email=>(<Text key={email}>{'\n'}Email: {email}</Text>))]
2020-08-18 21:29:44 -04:00
return(
<View>
2020-10-20 23:12:33 -04:00
<TouchableOpacity style={styles.item1} onPress={()=>setVisible(!visible)} activeOpacity={0.8}>
<View style = {{display: 'flex', flexDirection: 'row', alignItems: 'center'}}>
<Image source = {require('./assets/clubs.png')} style = {{height: 40, width: 40, marginRight: 10}}/>
<Text style={styles.title}>{item.item.name}</Text>
</View>
{visible?extra:<></>}
2020-08-18 21:29:44 -04:00
</TouchableOpacity>
</View>
)
}
class Staff extends React.Component {
2020-09-12 19:22:17 -04:00
2020-08-18 21:29:44 -04:00
constructor(props) {
2020-09-12 19:22:17 -04:00
super(props);
this.state = {
data: [],
dataSearch:[],
isLoading: true,
search:""
};
}
componentDidMount() {
fetch(`${url}/api/en/teachers`,{
headers: {
'Cache-Control': 'no-cache'
} })
.then((response) => {
return response.text();
})
.then((json) => {
this.setState({data: JSON.parse(json).data});
this.setState({dataSearch:JSON.parse(json).data});
2020-09-12 19:22:17 -04:00
})
.catch((error) => console.error(error))
.finally(() => {
this.setState({ isLoading: false });
});
}
updateSearch = (search) => {
this.setState({ search:search });
const searchPool = search.startsWith(this.state.search)?this.state.dataSearch:this.state.data;
const ds = searchPool.filter((thing)=>{return thing.name.toLowerCase().split(' ').some(x=>x.startsWith(search.toLowerCase()))})
2020-09-12 19:22:17 -04:00
this.setState({dataSearch: ds})
};
clearSearch = (search)=>{
const ds = this.state.data;
this.setState({dataSearch:ds})
}
render() {
const { data , dataSearch, isLoading,search} = this.state;
return (
<SafeAreaView style={styles.container}>
<SearchBar
lightTheme
placeholder="Search Staff"
onChangeText={this.updateSearch}
onCancel={this.clearSearch}
onClear={this.clearSearch}
value={this.state.search}/>
<FlatList
data={dataSearch}
renderItem={item => <StaffElement item={item}/>}
keyExtractor={item => JSON.stringify(item)}
/>
</SafeAreaView>
);
}
2020-08-18 21:29:44 -04:00
}
export default Staff;