-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLectureView.js
189 lines (158 loc) · 4.76 KB
/
LectureView.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { useEffect, useState } from "react";
import { Text, SafeAreaView, StyleSheet, ScrollView, View } from "react-native";
import SisApi from "./api";
import LectureCard from "./LectureCard";
import { useNavigation } from "@react-navigation/native";
import Button from "./Button";
/** LectureView: See all list of lectures organized by most recent
* lecture, then by future lectures and past lectures
*
* State:
* -isLoading: boolean
* -lectureData: array
*
* Props:
* none
*/
function LectureView() {
const [isLoading, setIsLoading] = useState(true);
const [lectureData, setLectureData] = useState();
const navigation = useNavigation();
/**
* Uses api to collect all data on all lectures in the cohort
* then changes staff data to the full names of all staff
* involved in each lecture
*
*/
useEffect(function fetchLecturesWhenMounted() {
async function fetchLectures() {
const allLectures = await SisApi.getLectures();
const lecturePromises = [];
allLectures.map(l => lecturePromises.push(SisApi.getLecture(l.id)));
let lectureDetailedData = await Promise.all(lecturePromises);
lectureDetailedData = lectureDetailedData.sort(function (a, b) {
return (a.start_at < b.start_at) ?
-1 :
((a.start_at > b.start_at) ?
1 :
0);
});
lectureDetailedData = await updateStaffDetails(lectureDetailedData);
console.log("after update staff", lectureDetailedData);
setLectureData(lectureDetailedData);
setIsLoading(false);
}
/**
* takes lecture data and edits staff data
* to replace urls with that staff's full name
*/
async function updateStaffDetails(lectureData) {
const allStaffData = await SisApi.getAllStaff();
const staffDirectory = {};
console.log(`data from all staff fetch is`, allStaffData);
allStaffData.forEach((staff) => staffDirectory[staff.api_url] = staff.full_name);
console.log(`our staff directory is`, staffDirectory);
for (let lecture of lectureData) {
for (let i = 0; i < lecture.staff.length; i++) {
let staffUrl = lecture.staff[i];
if (staffDirectory[staffUrl]) {
lecture.staff[i] = staffDirectory[staffUrl];
}
}
}
return lectureData;
}
fetchLectures();
}, []);
// Navigates to the homepage upon press
function homepagePress() {
navigation.navigate('Homepage');
}
if (isLoading === true) {
return (
<SafeAreaView style={styles.container}>
<Text>Loading...</Text>
</SafeAreaView>
);
}
const futureLectures = lectureData.filter(l => new Date() < new Date(l.start_at));
const pastLectures = lectureData.filter(l => new Date() > new Date(l.start_at));
return (
<SafeAreaView style={styles.container}>
<ScrollView>
<Button
onPress={homepagePress}
text="Home"
style={styles.button} />
<View>
<Text style={styles.textHeader}>Upcoming Lecture:</Text>
<LectureCard
title={futureLectures[0].title}
description={futureLectures[0].description}
startAt={futureLectures[0].start_at}
staff={futureLectures[0].staff}
/>
</View>
<View>
{futureLectures.slice(1).length > 0 &&
<Text style={styles.textHeader}>Future Lectures:</Text>}
{lectureData &&
futureLectures
.slice(1)
.map(l => (
<LectureCard
key={l.id}
title={l.title}
description={l.description}
startAt={l.start_at}
staff={l.staff} />
))}
</View>
<View>
{pastLectures.length > 0 &&
<Text style={styles.textHeader}>Past Lectures:</Text>}
{lectureData &&
pastLectures.map(l => (
<LectureCard
key={l.id}
title={l.title}
description={l.description}
startAt={l.start_at}
staff={l.staff} />
))}
</View>
</ScrollView>
</SafeAreaView>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
maxWidth: '100%',
minWidth: '100%',
alignSelf: 'center',
justifyContent: 'center',
},
text: {
alignSelf: 'center',
},
button: {
backgroundColor: '#f86161',
minWidth: '70%',
maxWidth: '70%',
padding: 5,
marginVertical: 10,
alignSelf: 'center',
textAlign: 'center',
borderRadius: 5,
fontWeight: 'bold',
color: 'white',
},
textHeader: {
alignSelf: 'center',
fontWeight: 'bold',
padding: 6,
fontSize: 20,
}
});
export default LectureView;