Няма описание

AddTakenCourse.js 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. import {Picker} from '@react-native-community/picker';
  7. const Item = ({ item, onPress, style }) => (
  8. <TouchableOpacity onPress={onPress} style={[styles.item, style]}>
  9. <Text style={styles.title}>{item.code}</Text>
  10. </TouchableOpacity>
  11. );
  12. // separates results, taken from https://stackoverflow.com/questions/60350768/how-to-make-search-bar-with-dropdown-list-in-react-native
  13. const renderSeparator = () => {
  14. return (
  15. <View
  16. style={{
  17. height: 1,
  18. width: '100%',
  19. backgroundColor: '#CED0CE',
  20. }}
  21. />
  22. );
  23. };
  24. const AddTakenCourse = () => {
  25. const [text, setText] = useState('')
  26. const [selectedId, setSelectedId] = useState(null);
  27. const [data, setData] = useState([])
  28. const [modalVisible, setModalVisible] = useState(false)
  29. const [grade, setGrade] = useState('A')
  30. const [year, setYear] = useState('1')
  31. const [semester, setSemester] = useState('1')
  32. const [animating, setAnimating] = useState(false)
  33. const toggle = ()=>{
  34. setModalVisible(!modalVisible)
  35. setSelectedId(null)
  36. }
  37. const addCourse = async ()=>{
  38. const token = await SecureStore.getItemAsync('token')
  39. let id = await SecureStore.getItemAsync('id')
  40. let user_id = parseInt(id)
  41. console.log('year', year)
  42. console.log('semester', semester)
  43. console.log('grade', grade)
  44. let response = await axios({
  45. method: 'POST',
  46. url: 'http://7f9219a069f7.ngrok.io/api/add_taken_course',
  47. headers: {
  48. 'content-type': 'application/json',
  49. Authorization: `Token ${token}`
  50. },
  51. data: {
  52. semester: semester,
  53. year: year,
  54. user_id: user_id,
  55. grade: grade,
  56. course_id: selectedId
  57. }
  58. })
  59. console.log(response.data.msg)
  60. setAnimating(true)
  61. setTimeout(()=>{
  62. setAnimating(false)
  63. setModalVisible(false)
  64. setSelectedId(null)
  65. setData([]) // courses will disappear because data would be empty
  66. }, 3000)
  67. setTimeout(()=>{
  68. Alert.alert(response.data.msg)
  69. }, 5000)
  70. }
  71. const searchCourses = async(text)=>{
  72. let courses = []
  73. setText(text)
  74. const token = await SecureStore.getItemAsync('token')
  75. const response = await axios({
  76. method: 'GET',
  77. url: `http://7f9219a069f7.ngrok.io/api/find_course?code=${text}`,
  78. headers: {
  79. 'content-type': 'application/json',
  80. Authorization: `Token ${token}`
  81. }
  82. })
  83. response.data.list.map((course)=>{
  84. let oneCourse = {'id': course.id, 'code': course.code}
  85. courses.push(oneCourse)
  86. })
  87. setData(courses)
  88. }
  89. const renderItem = ({ item }) => {
  90. const backgroundColor = item.id === selectedId ? "#e60505" : "#fafbfc";
  91. return (
  92. <Item
  93. item={item}
  94. onPress={() => {
  95. setSelectedId(item.id)
  96. setModalVisible(true)
  97. }}
  98. style={{ backgroundColor }}
  99. />
  100. );
  101. };
  102. console.log(selectedId)
  103. return (
  104. <View style={{flex: 1, padding: 10}}>
  105. <TextInput
  106. style={styles.searchBar}
  107. placeholder="Search for a course that you've taken"
  108. onChangeText={text=>searchCourses(text)}
  109. // onChangeText={text=>setText(text)}
  110. />
  111. <FlatList
  112. data={data}
  113. renderItem={renderItem}
  114. keyExtractor={(item) => item.id.toString()}
  115. extraData={selectedId}
  116. ItemSeparatorComponent={renderSeparator}
  117. />
  118. <Modal isVisible={modalVisible} style={{
  119. backgroundColor: 'white',
  120. flexDirection: 'row',
  121. borderRadius: 20,
  122. margin: 50,
  123. padding: 10,
  124. }}>
  125. <View style={styles.modalItem}>
  126. <Text>Grade</Text>
  127. <Picker
  128. selectedValue={grade}
  129. style={{ width: 50}}
  130. onValueChange={(itemValue, itemIndex) => setGrade(itemValue) }>
  131. <Picker.Item label="A" value="A" />
  132. <Picker.Item label="B" value="B" />
  133. <Picker.Item label="C" value="C" />
  134. <Picker.Item label="D" value="D" />
  135. <Picker.Item label="F" value="F" />
  136. </Picker>
  137. </View>
  138. <View style={styles.modalItem}>
  139. <Text>Year</Text>
  140. <Picker
  141. selectedValue={year}
  142. style={{ width: 50}}
  143. onValueChange={(itemValue, itemIndex) => setYear(itemValue) }>
  144. <Picker.Item label="1" value="1" />
  145. <Picker.Item label="2" value="2" />
  146. <Picker.Item label="3" value="3" />
  147. <Picker.Item label="4" value="4" />
  148. <Picker.Item label="5" value="5" />
  149. <Picker.Item label="6" value="6" />
  150. </Picker>
  151. </View>
  152. <View style={styles.modalItem}>
  153. <Text>Semester</Text>
  154. <Picker
  155. selectedValue={semester}
  156. style={{ width: 50}}
  157. onValueChange={(itemValue, itemIndex) => setSemester(itemValue) }>
  158. <Picker.Item label="1" value="1" />
  159. <Picker.Item label="2" value="2" />
  160. </Picker>
  161. </View>
  162. <View style={{flex: 1, justifyContent: 'center'}}>
  163. <ActivityIndicator size="large" color="0000ff" animating={animating}/>
  164. <View>
  165. <Button title="Submit" onPress={addCourse}/>
  166. <Button title="Close" onPress={toggle}/>
  167. </View>
  168. </View>
  169. </Modal>
  170. </View>
  171. );
  172. };
  173. const styles = StyleSheet.create({
  174. container: {
  175. flex: 1,
  176. marginTop: StatusBar.currentHeight || 0,
  177. },
  178. item: {
  179. padding: 10,
  180. marginVertical: 8,
  181. marginHorizontal: 16,
  182. },
  183. title: {
  184. fontSize: 15,
  185. },
  186. searchBar: {
  187. height: 40,
  188. borderColor: '#000',
  189. borderWidth: 1
  190. },
  191. modalItem: {
  192. // width: '30%', // is 30% of container width
  193. margin: 8 // 300
  194. }
  195. });
  196. export default AddTakenCourse;