import React, {useState} from 'react'; import { SafeAreaView, StyleSheet, ScrollView, View, Text, StatusBar, FlatList, TouchableOpacity, } from 'react-native'; import { Header, LearnMoreLinks, Colors, DebugInstructions, ReloadInstructions, } from 'react-native/Libraries/NewAppScreen'; import styles from './styles/liststyles' import { url } from './resources/fetchInfo.json' import { SearchBar } from 'react-native-elements'; import Icon from 'react-native-vector-icons/AntDesign' import Ionicons from 'react-native-vector-icons/Ionicons'; import AsyncStorage from '@react-native-async-storage/async-storage' import LinearGradient from 'react-native-linear-gradient'; import I18n from './i18n'; const STORAGE_KEY = "teacherAnnouncements" const getCurrentDate=()=>{ var date = new Date().getDate(); var month = new Date().getMonth(); var year = new Date().getFullYear(); return new Date(year, month, date); } const Announcement = ({item}) => { const todayDate = getCurrentDate() const itemDate = new Date(item.item.date) const dateInfo = todayDate.getTime()===itemDate.getTime()&&item.item.time!==undefined?item.item.time:(item.item.date+", " + item.item.time) return ( {item.item.message} {dateInfo!==undefined?{dateInfo}:<>} ) } function NewTeacherList(props) { return ( {I18n.t('dates.'+props.name)} } keyExtractor={item=>JSON.stringify(item)} /> ) } export const TeacherList = ({route}) => { const todayDate = getCurrentDate() const weekPastDate = new Date(); var pastDate = weekPastDate.getDate() - 7; weekPastDate.setDate(pastDate); const weekFutureDate = new Date(); var futureDate = weekFutureDate.getDate() + 7; weekFutureDate.setDate(futureDate); const today = [] const past = [] const future = [] var todayBoolean = true var pastBoolean = true var futureBoolean = true for (var i = 0; i < route.params.data.length; i++) { const itemDate = new Date(route.params.data[i].date) if (itemDate.getTime() == todayDate.getTime()) { today.push(route.params.data[i]) } else if (itemDate.getTime() > todayDate.getTime() && itemDate.getTime() <= weekFutureDate.getTime()) { future.push(route.params.data[i]) } //else if (itemDate >= weekPastDate && itemDate < todayDate) { else if (itemDate.getTime() < todayDate.getTime()) { past.push(route.params.data[i]) } } if (today.length === 0) todayBoolean = false if (past.length === 0) pastBoolean = false if (future.length === 0) futureBoolean = false var noAnn = (todayBoolean||pastBoolean||futureBoolean) return ( {todayBoolean?:<>} {pastBoolean?:<>} {futureBoolean?:<>} {!noAnn?{I18n.t('announcements.noAnnouncements')}:<>} ) } function TeacherButton(props) { const [color, setColor] = useState(props.color?props.color:'lightgrey') return ( {props.navigation.navigate('TeacherList',{data:props.data,name:props.name})}} activeOpacity={0.8}> {props.name} {props.icon?{setColor(color=='red'?'lightgrey':'red');props.addFavorite(props.name)}}/>:<>} ) } class Announcements extends React.Component { constructor(props) { super(props) this.state = { data: [], teacherNames: [], favoriteNames: [] } } 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() } componentDidMount() { 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() { fetch(`${url}/api/en/announcements`,{ headers: { 'Cache-Control': 'no-cache' } } ) .then((response) => { return response.text() }) .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}); }) .catch((error) => console.error(error)) } render() { return ( x.teacher==null||x.teacher.trim()==='')} name="No Teacher" navigation={this.props.navigation} /> this.state.favoriteNames.map(({name})=>name).indexOf(x.name) < 0))} renderItem={({item})=>= 0?'red':'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}/>} keyExtractor={(item,index)=>item.name+index} /> ) } } export default Announcements;