blazerapp/app/Calendar.js

140 lines
4.0 KiB
JavaScript
Raw Normal View History

import React, {useState} from 'react';
2020-08-09 17:01:25 -04:00
import {
SafeAreaView,
StyleSheet,
ScrollView,
View,
Text,
StatusBar,
FlatList,
2020-12-07 01:07:07 -05:00
TouchableOpacity,
Image,
2020-08-09 17:01:25 -04:00
} from 'react-native';
2020-12-07 01:07:07 -05:00
import {
2020-08-09 17:01:25 -04:00
ReloadInstructions,
} from 'react-native/Libraries/NewAppScreen';
2020-12-07 01:07:07 -05:00
import LinearGradient from 'react-native-linear-gradient';
import styles from './styles/liststyles'
import { url } from './resources/fetchInfo.json'
2020-12-07 01:07:07 -05:00
const getCurrentDate=()=>{
var date = new Date().getDate();
var month = new Date().getMonth() + 1;
var year = new Date().getFullYear();
return year + '-' + month + '-' + date;
}
const Event = ({item}) => {
const [visible, setVisible] = useState(false)
2020-12-07 01:07:07 -05:00
const date = item.item.date.split('-')
const today = new Date(getCurrentDate())
2020-12-21 02:01:08 -05:00
const week = new Date().setDate(new Date().getDate() - 8)
2020-12-07 01:07:07 -05:00
const itemDate = new Date(item.item.date)
const extra = (
<>
<Text style={{fontSize:20}}>{item.item.text}</Text>
<Text style={{fontSize:20}}>Location: {item.item.location}</Text>
</>
2020-12-07 01:07:07 -05:00
)
if (itemDate >= today) {
return (
<TouchableOpacity style={styles.item1} onPress={()=>setVisible(!visible)} activeOpacity={0.8}>
2020-12-07 01:07:48 -05:00
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center', justifyContent: 'space-between'}}>
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center', width: '60%'}}>
2020-12-07 01:07:07 -05:00
<Image source ={require('./assets/calendar.png')} style = {{height: 40, width: 40, marginRight: 15}}/>
<Text style={styles.title3}>{item.item.title}</Text>
</View>
2020-12-07 01:07:48 -05:00
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center'}}>
<Text style = {{fontSize: 16, alignSelf: 'center'}}>{`${date[1]}/${date[2]}/${date[0]}`}</Text>
</View>
</View>
2020-12-07 01:07:07 -05:00
{visible?extra:<></>}
</TouchableOpacity>
)
}
2020-12-21 02:01:08 -05:00
else if (itemDate >= week){
2020-12-07 01:07:07 -05:00
return (
<TouchableOpacity style={{backgroundColor: '#e3e3e3', padding: 15, borderBottomWidth: 1, borderColor: 'black', width: '100%',}} onPress={()=>setVisible(!visible)} activeOpacity={0.8}>
2020-12-07 01:07:48 -05:00
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center', justifyContent: 'space-between'}}>
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center', width: '60%'}}>
<Image source ={require('./assets/calendar.png')} style = {{height: 40, width: 40, marginRight: 15}}/>
<Text style={styles.title3}>{item.item.title}</Text>
</View>
<View style = {{display: 'flex', flexDirection: 'row', alignContent: 'center'}}>
<Text style = {{fontSize: 16, alignSelf: 'center'}}>{`${date[1]}/${date[2]}/${date[0]}`}</Text>
</View>
2020-12-07 01:07:07 -05:00
</View>
{visible?extra:<></>}
</TouchableOpacity>
2020-12-07 01:07:07 -05:00
)
}
2020-12-21 02:01:08 -05:00
else {
return (
null
)
}
}
2020-08-09 17:01:25 -04:00
class Calendar extends React.Component {
2020-12-07 01:07:07 -05:00
constructor(props) {
super(props)
this.state = {
data: []
}
}
2020-12-07 01:07:07 -05:00
componentDidMount() {
this.getData()
this.props.navigation.addListener(
'focus',
() => {
this.getData()
}
);
}
2020-12-07 01:07:07 -05:00
getData() {
fetch(`${url}/api/en/events`,{
headers: {
'Cache-Control': 'no-cache'
} })
.then((response) => {
return response.text();
})
.then((json) => {
const data = JSON.parse(json).data
data.sort((a,b)=>new Date(b.date).getTime()-new Date(a.date).getTime())
this.setState({data: data});
})
.catch((error) => console.error(error))
}
2020-12-07 01:07:07 -05:00
2020-08-09 17:01:25 -04:00
render() {
return (
2020-12-07 01:07:07 -05:00
<View>
<View style = {{height: 90, display: 'flex'}}>
<LinearGradient
colors={['#f99', 'white']}
style = {{height: '100%', borderBottomColor:'black', borderBottomWidth:0.5, display: 'flex', justifyContent: 'flex-end', paddingBottom: '2.5%'}}
>
2020-12-07 01:07:48 -05:00
<Text style = {{fontSize: 24, fontWeight: 'bold', alignSelf: 'center'}}>Calendar Events</Text>
2020-12-07 01:07:07 -05:00
</LinearGradient>
</View>
<FlatList
data={this.state.data}
renderItem={item=><Event item={item}/>}
keyExtractor={item=>JSON.stringify(item)}
/>
2020-08-09 17:01:25 -04:00
</View>
)
2020-12-07 01:07:07 -05:00
}
2020-08-09 17:01:25 -04:00
}
export default Calendar;