No Description

Search.js 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. import React, { useState } from 'react'
  2. import { Text, View, StyleSheet, FlatList, TouchableOpacity, Button, ScrollView } from 'react-native'
  3. import SelectBox from 'react-native-multi-selectbox'
  4. import { xorBy } from 'lodash'
  5. import firebase from 'firebase'
  6. import { firebaseConfig } from '../../config/firebaseConfig'
  7. if (firebase.apps.length === 0) {
  8. firebase.initializeApp(firebaseConfig)
  9. }
  10. require('firebase/firestore')
  11. // Options data must contain 'item' & 'id' keys
  12. const Tags = [
  13. {
  14. item: 'Presencial',
  15. id: 'PL',
  16. },
  17. {
  18. item: 'Online',
  19. id: 'ON',
  20. },
  21. {
  22. item: 'Individual',
  23. id: 'IL',
  24. },
  25. {
  26. item: 'Grupal',
  27. id: 'GL',
  28. },
  29. {
  30. item: 'Esp',
  31. id: 'ESP',
  32. },
  33. {
  34. item: 'Ing',
  35. id: 'ING',
  36. }
  37. ]
  38. const Days = [
  39. {
  40. item: 'Lunes',
  41. id: 'L',
  42. },
  43. {
  44. item: 'Martes',
  45. id: 'M',
  46. },
  47. {
  48. item: 'Miercoles',
  49. id: 'W',
  50. },
  51. {
  52. item: 'Jueves',
  53. id: 'J',
  54. },
  55. {
  56. item: 'Viernes',
  57. id: 'V',
  58. },
  59. {
  60. item: 'Sabado',
  61. id: 'S',
  62. },
  63. {
  64. item: 'Domingo',
  65. id: 'D',
  66. },
  67. {
  68. item: 'Feriados',
  69. id: 'F',
  70. }
  71. ]
  72. function Search({ navigation }) {
  73. const [users, setUsers] = useState([])
  74. const [selectedTags, setSelectedTags] = useState([])
  75. const [selectedDays, setSelectedDays] = useState([])
  76. const fetchUsers = (tags, days) => {
  77. //if the array that have the interpreter tags
  78. //and the day tags are empty we dont send the query seeing as the user
  79. //is not searching for anything yet
  80. if (tags.length === 0 && days.length === 0) {
  81. return
  82. }
  83. //reset the users usestate to empty
  84. const users = []
  85. const db = firebase.firestore()
  86. var query = db.collection('Interprete')
  87. for (let i = 0; i < tags.length; i++) {
  88. //check which tags where called in the search
  89. if (tags[i].id === 'PL') {
  90. console.log('Presencial')
  91. query = query.where('Modalidad.Presencial', '==', true)
  92. }
  93. if (tags[i].id == 'ON') {
  94. query = query.where('Modalidad.Online', '==', true)
  95. }
  96. if (tags[i].id == 'IL') {
  97. query = query.where('Cantidad.Individual', '==', true)
  98. }
  99. if (tags[i].id == 'GL') {
  100. query = query.where('Cantidad.Grupal', '==', true)
  101. }
  102. if (tags[i].id == 'ESP') {
  103. query = query.where('Lenguaje.Esp', '==', true)
  104. }
  105. if (tags[i].id == 'ING') {
  106. query = query.where('Lenguaje.Ing', '==', true)
  107. }
  108. }
  109. for(let i = 0; i < days.length; i++) {
  110. if (days[i].id === 'L') {
  111. query = query.where('dias.Lunes', '==', true)
  112. }
  113. if (days[i].id === 'M') {
  114. query = query.where('dias.Martes', '==', true)
  115. }
  116. if (days[i].id === 'W') {
  117. query = query.where('dias.Miercoles', '==', true)
  118. }
  119. if (days[i].id === 'J') {
  120. query = query.where('dias.Jueves', '==', true)
  121. }
  122. if (days[i].id === 'V') {
  123. query = query.where('dias.Viernes', '==', true)
  124. }
  125. if (days[i].id === 'S') {
  126. query = query.where('dias.Sabado', '==', true)
  127. }
  128. if (days[i].id === 'D') {
  129. query = query.where('dias.Domingo', '==', true)
  130. }
  131. if (days[i].id === 'F') {
  132. query = query.where('dias.Feriados', '==', true)
  133. }
  134. }
  135. query.get().then(querySnapshot => {
  136. console.log('Total users: ', querySnapshot.size);
  137. //traverse the query snapshot
  138. //add the user to the users array
  139. querySnapshot.forEach(documentSnapshot => {
  140. //save the user id and the user data
  141. const user = {
  142. id: documentSnapshot.id,
  143. data: documentSnapshot.data()
  144. }
  145. users.push(user)
  146. setUsers(users)
  147. });
  148. });
  149. console.log('These are the users after the query is executed: ', users)
  150. }
  151. return (
  152. <ScrollView>
  153. <View style={{ margin: 30 }}>
  154. <View style={{ width: '100%', alignItems: 'center' }}>
  155. <Text style={{ fontSize: 30, paddingBottom: 20 }}>Busqueda</Text>
  156. </View>
  157. <Text style={{ fontSize: 20, paddingBottom: 10 }}>Filtro de Interpretes</Text>
  158. <SelectBox
  159. arrowIconColor='#E4CD05'
  160. toggleIconColor='#E4CD05'
  161. searchIconColor='#E4CD05'
  162. multiOptionContainerStyle = {{
  163. backgroundColor: '#E4CD05',
  164. borderColor: '#E4CD05',
  165. borderWidth: 1,
  166. borderRadius: 5,
  167. marginBottom: 10,
  168. marginTop: 10,
  169. padding: 10,
  170. }}
  171. multiOptionsLabelStyle = {{
  172. color: '#000000',
  173. fontSize: 20,
  174. }}
  175. label="Select multiple"
  176. options={Tags}
  177. selectedValues={selectedTags}
  178. onMultiSelect={onMultiChange()}
  179. onTapClose={onMultiChange()}
  180. isMulti
  181. />
  182. <Text style={{ fontSize: 20, paddingBottom: 10 }}>Dias Disponibles</Text>
  183. <SelectBox
  184. arrowIconColor='#E4CD05'
  185. toggleIconColor='#E4CD05'
  186. searchIconColor='#E4CD05'
  187. multiOptionContainerStyle = {{
  188. backgroundColor: '#E4CD05',
  189. borderColor: '#E4CD05',
  190. borderWidth: 1,
  191. borderRadius: 5,
  192. marginBottom: 10,
  193. marginTop: 10,
  194. padding: 10,
  195. }}
  196. multiOptionsLabelStyle = {{
  197. color: '#000000',
  198. fontSize: 20,
  199. }}
  200. label="Select multiple"
  201. options={Days}
  202. selectedValues={selectedDays}
  203. onMultiSelect={(item) => setSelectedDays(xorBy(selectedDays, [item], 'id'))}
  204. onTapClose={(item) => setSelectedDays(xorBy(selectedDays, [item], 'id'))}
  205. isMulti
  206. />
  207. {/* button that will fetch the users */}
  208. <Button title='Buscar' onPress={() => fetchUsers(selectedTags, selectedDays)}/>
  209. <FlatList
  210. numColumns={1}
  211. horizontal={false}
  212. data={users}
  213. renderItem={({ item, }) => {
  214. return (
  215. <View>
  216. <View style={{ flexDirection: "row", paddingBottom: 20, paddingTop: 20, justifyContent: "space-between" }}>
  217. <Text>{item.data.name}</Text>
  218. <Text>{item.data.Precio}</Text>
  219. <Button title='Buscar' onPress={() => { dothing(item.id) }}/>
  220. </View>
  221. <View style={{ borderBottomColor: 'black', borderBottomWidth: 1 }} />
  222. </View>
  223. )
  224. }}
  225. />
  226. </View>
  227. </ScrollView>
  228. );
  229. function onMultiChange() {
  230. return (item) => setSelectedTags(xorBy(selectedTags, [item], 'id'))
  231. }
  232. function dothing(item) {
  233. console.log('This is the item id: ', item)
  234. navigation.navigate('Calendar', {Intereprete_id: item})
  235. }
  236. }
  237. const styles = StyleSheet.create({
  238. button: {
  239. alignItems: "center",
  240. backgroundColor: 'transparent'
  241. },
  242. });
  243. export default Search