暫無描述

EditGrades.js 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. 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}: ${item.grade}`}</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 CurrentCourses = () =>{
  25. const [courses, setCourses] = useState(null)
  26. const [refreshing, setRefreshing] = useState(false)
  27. const [selectedId, setSelectedId] = useState(null); // course_id
  28. const [animating, setAnimating] = useState('')
  29. const [modalVisible, setModalVisible] = useState(false)
  30. const [year, setYear] = useState(null)
  31. const [semestre, setSemestre] = useState(null)
  32. const [grade, setGrade] = useState('A')
  33. const toggle = ()=>{
  34. setModalVisible(!modalVisible)
  35. setSelectedId(null)
  36. }
  37. const getAllMyCourses = async()=>{
  38. const token = await SecureStore.getItemAsync('token')
  39. let id = await SecureStore.getItemAsync('id')
  40. let user_id = parseInt(id)
  41. console.log(user_id)
  42. try {
  43. let response = await axios(`http://da0406585426.ngrok.io/api/get_all_courses_user_has_taken?user_id=${user_id}`, {
  44. method: 'GET',
  45. headers: {
  46. 'content-type': 'application/json',
  47. Authorization: `Token ${token}`
  48. }
  49. })
  50. // console.log(response.data.list)
  51. setCourses(response.data.list)
  52. } catch(error){
  53. console.log(error)
  54. }
  55. }
  56. const deleteOneCourse = async()=>{
  57. const token = await SecureStore.getItemAsync('token')
  58. let id = await SecureStore.getItemAsync('id')
  59. let user_id = parseInt(id)
  60. try {
  61. let response = await axios(`http://da0406585426.ngrok.io/api/delete_course`, {
  62. method: 'DELETE',
  63. headers: {
  64. 'content-type': 'application/json',
  65. Authorization: `Token ${token}`
  66. },
  67. data:{
  68. user_id: user_id,
  69. course_id: selectedId,
  70. year: year,
  71. semestre: semestre
  72. }
  73. })
  74. setAnimating(true)
  75. // after 3 seconds, the courses, the modal and the activity indicator will disappear
  76. setTimeout(()=>{
  77. setAnimating(false)
  78. setModalVisible(false)
  79. setSelectedId(null)
  80. }, 3000)
  81. setTimeout(()=>{
  82. Alert.alert(response.data.msg)
  83. }, 5000)
  84. getAllMyCourses() // get current courses again
  85. } catch(error){
  86. console.log(error)
  87. }
  88. }
  89. const updateGrade = async()=>{
  90. const token = await SecureStore.getItemAsync('token')
  91. let id = await SecureStore.getItemAsync('id')
  92. let user_id = parseInt(id)
  93. console.log(semestre)
  94. console.log(year)
  95. try {
  96. let response = await axios(`http://da0406585426.ngrok.io/api/update_grade_and_gpa`, {
  97. method: 'PATCH',
  98. headers: {
  99. 'content-type': 'application/json',
  100. Authorization: `Token ${token}`
  101. },
  102. data:{
  103. user_id: user_id,
  104. course_id: selectedId,
  105. year: year,
  106. semestre: semestre,
  107. grade: grade
  108. }
  109. })
  110. setAnimating(true)
  111. // after 3 seconds, the courses, the modal and the activity indicator will disappear
  112. setTimeout(()=>{
  113. setAnimating(false)
  114. setModalVisible(false)
  115. setSelectedId(null)
  116. }, 3000)
  117. setTimeout(()=>{
  118. Alert.alert(response.data.msg)
  119. }, 5000)
  120. getAllMyCourses() // get current courses again
  121. } catch(error){
  122. console.log(error)
  123. }
  124. }
  125. const onRefresh = React.useCallback(async ()=>{
  126. setRefreshing(true)
  127. getAllMyCourses()
  128. setRefreshing(false)
  129. }, [refreshing])
  130. useEffect(()=>{
  131. getAllMyCourses()
  132. },[])
  133. const renderItem = ({ item }) => {
  134. const backgroundColor = item.course_id === selectedId ? "#e60505" : "#fafbfc";
  135. return (
  136. <Item
  137. item={item}
  138. onPress={() => {
  139. setModalVisible(true)
  140. setSelectedId(item.course_id)
  141. setYear(item.year)
  142. setSemestre(item.semestre)
  143. setGrade(item.grade)
  144. }}
  145. style={{ backgroundColor }}
  146. />
  147. );
  148. };
  149. // if student does not have courses currently, return this
  150. if(courses === null || courses.length === 0){
  151. return (
  152. <ScrollView
  153. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh}/>}
  154. >
  155. <Text>No tienes cursos</Text>
  156. </ScrollView>
  157. )
  158. }
  159. // console.log(grade)
  160. return (
  161. <View style={styles.container}>
  162. <FlatList
  163. data={courses}
  164. renderItem={renderItem}
  165. keyExtractor={(item) => item.course_id.toString()}
  166. extraData={selectedId}
  167. ItemSeparatorComponent={renderSeparator}
  168. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh}/>}
  169. />
  170. <Modal isVisible={modalVisible}
  171. >
  172. <View style={styles.modalItem}>
  173. <Button title= "Update grade" onPress={updateGrade} />
  174. <Picker
  175. selectedValue={grade}
  176. style={{ width: 50}}
  177. onValueChange={(itemValue, itemIndex) => setGrade(itemValue) }>
  178. <Picker.Item label="A" value="A" />
  179. <Picker.Item label="B" value="B" />
  180. <Picker.Item label="C" value="C" />
  181. <Picker.Item label="D" value="D" />
  182. <Picker.Item label="F" value="F" />
  183. </Picker>
  184. <ActivityIndicator size="small" color="0000ff" animating={animating}/>
  185. <Button title="Delete Course" onPress={deleteOneCourse}/>
  186. <Button title="Close" onPress={toggle}/>
  187. </View>
  188. </Modal>
  189. </View>
  190. );
  191. }
  192. export default CurrentCourses
  193. const styles = StyleSheet.create({
  194. container: {
  195. flex: 1,
  196. marginTop: StatusBar.currentHeight || 0,
  197. },
  198. item: {
  199. padding: 10,
  200. marginVertical: 8,
  201. marginHorizontal: 16,
  202. },
  203. title: {
  204. fontSize: 15,
  205. },
  206. searchBar: {
  207. height: 40,
  208. borderColor: '#000',
  209. borderWidth: 1
  210. },
  211. modalItem: {
  212. // width: '30%', // is 30% of container width
  213. margin: 60, // 300
  214. backgroundColor: 'white',
  215. borderRadius:20,
  216. height: 340
  217. },
  218. course_info: {
  219. fontWeight: 'bold',
  220. fontSize: 13
  221. }
  222. });