설명 없음

CurrentCourses.js 8.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import React, {useState, useEffect} from 'react';
  2. import { FlatList, StatusBar, StyleSheet, Text, TouchableOpacity, TextInput, View, Button, ActivityIndicator, Alert, ScrollView, RefreshControl } from "react-native";
  3. import axios from 'axios'
  4. import * as SecureStore from 'expo-secure-store';
  5. import Modal from 'react-native-modal';
  6. const Item = ({ item, onPress, style }) => (
  7. <TouchableOpacity onPress={onPress} style={[styles.item, style]}>
  8. <Text style={styles.title}>{`${item.code}`}</Text>
  9. </TouchableOpacity>
  10. );
  11. // separates results, taken from https://stackoverflow.com/questions/60350768/how-to-make-search-bar-with-dropdown-list-in-react-native
  12. const renderSeparator = () => {
  13. return (
  14. <View
  15. style={{
  16. height: 1,
  17. width: '100%',
  18. backgroundColor: '#CED0CE',
  19. }}
  20. />
  21. );
  22. };
  23. const CurrentCourses = () =>{
  24. const [courses, setCourses] = useState(null)
  25. const [refreshing, setRefreshing] = useState(false)
  26. const [selectedId, setSelectedId] = useState(null); // course_id
  27. const [animating, setAnimating] = useState(false)
  28. const [modalVisible, setModalVisible] = useState(false)
  29. const [code, setCode] = useState(null)
  30. const [days, setDays] = useState(null)
  31. const [courseName, setCourseName] = useState(null)
  32. const [prof, setProf] = useState(null)
  33. const [rooms, setRooms] = useState(null)
  34. const [section, setSection] = useState(null)
  35. const [hours, setHours] = useState(null)
  36. const [year, setYear] = useState(null)
  37. const [semestre, setSemestre] = useState(null)
  38. const toggle = ()=>{
  39. setModalVisible(!modalVisible)
  40. setSelectedId(null)
  41. }
  42. const getMyCurrentCourses = async()=>{
  43. const token = await SecureStore.getItemAsync('token')
  44. let id = await SecureStore.getItemAsync('id')
  45. let user_id = parseInt(id)
  46. try {
  47. let response = await axios(`http://481cb6e289f9.ngrok.io/api/get_current_courses`, {
  48. method: 'POST',
  49. headers: {
  50. 'content-type': 'application/json',
  51. Authorization: `Token ${token}`
  52. },
  53. data:{
  54. user_id: user_id
  55. }
  56. })
  57. console.log(response.data.list)
  58. if(response.data.list){
  59. setCourses(response.data.list)
  60. }
  61. else{
  62. setCourses(response.data.msg)
  63. }
  64. console.log(courses)
  65. } catch(error){
  66. console.log(error)
  67. }
  68. }
  69. const deleteOneCourse = async()=>{
  70. const token = await SecureStore.getItemAsync('token')
  71. let id = await SecureStore.getItemAsync('id')
  72. let user_id = parseInt(id)
  73. console.log(semestre)
  74. console.log(year)
  75. try {
  76. let response = await axios(`http://481cb6e289f9.ngrok.io/api/delete_course`, {
  77. method: 'DELETE',
  78. headers: {
  79. 'content-type': 'application/json',
  80. Authorization: `Token ${token}`
  81. },
  82. data:{
  83. user_id: user_id,
  84. course_id: selectedId,
  85. year: year,
  86. semestre: semestre
  87. }
  88. })
  89. setAnimating(true)
  90. // after 3 seconds, the courses, the modal and the activity indicator will disappear
  91. setTimeout(()=>{
  92. setAnimating(false)
  93. setModalVisible(false)
  94. setSelectedId(null)
  95. }, 3000)
  96. setTimeout(()=>{
  97. Alert.alert(response.data.msg)
  98. }, 5000)
  99. getMyCurrentCourses() // get current courses again
  100. setSection(null)
  101. setDays(null)
  102. setHours(null)
  103. setRooms(null)
  104. setProf(null)
  105. } catch(error){
  106. console.log(error)
  107. }
  108. }
  109. const onRefresh = React.useCallback(async ()=>{
  110. setRefreshing(true)
  111. getMyCurrentCourses()
  112. setRefreshing(false)
  113. }, [refreshing])
  114. useEffect(()=>{
  115. console.log('dimelo')
  116. getMyCurrentCourses()
  117. },[])
  118. const renderItem = ({ item }) => {
  119. const backgroundColor = item.id === selectedId ? "#e60505" : "#fafbfc";
  120. return (
  121. <Item
  122. item={item}
  123. onPress={() => {
  124. setSelectedId(item.id)
  125. if(item.section === undefined){
  126. setCourseName(item.name)
  127. setCode(item.code)
  128. setYear(item.year)
  129. setSemestre(item.semestre)
  130. setModalVisible(true)
  131. }
  132. else{
  133. setCourseName(item.name)
  134. setCode(item.code)
  135. setYear(item.year)
  136. setSemestre(item.semestre)
  137. setSection(item.section)
  138. setDays(item.dias)
  139. setHours(item.horarios)
  140. setRooms(item.salones)
  141. setProf(item.prof)
  142. setModalVisible(true)
  143. }
  144. }}
  145. style={{ backgroundColor }}
  146. />
  147. );
  148. };
  149. // if student does not have courses currently, return this
  150. if(courses === null || courses === 'No tienes cursos'){
  151. return (
  152. <ScrollView
  153. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh}/>}
  154. >
  155. <Text>No tienes cursos</Text>
  156. </ScrollView>
  157. )
  158. }
  159. return (
  160. <View style={styles.container}>
  161. <FlatList
  162. data={courses}
  163. renderItem={renderItem}
  164. keyExtractor={(item) => item.id.toString()}
  165. extraData={selectedId}
  166. ItemSeparatorComponent={renderSeparator}
  167. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh}/>}
  168. />
  169. <Modal isVisible={modalVisible}
  170. >
  171. <View style={styles.modalItem}>
  172. <Text></Text>
  173. <Text style={styles.course_info}>Name: {courseName}</Text>
  174. <Text style={styles.course_info}>Code: {code}</Text>
  175. <Text style={styles.course_info}>{section === null ? '' : 'Section '+ section} </Text>
  176. <Text style={styles.course_info}>{prof === null ? '' : 'Prof:' + prof} </Text>
  177. <Text style={styles.course_info}>{days === null ? '' : 'Days: ' + days} </Text>
  178. <Text style={styles.course_info}>{hours === null ? '' : 'Hours: '+ hours} </Text>
  179. <Text style={styles.course_info}>{rooms === null ? '' : 'Rooms: ' + rooms} </Text>
  180. <ActivityIndicator size="small" color="0000ff" animating={animating}/>
  181. <Button title="Delete Course" onPress={deleteOneCourse}/>
  182. <Button title="Close" onPress={toggle}/>
  183. </View>
  184. </Modal>
  185. </View>
  186. );
  187. }
  188. export default CurrentCourses
  189. const styles = StyleSheet.create({
  190. container: {
  191. flex: 1,
  192. marginTop: StatusBar.currentHeight || 0,
  193. },
  194. item: {
  195. padding: 10,
  196. marginVertical: 8,
  197. marginHorizontal: 16,
  198. },
  199. title: {
  200. fontSize: 15,
  201. },
  202. searchBar: {
  203. height: 40,
  204. borderColor: '#000',
  205. borderWidth: 1
  206. },
  207. modalItem: {
  208. // width: '30%', // is 30% of container width
  209. margin: 60, // 300
  210. backgroundColor: 'white',
  211. borderRadius:20,
  212. height: 270
  213. },
  214. course_info: {
  215. fontWeight: 'bold',
  216. fontSize: 13
  217. }
  218. });
  219. // const styles = StyleSheet.create({
  220. // container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
  221. // head: { height: 40, backgroundColor: '#e82020' },
  222. // wrapper: { flexDirection: 'row' },
  223. // title: { flex: 1, backgroundColor: '#e82020' },
  224. // row: { height: 28 },
  225. // text: { textAlign: 'center', fontSize:10 },
  226. // });