Aucune description

AddTakenCourse.js 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import axios from "axios";
  2. import React, { useState } from "react";
  3. import { FlatList, SafeAreaView, StatusBar, StyleSheet, Text, TouchableOpacity, TextInput, View } from "react-native";
  4. import * as SecureStore from 'expo-secure-store';
  5. const Item = ({ item, onPress, style }) => (
  6. <TouchableOpacity onPress={onPress} style={[styles.item, style]}>
  7. <Text style={styles.title}>{item.code}</Text>
  8. </TouchableOpacity>
  9. );
  10. // separates results, taken from https://stackoverflow.com/questions/60350768/how-to-make-search-bar-with-dropdown-list-in-react-native
  11. const renderSeparator = () => {
  12. return (
  13. <View
  14. style={{
  15. height: 1,
  16. width: '100%',
  17. backgroundColor: '#CED0CE',
  18. }}
  19. />
  20. );
  21. };
  22. const AddTakenCourse = () => {
  23. const [text, setText] = useState('')
  24. const [selectedId, setSelectedId] = useState(null);
  25. const [data, setData] = useState([])
  26. const searchCourses = async(text)=>{
  27. let courses = []
  28. setText(text)
  29. const token = await SecureStore.getItemAsync('token')
  30. const response = await axios({
  31. method: 'GET',
  32. url: `http://ef32e7a10841.ngrok.io/api/find_course?code=${text}`,
  33. headers: {
  34. 'content-type': 'application/json',
  35. Authorization: `Token ${token}`
  36. }
  37. })
  38. response.data.list.map((course)=>{
  39. let oneCourse = {'id': course.id, 'code': course.code}
  40. courses.push(oneCourse)
  41. })
  42. setData(courses)
  43. }
  44. const renderItem = ({ item }) => {
  45. const backgroundColor = item.id === selectedId ? "#e60505" : "#fafbfc";
  46. return (
  47. <Item
  48. item={item}
  49. onPress={() => setSelectedId(item.id)}
  50. style={{ backgroundColor }}
  51. />
  52. );
  53. };
  54. console.log(selectedId)
  55. return (
  56. <View style={{padding: 10}}>
  57. <TextInput
  58. style={styles.searchBar}
  59. placeholder="Search for a course that you've taken"
  60. onChangeText={text=>searchCourses(text)}
  61. // onChangeText={text=>setText(text)}
  62. />
  63. <FlatList
  64. data={data}
  65. renderItem={renderItem}
  66. keyExtractor={(item) => item.id.toString()}
  67. extraData={selectedId}
  68. ItemSeparatorComponent={renderSeparator}
  69. />
  70. </View>
  71. );
  72. };
  73. const styles = StyleSheet.create({
  74. container: {
  75. flex: 1,
  76. marginTop: StatusBar.currentHeight || 0,
  77. },
  78. item: {
  79. padding: 10,
  80. marginVertical: 8,
  81. marginHorizontal: 16,
  82. },
  83. title: {
  84. fontSize: 15,
  85. },
  86. searchBar: {
  87. height: 40,
  88. borderColor: '#000',
  89. borderWidth: 1
  90. }
  91. });
  92. export default AddTakenCourse;