123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206 |
- import axios from "axios";
- import React, { useState } from "react";
- import { FlatList, StatusBar, StyleSheet, Text, TouchableOpacity, TextInput, View, Button, ActivityIndicator, Alert } from "react-native";
- import * as SecureStore from 'expo-secure-store';
- import Modal from 'react-native-modal';
-
-
- const Item = ({ item, onPress, style }) => (
- <TouchableOpacity onPress={onPress} style={[styles.item, style]}>
- <Text style={styles.title}>{`${item.code}-${item.section}`}</Text>
- </TouchableOpacity>
- );
-
- // separates results, taken from https://stackoverflow.com/questions/60350768/how-to-make-search-bar-with-dropdown-list-in-react-native
- const renderSeparator = () => {
- return (
- <View
- style={{
- height: 1,
- width: '100%',
- backgroundColor: '#CED0CE',
- }}
- />
- );
- };
-
-
- const EnrollNextSemester = () => {
- const [text, setText] = useState('')
- const [selectedId, setSelectedId] = useState(null);
- const [data, setData] = useState([])
- const [modalVisible, setModalVisible] = useState(false)
- const [animating, setAnimating] = useState(false)
- const [isSummer, setIsSummer] = useState('false')
- const [code, setCode] = useState('')
- const [creditos, setCreditos] = useState('')
- const [days, setDays] = useState('')
- const [courseName, setCourseName] = useState('')
- const [prof, setProf] = useState('')
- const [rooms, setRooms] = useState('')
- const [section, setSection] = useState('')
- const [hours, setHours] = useState('')
-
- const toggle = ()=>{
- setModalVisible(!modalVisible)
- setSelectedId(null)
- }
-
- // add course (enroll) to your next semester
- const enrollCourse = async ()=>{
- const token = await SecureStore.getItemAsync('token')
- let id = await SecureStore.getItemAsync('id')
- let user_id = parseInt(id)
-
-
- let response = await axios({
- method: 'POST',
- url: 'http://7f9219a069f7.ngrok.io/api/matricular_prox_semestre',
- headers: {
- 'content-type': 'application/json',
- Authorization: `Token ${token}`
- },
- data: {
- user_id: user_id,
- course_id: selectedId,
- isSummer: isSummer
- }
- })
- console.log(response.data.msg)
- // setMsg(response.data.msg)
-
- setAnimating(true)
-
- // after 3 seconds, the courses, the modal and the activity indicator will disappear
- setTimeout(()=>{
- setAnimating(false)
- setModalVisible(false)
- setSelectedId(null)
- setData([]) // courses will disappear because data would be empty
- }, 3000)
-
- setTimeout(()=>{
- Alert.alert(response.data.msg)
- }, 5000)
-
- }
-
-
-
- const searchCourses = async(text)=>{
- let courses = []
- setText(text)
- const token = await SecureStore.getItemAsync('token')
- const response = await axios({
- method: 'GET',
- url: `http://7f9219a069f7.ngrok.io/api/select_course_prox_semestre?code=${text}`,
- headers: {
- 'content-type': 'application/json',
- Authorization: `Token ${token}`
- }
-
- })
- // looping through our response data and creating a course object to append it to the courses array
- response.data.list.map((course)=>{
- 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}
- courses.push(oneCourse)
- })
-
- setData(courses)
- // console.log(courses)
- }
-
- const renderItem = ({ item }) => {
- const backgroundColor = item.id === selectedId ? "#e60505" : "#fafbfc";
- return (
- <Item
- item={item}
- onPress={() => {
- console.log('heyyyy')
- setSelectedId(item.id)
- setCourseName(item.name)
- setCode(item.code)
- setCreditos(item.creditos)
- setSection(item.section)
- setDays(item.days)
- setHours(item.hours)
- setRooms(item.rooms)
- setProf(item.prof)
- setModalVisible(true)
- }}
- style={{ backgroundColor }}
- />
- );
- };
-
-
- return (
- <View style={{flex: 1, padding: 10}}>
- <TextInput
- style={styles.searchBar}
- placeholder="Search for a course that you want to enroll for next semester"
- onChangeText={text=>searchCourses(text)}
- // onChangeText={text=>setText(text)}
- />
- <FlatList
- data={data}
- renderItem={renderItem}
- keyExtractor={(item) => item.id.toString()}
- extraData={selectedId}
- ItemSeparatorComponent={renderSeparator}
- />
- <Modal isVisible={modalVisible}
- >
- <View style={styles.modalItem}>
- <Text></Text>
- <Text style={styles.course_info}>Name: {courseName}</Text>
- <Text style={styles.course_info}>Code: {code}</Text>
- <Text style={styles.course_info}>Credits: {creditos}</Text>
- <Text style={styles.course_info}>Section: {section}</Text>
- <Text style={styles.course_info}>Prof: {prof}</Text>
- <Text style={styles.course_info}>Days: {days}</Text>
- <Text style={styles.course_info}>Hours: {hours}</Text>
- <Text style={styles.course_info}>Rooms: {rooms}</Text>
- <ActivityIndicator size="small" color="0000ff" animating={animating}/>
- <Button title="Enroll" onPress={enrollCourse}/>
- <Button title="Close" onPress={toggle}/>
- </View>
- </Modal>
- </View>
- );
- };
-
-
- const styles = StyleSheet.create({
- container: {
- flex: 1,
- marginTop: StatusBar.currentHeight || 0,
- },
- item: {
- padding: 10,
- marginVertical: 8,
- marginHorizontal: 16,
- },
- title: {
- fontSize: 15,
- },
- searchBar: {
- height: 40,
- borderColor: '#000',
- borderWidth: 1
- },
- modalItem: {
- // width: '30%', // is 30% of container width
- margin: 60, // 300
- backgroundColor: 'white',
- borderRadius:20,
- height: 270
- },
- course_info: {
- fontWeight: 'bold',
- fontSize: 13
- }
-
- });
-
- export default EnrollNextSemester;
|