Nav apraksta

EnrollNextSemester.js 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import axios from "axios";
  2. import React, { useState } from "react";
  3. import { FlatList, StatusBar, StyleSheet, Text, TouchableOpacity, TextInput, View, Button, ActivityIndicator, Alert } from "react-native";
  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}-${item.section}`}</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 EnrollNextSemester = () => {
  24. const [text, setText] = useState('')
  25. const [selectedId, setSelectedId] = useState(null);
  26. const [data, setData] = useState([])
  27. const [modalVisible, setModalVisible] = useState(false)
  28. const [animating, setAnimating] = useState(false)
  29. const [isSummer, setIsSummer] = useState('false')
  30. const [code, setCode] = useState('')
  31. const [creditos, setCreditos] = useState('')
  32. const [days, setDays] = useState('')
  33. const [courseName, setCourseName] = useState('')
  34. const [prof, setProf] = useState('')
  35. const [rooms, setRooms] = useState('')
  36. const [section, setSection] = useState('')
  37. const [hours, setHours] = useState('')
  38. const toggle = ()=>{
  39. setModalVisible(!modalVisible)
  40. setSelectedId(null)
  41. }
  42. // add course (enroll) to your next semester
  43. const enrollCourse = async ()=>{
  44. const token = await SecureStore.getItemAsync('token')
  45. let id = await SecureStore.getItemAsync('id')
  46. let user_id = parseInt(id)
  47. let response = await axios({
  48. method: 'POST',
  49. url: 'http://7f9219a069f7.ngrok.io/api/matricular_prox_semestre',
  50. headers: {
  51. 'content-type': 'application/json',
  52. Authorization: `Token ${token}`
  53. },
  54. data: {
  55. user_id: user_id,
  56. course_id: selectedId,
  57. isSummer: isSummer
  58. }
  59. })
  60. console.log(response.data.msg)
  61. // setMsg(response.data.msg)
  62. setAnimating(true)
  63. // after 3 seconds, the courses, the modal and the activity indicator will disappear
  64. setTimeout(()=>{
  65. setAnimating(false)
  66. setModalVisible(false)
  67. setSelectedId(null)
  68. setData([]) // courses will disappear because data would be empty
  69. }, 3000)
  70. setTimeout(()=>{
  71. Alert.alert(response.data.msg)
  72. }, 5000)
  73. }
  74. const searchCourses = async(text)=>{
  75. let courses = []
  76. setText(text)
  77. const token = await SecureStore.getItemAsync('token')
  78. const response = await axios({
  79. method: 'GET',
  80. url: `http://7f9219a069f7.ngrok.io/api/select_course_prox_semestre?code=${text}`,
  81. headers: {
  82. 'content-type': 'application/json',
  83. Authorization: `Token ${token}`
  84. }
  85. })
  86. // looping through our response data and creating a course object to append it to the courses array
  87. response.data.list.map((course)=>{
  88. let oneCourse = {'id': course.id, 'name': course.name,'code': course.code, 'creditos': course.creditos, 'prof': course.prof, 'section': course.section, 'hours': course.hours, 'days': course.days, 'rooms': course.rooms}
  89. courses.push(oneCourse)
  90. })
  91. setData(courses)
  92. // console.log(courses)
  93. }
  94. const renderItem = ({ item }) => {
  95. const backgroundColor = item.id === selectedId ? "#e60505" : "#fafbfc";
  96. return (
  97. <Item
  98. item={item}
  99. onPress={() => {
  100. console.log('heyyyy')
  101. setSelectedId(item.id)
  102. setCourseName(item.name)
  103. setCode(item.code)
  104. setCreditos(item.creditos)
  105. setSection(item.section)
  106. setDays(item.days)
  107. setHours(item.hours)
  108. setRooms(item.rooms)
  109. setProf(item.prof)
  110. setModalVisible(true)
  111. }}
  112. style={{ backgroundColor }}
  113. />
  114. );
  115. };
  116. return (
  117. <View style={{flex: 1, padding: 10}}>
  118. <TextInput
  119. style={styles.searchBar}
  120. placeholder="Search for a course that you want to enroll for next semester"
  121. onChangeText={text=>searchCourses(text)}
  122. // onChangeText={text=>setText(text)}
  123. />
  124. <FlatList
  125. data={data}
  126. renderItem={renderItem}
  127. keyExtractor={(item) => item.id.toString()}
  128. extraData={selectedId}
  129. ItemSeparatorComponent={renderSeparator}
  130. />
  131. <Modal isVisible={modalVisible}
  132. >
  133. <View style={styles.modalItem}>
  134. <Text></Text>
  135. <Text style={styles.course_info}>Name: {courseName}</Text>
  136. <Text style={styles.course_info}>Code: {code}</Text>
  137. <Text style={styles.course_info}>Credits: {creditos}</Text>
  138. <Text style={styles.course_info}>Section: {section}</Text>
  139. <Text style={styles.course_info}>Prof: {prof}</Text>
  140. <Text style={styles.course_info}>Days: {days}</Text>
  141. <Text style={styles.course_info}>Hours: {hours}</Text>
  142. <Text style={styles.course_info}>Rooms: {rooms}</Text>
  143. <ActivityIndicator size="small" color="0000ff" animating={animating}/>
  144. <Button title="Enroll" onPress={enrollCourse}/>
  145. <Button title="Close" onPress={toggle}/>
  146. </View>
  147. </Modal>
  148. </View>
  149. );
  150. };
  151. const styles = StyleSheet.create({
  152. container: {
  153. flex: 1,
  154. marginTop: StatusBar.currentHeight || 0,
  155. },
  156. item: {
  157. padding: 10,
  158. marginVertical: 8,
  159. marginHorizontal: 16,
  160. },
  161. title: {
  162. fontSize: 15,
  163. },
  164. searchBar: {
  165. height: 40,
  166. borderColor: '#000',
  167. borderWidth: 1
  168. },
  169. modalItem: {
  170. // width: '30%', // is 30% of container width
  171. margin: 60, // 300
  172. backgroundColor: 'white',
  173. borderRadius:20,
  174. height: 270
  175. },
  176. course_info: {
  177. fontWeight: 'bold',
  178. fontSize: 13
  179. }
  180. });
  181. export default EnrollNextSemester;