2020-08-15 17:04:43 -04:00
|
|
|
import React, {useState} from 'react';
|
2020-08-09 20:56:12 -04:00
|
|
|
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'
|
2020-08-13 18:55:10 -04:00
|
|
|
import { url } from './resources/fetchInfo.json'
|
2020-08-15 17:04:43 -04:00
|
|
|
import { SearchBar } from 'react-native-elements';
|
|
|
|
import Icon from 'react-native-vector-icons/AntDesign'
|
|
|
|
import AsyncStorage from '@react-native-community/async-storage'
|
|
|
|
|
|
|
|
const STORAGE_KEY = "teacherAnnouncements"
|
2020-08-11 12:35:10 -04:00
|
|
|
|
|
|
|
const Announcement = ({item}) => {
|
2020-08-13 18:55:10 -04:00
|
|
|
const date = new Date
|
2020-08-14 20:39:04 -04:00
|
|
|
const dateStr = `${date.getMonth()+1}/${date.getDate()}/${date.getFullYear()}`
|
2020-08-13 18:55:10 -04:00
|
|
|
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 (
|
2020-11-06 16:34:17 -05:00
|
|
|
<View style={styles.item1}>
|
2020-08-13 18:55:10 -04:00
|
|
|
{dateInfo!==undefined?<Text style={styles.date}>{dateInfo}</Text>:<></>}
|
2020-11-06 16:34:17 -05:00
|
|
|
<Text style={{fontSize:20}}>{item.item.message}</Text>
|
2020-08-11 12:35:10 -04:00
|
|
|
</View>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2020-08-14 20:03:40 -04:00
|
|
|
export const TeacherList = ({route}) => {
|
2020-08-15 17:04:43 -04:00
|
|
|
return (
|
|
|
|
<View style={styles.container}>
|
|
|
|
<FlatList
|
|
|
|
data={route.params.data}
|
|
|
|
renderItem={item=><Announcement item={item}/>}
|
|
|
|
keyExtractor={item=>JSON.stringify(item)}
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
)
|
2020-08-14 20:03:40 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
function TeacherButton(props) {
|
2020-11-06 16:34:17 -05:00
|
|
|
const [color, setColor] = useState(props.color?props.color:'lightgrey')
|
2020-08-14 20:03:40 -04:00
|
|
|
return (
|
2020-11-06 16:34:17 -05:00
|
|
|
<View style={[styles.item1,{flexDirection:'row'}]}>
|
2020-11-06 20:39:39 -05:00
|
|
|
<TouchableOpacity style={{flex:1}} onPress={()=>{props.navigation.navigate('TeacherList',{data:props.data,name:props.name})}} activeOpacity={0.8}>
|
2020-08-14 20:03:40 -04:00
|
|
|
<Text style={styles.title}>{props.name}</Text>
|
|
|
|
</TouchableOpacity>
|
2020-11-06 16:34:17 -05:00
|
|
|
{props.icon?<Icon.Button color={color} name="star" size={30} style={{alignSelf:'center'}} backgroundColor="white" onPress={()=>{setColor(color=='#dba309'?'lightgrey':'#dba309');props.addFavorite(props.name)}}/>:<></>}
|
2020-08-14 20:03:40 -04:00
|
|
|
</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: [],
|
2020-11-07 08:55:23 -05:00
|
|
|
favoriteNames: []
|
2020-08-11 12:35:10 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-15 17:04:43 -04:00
|
|
|
addFavorite = (name) => {
|
|
|
|
const favoriteNames = this.state.favoriteNames.slice().map(({name})=>name)
|
|
|
|
const index = favoriteNames.indexOf(name)
|
|
|
|
if (index < 0) {
|
|
|
|
favoriteNames.push(name)
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
favoriteNames.splice(index,1)
|
|
|
|
}
|
|
|
|
favoriteNames.sort()
|
|
|
|
this.setState({favoriteNames:favoriteNames.map(name=>({name:name}))})
|
|
|
|
AsyncStorage.setItem(STORAGE_KEY,JSON.stringify(favoriteNames)).catch(console.log).done()
|
|
|
|
}
|
|
|
|
|
2020-08-11 12:35:10 -04:00
|
|
|
componentDidMount() {
|
2020-11-06 20:39:39 -05:00
|
|
|
this.getData()
|
|
|
|
AsyncStorage.getItem(STORAGE_KEY)
|
|
|
|
.then(value=>value==null?[]:JSON.parse(value).map(x=>({name:x})))
|
|
|
|
.then(names=>this.setState({favoriteNames:names}))
|
|
|
|
.catch(console.log)
|
|
|
|
.done()
|
|
|
|
}
|
|
|
|
|
|
|
|
getData() {
|
2020-08-13 18:55:10 -04:00
|
|
|
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() {
|
|
|
|
return (
|
2020-08-15 17:04:43 -04:00
|
|
|
<View style={[styles.container]}>
|
|
|
|
<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-15 17:04:43 -04:00
|
|
|
data={this.state.favoriteNames.concat(this.state.teacherNames.filter(x=>this.state.favoriteNames.map(({name})=>name).indexOf(x.name) < 0))}
|
2020-11-06 16:34:17 -05:00
|
|
|
renderItem={({item})=><TeacherButton color={this.state.favoriteNames.indexOf(item) >= 0?'#dba309':'lightgrey'} item={item} data={this.state.data.filter(x=>x.teacher===item.name)} name={item.name} navigation={this.props.navigation} icon={true} addFavorite={this.addFavorite}/>}
|
2020-08-14 20:03:40 -04:00
|
|
|
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;
|