Keine Beschreibung

Agenda.js 8.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. import React, {useState, useEffect} from 'react';
  2. import { StyleSheet, View, Text, ScrollView, RefreshControl } from 'react-native';
  3. import WeeklyCalendar from 'react-native-weekly-calendar';
  4. import * as SecureStore from 'expo-secure-store';
  5. import axios from 'axios'
  6. export default function App() {
  7. const [data, setData] = useState('')
  8. const [sampleEvents, setSampleEvents] = useState([])
  9. const [refreshing, setRefreshing] = useState(false)
  10. // const sampleEvents = [
  11. // { 'start': '2020-12-06 09:00:00', 'duration': '01:20:00', 'note': 'CCOM4120' },
  12. // { 'start': '2020-12-06 14:00:00', 'duration': '01:20:00', 'note': 'MATE3001' },
  13. // { 'start': '2020-12-06 17:00:00', 'duration': '00:30:00', 'note': 'INGL1001' },
  14. // { 'start': '2020-12-07 14:00:00', 'duration': '02:00:00', 'note': 'SICI3211' },
  15. // { 'start': '2020-12-07 10:00:00', 'duration': '02:00:00', 'note': 'BIOL3101' },
  16. // { 'start': '2020-12-08 09:00:00', 'duration': '01:20:00', 'note': 'CCOM4120' },
  17. // { 'start': '2020-12-08 14:00:00', 'duration': '01:20:00', 'note': 'MATE3001' },
  18. // { 'start': '2020-12-08 17:00:00', 'duration': '00:30:00', 'note': 'INGL1001' },
  19. // { 'start': '2020-12-09 14:00:00', 'duration': '02:00:00', 'note': 'SICI3211' },
  20. // { 'start': '2020-12-09 10:00:00', 'duration': '02:00:00', 'note': 'BIOL3101' },
  21. // ]
  22. const getMyCurriculum = async()=>{
  23. const token = await SecureStore.getItemAsync('token')
  24. let id = await SecureStore.getItemAsync('id')
  25. let user_id = parseInt(id)
  26. let response = await axios({
  27. method: 'POST',
  28. url: 'http://7f9219a069f7.ngrok.io/api/get_current_courses',
  29. headers: {
  30. 'content-type': 'application/json',
  31. Authorization: `Token ${token}`
  32. },
  33. data: {
  34. user_id: user_id
  35. }
  36. })
  37. // setSampleEvents([])
  38. console.log(response.data.list, '------------------------------------------------------------------------------d')
  39. if(response.data.msg){
  40. setSampleEvents([])
  41. }
  42. else if(response.data.list[0].horarios === undefined){
  43. setSampleEvents([])
  44. }
  45. else {
  46. // courses
  47. setData(response.data.list)
  48. console.log(data)
  49. // Loop para ir curso por curso y a~adir a la agenda
  50. let courses = []
  51. data.map((course)=>{
  52. // console.log(typeof(course.horarios))
  53. let horarios = course.horarios
  54. let titulo = course.name
  55. let horario_length = horarios.length
  56. let horas = 0
  57. let minutos = 0
  58. let segundos = "00"
  59. let dura = ""
  60. let repetir = 0
  61. let hora =""
  62. let empieza= ""
  63. let minuto = ""
  64. // console.log(horario_length)
  65. horarios = horarios.replace(/A/g,'')
  66. horarios = horarios.replace(/M/g,'')
  67. horarios = horarios.replace(/P/g,'')
  68. horarios = horarios.replace(/-/g,':')
  69. // console.log(horarios)
  70. // Bregando con la DURACION
  71. if(horario_length == 15){
  72. horarios = horarios.split(':')
  73. horas = horarios[2] - horarios[0]
  74. minutos = horarios[3] - horarios[1]
  75. minutos = minutos.toString()
  76. horas = horas.toString()
  77. horas = '0' + horas
  78. // console.log("Horas: ", horas)
  79. // console.log("Minutos: ", minutos)
  80. // console.log("Segundos: ", segundos)
  81. dura = horas + ":" + minutos + ":" + segundos
  82. // console.log(dura)
  83. // console.log(typeof(segundos))
  84. // console.log(typeof(dura))
  85. }
  86. // Duracion para mas de un horario de una clase
  87. else{
  88. horarios = horarios.replace(/,/g,':')
  89. horarios = horarios.split(':')
  90. horario_length = horarios.length
  91. console.log(horario_length)
  92. repetir = horario_length/4
  93. console.log("Repetir: ", repetir)
  94. console.log(horarios)
  95. horas = horarios[2] - horarios[0]
  96. minutos = horarios[3] - horarios[1]
  97. minutos = minutos.toString()
  98. horas = horas.toString()
  99. horas = '0' + horas
  100. // console.log("Horas: ", horas)
  101. // console.log("Minutos: ", minutos)
  102. // console.log("Segundos: ", segundos)
  103. dura = horas + ":" + minutos + ":" + segundos
  104. // let horas2 = horarios[7] - horarios[5]
  105. // let minutos2 = horarios[8] - horarios[6]
  106. // minutos2 = minutos.toString()
  107. // horas2 = horas.toString()
  108. // horas2 = '0' + horas
  109. // // console.log("Horas: ", horas)
  110. // // console.log("Minutos: ", minutos)
  111. // // console.log("Segundos: ", segundos)
  112. // dura2 = horas2 + ":" + minutos2 + ":" + segundos
  113. }
  114. // Bregando con el START
  115. if (horarios[0] < 7){
  116. // console.log("Es menor: ", horarios[0])
  117. hora = Number(horarios[0]) + 12
  118. }
  119. else{
  120. hora = horarios[0]
  121. }
  122. minuto = horarios[1]
  123. hora = hora.toString()
  124. // console.log("Hora: ", hora)
  125. // console.log(typeof(hora))
  126. // console.log("Min: ", minuto)
  127. // console.log(typeof(minuto))
  128. empieza = hora + ":" + minuto + ":" + segundos
  129. let date =""
  130. let i = 0
  131. let dias = course.dias
  132. for (i = 0; i < dias.length; i++ ){
  133. console.log("Esta es la letra ahora: ", dias[i])
  134. let month_year = "2020-12"
  135. let date_time = ""
  136. if(dias[i] == "L"){
  137. date = "-14 "
  138. date_time = month_year + date + empieza
  139. console.log()
  140. console.log("Starts: ", date_time)
  141. // console.log("Duracion: ", dura)
  142. // console.log(typeof(empieza))
  143. courses.push({start: date_time, duration: dura , note: course.code})
  144. console.log(dias)
  145. }
  146. if(dias[i] == "M"){
  147. date = "-15 "
  148. date_time = month_year + date + empieza
  149. console.log()
  150. console.log("Starts: ", date_time)
  151. // console.log("Duracion: ", dura)
  152. // console.log(typeof(empieza))
  153. courses.push({start: date_time, duration: dura , note: course.code})
  154. console.log(dias)
  155. }
  156. if(dias[i] == "W"){
  157. date = "-16 "
  158. date_time = month_year + date + empieza
  159. console.log()
  160. console.log("Starts: ", date_time)
  161. // console.log("Duracion: ", dura)
  162. // console.log(typeof(empieza))
  163. courses.push({start: date_time, duration: dura , note: course.code})
  164. console.log(dias)
  165. }
  166. if(dias[i] == "J"){
  167. date = "-17 "
  168. date_time = month_year + date + empieza
  169. console.log()
  170. console.log("Starts: ", date_time)
  171. // console.log("Duracion: ", dura)
  172. // console.log(typeof(empieza))
  173. courses.push({start: date_time, duration: dura , note: course.code})
  174. console.log(dias)
  175. }
  176. if(dias[i] == "V"){
  177. date = "-18 "
  178. date_time = month_year + date + empieza
  179. console.log()
  180. console.log("Starts: ", date_time)
  181. // console.log("Duracion: ", dura)
  182. // console.log(typeof(empieza))
  183. courses.push({start: date_time, duration: dura , note: course.code})
  184. console.log(dias)
  185. }
  186. }
  187. })
  188. setSampleEvents(courses)
  189. }
  190. // console.log(courses)
  191. }
  192. const onRefresh = React.useCallback(async ()=>{
  193. setRefreshing(true)
  194. setSampleEvents([])
  195. getMyCurriculum()
  196. setRefreshing(false)
  197. }, [refreshing])
  198. const renderCourses = ()=>{
  199. console.log(sampleEvents, 'sampleevents')
  200. if(sampleEvents.length > 0){
  201. return (
  202. <View>
  203. <Text>AGENDA DE LA UNI</Text>
  204. <WeeklyCalendar events={sampleEvents} style={{ height: 400 }} />
  205. </View>
  206. )
  207. }
  208. else{
  209. return (
  210. <View>
  211. <Text>No tienes agenda porque todavía no has hecho matricula</Text>
  212. {/* <WeeklyCalendar events={sampleEvents} style={{ height: 400 }} /> */}
  213. </View>
  214. )
  215. }
  216. }
  217. useEffect(()=>{
  218. getMyCurriculum()
  219. }, [])
  220. return (
  221. <ScrollView
  222. refreshControl={<RefreshControl refreshing={refreshing} onRefresh={onRefresh}/>}
  223. >
  224. {renderCourses()}
  225. </ScrollView>
  226. );
  227. }
  228. const styles = StyleSheet.create({
  229. container: {
  230. flex: 1,
  231. backgroundColor: '#fff',
  232. alignItems: 'center',
  233. justifyContent: 'center',
  234. }
  235. });