blazerapp/app/Announcements.js

101 lines
2.5 KiB
JavaScript
Raw Normal View History

2020-08-09 20:56:12 -04:00
import React from 'react';
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
2020-08-11 12:35:10 -04:00
FlatList,
2020-08-14 20:03:40 -04:00
TouchableOpacity,
2020-08-09 20:56:12 -04:00
} from 'react-native';
import {
Header,
LearnMoreLinks,
Colors,
DebugInstructions,
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
2020-08-11 12:35:10 -04:00
import styles from './styles/liststyles'
import { url } from './resources/fetchInfo.json'
2020-08-11 12:35:10 -04:00
const Announcement = ({item}) => {
const date = new Date
const dateStr = `${date.getMonth()+1}/${date.getUTCDate()}/${date.getFullYear()}`
const dateInfo = dateStr===item.item.date&&item.item.time!==undefined?item.item.time:item.item.date;
2020-08-11 12:35:10 -04:00
return (
<View style={styles.item}>
{dateInfo!==undefined?<Text style={styles.date}>{dateInfo}</Text>:<></>}
2020-08-11 12:35:10 -04:00
<Text style={styles.title}>{item.item.message}</Text>
</View>
)
}
2020-08-14 20:03:40 -04:00
export const TeacherList = ({route}) => {
return <View style={styles.container}>
<FlatList
data={route.params.data}
renderItem={item=><Announcement item={item}/>}
keyExtractor={item=>JSON.stringify(item)}
/>
</View>
}
function TeacherButton(props) {
return (
<View>
<TouchableOpacity style={styles.item} onPress={()=>props.navigation.navigate('TeacherList',{data:props.data,name:props.name})} activeOpacity={0.8}>
<Text style={styles.title}>{props.name}</Text>
</TouchableOpacity>
</View>
)
}
2020-08-09 20:56:12 -04:00
class Announcements extends React.Component {
2020-08-11 12:35:10 -04:00
constructor(props) {
super(props)
this.state = {
2020-08-14 20:03:40 -04:00
data: [],
teacherNames: [],
isLoading:true
2020-08-11 12:35:10 -04:00
}
}
componentDidMount() {
fetch(`${url}/api/en/announcements`,{
2020-08-11 12:35:10 -04:00
headers: {
'Cache-Control': 'no-cache'
}
}
)
.then((response) => {
2020-08-14 20:03:40 -04:00
return response.text()
2020-08-11 12:35:10 -04:00
})
2020-08-14 20:03:40 -04:00
.then((txt) => {
const data = JSON.parse(txt).data;
const teacherNames = [...new Set(data.filter(x=>x.teacher!=null&&x.teacher.trim()!=='').map(x=>x.teacher))];
teacherNames.sort()
this.setState({data: data, teacherNames: teacherNames.map(x=>({name:x})),isLoading:false});
2020-08-11 12:35:10 -04:00
})
.catch((error) => console.error(error))
}
2020-08-09 20:56:12 -04:00
render() {
2020-08-14 20:03:40 -04:00
if (this.state.isLoading) return <></>
2020-08-09 20:56:12 -04:00
return (
2020-08-11 12:35:10 -04:00
<View style={styles.container}>
2020-08-14 20:03:40 -04:00
<TeacherButton data={this.state.data.filter(x=>x.teacher==null||x.teacher.trim()==='')} name="No Teacher" navigation={this.props.navigation}/>
2020-08-11 12:35:10 -04:00
<FlatList
2020-08-14 20:03:40 -04:00
data={this.state.teacherNames}
renderItem={({item})=><TeacherButton item={item} data={this.state.data.filter(x=>x.teacher===item.name)} name={item.name} navigation={this.props.navigation}/>}
keyExtractor={(item,index)=>item.name+index}
2020-08-11 12:35:10 -04:00
/>
2020-08-09 20:56:12 -04:00
</View>
)
}
}
export default Announcements;