PlaceDetails.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { Button, TextInput, Image, ScrollView, View, Text, StyleSheet, TouchableOpacity, Keyboard, Alert, Dimensions} from 'react-native';
  3. import { format } from 'date-fns';
  4. import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu';
  5. import { Controller, useForm } from 'react-hook-form';
  6. import { useMapState } from '../contexts/MapContext';
  7. import Icon from 'react-native-vector-icons/FontAwesome'
  8. import { Icons } from '../globals';
  9. import { v4 as uuidv4 } from 'uuid';
  10. const PlaceDetails = ({closeModal, addLandmark}) => {
  11. const {mapState, mapDispatch} = useMapState();
  12. const [ownedByUser, setOwned] = useState(mapState.username === mapState.selectedLandmark.postedBy);
  13. const [keyboardShown, setKeyboard] = useState(false);
  14. const [commentText, setComment] = useState('');
  15. const [commentPosted, setPosted] = useState(false);
  16. const [commentInputOffset, setOffset] = useState({});
  17. const placePicker = useRef();
  18. const placeMenu = useRef();
  19. const commentInput = useRef()
  20. const _keyboardDidShow = (e) => {
  21. setKeyboard(true);
  22. console.log(mapState.selectedPlace);
  23. }
  24. const _keyboardDidHide = (e) => {
  25. setKeyboard(false);
  26. setOffset({});
  27. commentInput.current.blur();
  28. // only show alert if comment wasn't posted
  29. if (!commentPosted && commentText != '') {
  30. Alert.alert(
  31. 'Discard comment?',
  32. '',
  33. [
  34. {
  35. text: 'Keep writing', onPress: () => commentInput.current.focus(),
  36. },
  37. {
  38. text: 'Discard', onPress: () => {
  39. setComment('');
  40. setOffset({});
  41. setPosted(false);
  42. },
  43. style: 'cancel'
  44. }
  45. ]
  46. )
  47. }
  48. }
  49. useEffect(() => {
  50. Keyboard.addListener('keyboardDidShow', _keyboardDidShow);
  51. Keyboard.addListener('keyboardDidHide', _keyboardDidHide);
  52. return () => {
  53. Keyboard.removeListener('keyboardDidShow', _keyboardDidShow);
  54. Keyboard.removeListener('keyboardDidHide', _keyboardDidHide);
  55. }
  56. })
  57. const postComment = () => {
  58. let currentPlace;
  59. // api call
  60. // dispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => {
  61. // if (place.id === mapState.selectedPlace.id) {
  62. // const newTip = {
  63. // id: uuidv4(),
  64. // dateAdded: new Date(),
  65. // name: "cdmoss",
  66. // text: commentText,
  67. // rating: 0,
  68. // }
  69. // currentPlace = {...place, tips: [...place.tips, newTip]};
  70. // dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  71. // return currentPlace;
  72. // }
  73. // return place;
  74. // })});
  75. setComment('');
  76. Keyboard.dismiss();
  77. }
  78. const setSelectedPlace = (place) => {
  79. mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: {
  80. id: place.id,
  81. name: place.properties.name,
  82. desc: place.properties.desc,
  83. tips: place.properties.tips,
  84. dateAdded: place.properties.dateAdded,
  85. postedBy: place.properties.postedBy,
  86. rating: place.properties.rating,
  87. }});
  88. }
  89. const likeTip = (tip) => {
  90. console.log("Tip rating before: " + tip.rating);
  91. let currentPlace = {};
  92. let currentTip = {};
  93. // api call
  94. // mapDispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => {
  95. // if (place.id == mapState.selectedPlace.id) {
  96. // currentPlace = {...place, tips:
  97. // place.tips.map(t => {
  98. // if (t.id == tip.id) {
  99. // currentTip = {...t, rating: t.rating + 1, liked: true};
  100. // console.log("Tip rating after " + currentTip.rating);
  101. // return currentTip;
  102. // }
  103. // return t;
  104. // }
  105. // )}
  106. // mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  107. // return currentPlace;
  108. // }
  109. // return place;
  110. // })})
  111. }
  112. const unlikeTip = (tip) => {
  113. console.log('test')
  114. let currentPlace = {};
  115. let currentTip = {};
  116. //api call
  117. // dispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => {
  118. // if (place.id == mapState.selectedPlace.id) {
  119. // currentPlace = {...place, tips:
  120. // place.tips.map(t => {
  121. // if (t.id == tip.id) {
  122. // currentTip = {...t, rating: t.rating - 1, liked: false};
  123. // console.log("Tip rating after " + currentTip.rating);
  124. // return currentTip;
  125. // }
  126. // return t;
  127. // }
  128. // )}
  129. // mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  130. // return currentPlace;
  131. // }
  132. // return place;
  133. // })})
  134. }
  135. const notifyError = (errors) => {
  136. Alert.alert('Comment must include text.');
  137. }
  138. return (
  139. <View style={{backgroundColor: '#df3f3f',}}>
  140. <View style={{flexDirection: 'row', height: 35, justifyContent: 'space-between', alignContent: 'center', borderBottomColor: 'white', borderBottomWidth: 1}}>
  141. <Icon.Button name="times" style={{width: 40, backgroundColor: '#df3f3f', }} onPress={closeModal}/>
  142. {mapState.selectedPlaces.length > 1 ?
  143. <Menu
  144. ref={placePicker}
  145. contentStyle={{backgroundColor: 'white', zIndex: 10}}anchor
  146. button={
  147. <TouchableOpacity style={{flexDirection: 'row', height: '100%', alignItems: 'center'}} onPress={() => placePicker.current.show()}>
  148. <Text style={{color: 'white'}}>{mapState.selectedPlace.name}</Text>
  149. <Icon style={{marginLeft: 5, color: 'white'}} name="chevron-down" />
  150. </TouchableOpacity>}>
  151. {mapState.selectedPlaces.map(p => {
  152. return(
  153. <MenuItem
  154. key={p.id}
  155. onPress={() => {
  156. setSelectedPlace(p);
  157. placePicker.current.hide();
  158. }} >{p.properties.name}</MenuItem>
  159. )
  160. })}
  161. </Menu> : <Text style={{textAlignVertical: 'center', color: 'white'}}>{mapState.selectedPlaces.map(p => p.properties.name)[0]}</Text> }
  162. <Menu
  163. ref={placeMenu}
  164. button={<Icon.Button onPress={() => placeMenu.current.show()} name="ellipsis-h" style={{paddingLeft: 10, paddingRight: 0, backgroundColor: '#df3f3f'}} />}>
  165. <MenuItem onPress={() => {
  166. addLandmark();
  167. placeMenu.current.hide();
  168. }} >Add landmark here</MenuItem>
  169. <MenuItem onPress={() => {
  170. placeMenu.current.hide()
  171. }} >Cancel</MenuItem>
  172. </Menu>
  173. </View>
  174. <View style={{margin: 20}}>
  175. <Text style={{marginBottom: 20, fontSize: 15, color: 'white'}}>Tips</Text>
  176. <View style={{backgroundColor: 'white',}}>
  177. <View style={{height: 220}}>
  178. <ScrollView>
  179. <TouchableOpacity activeOpacity={1}>
  180. {mapState.selectedPlace.tips ?
  181. mapState.selectedPlace.tips.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(tip => {
  182. console.log(tip.dateAdded)
  183. return(
  184. <View style={styles.commentContainer} key={tip.id}>
  185. <View style={styles.commentHeader}>
  186. <Text style={{fontWeight: 'bold'}} >{tip.name}</Text>
  187. <Text style={{fontSize: 12, color: 'lightgrey'}}>{tip.dateAdded.toString()}</Text>
  188. </View>
  189. <Text>{tip.text}</Text>
  190. <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
  191. <Text style={{padding: 5, textAlign: 'right', color: 'black'}}>{tip.rating}</Text>
  192. {tip.liked ?
  193. <View style={{flexDirection: 'row', alignItems: 'center'}}>
  194. <Icon size={20} style={{alignSelf: 'center', marginLeft: 2, opacity: .5, zIndex: 5}} color="black" name="thumbs-up"/>
  195. <Text style={{alignSelf: 'center', marginLeft: 5, opacity: .5, zIndex: 5, color: "black"}} >You like this</Text>
  196. <TouchableOpacity style={{marginLeft: 5}} onPress={() => unlikeTip(tip)}>
  197. <Text style={{color: '#A9A9A9'}}>(Unlike)</Text>
  198. </TouchableOpacity>
  199. </View> :
  200. <TouchableOpacity style={{justifyContent: 'center', }}>
  201. <Icon size={20} style={{marginLeft: 2, zIndex: 5}} onPress={() => likeTip(tip)} color="black" name="thumbs-up"/>
  202. </TouchableOpacity>}
  203. </View>
  204. </View>
  205. );
  206. }) :
  207. <Text>There are no tips for this place.</Text>}
  208. </TouchableOpacity>
  209. </ScrollView>
  210. </View>
  211. <View style={[styles.commentInputContainer, commentInputOffset]}>
  212. <TextInput
  213. ref={commentInput}
  214. multiline={true}
  215. style={[styles.addComment]}
  216. placeholder="Add tip..."
  217. onChangeText={text => setComment(text)}
  218. value={commentText}
  219. ></TextInput>
  220. {keyboardShown && commentText != '' ?
  221. <View style={styles.postBtn}>
  222. <Icon.Button
  223. name='paper-plane'
  224. color='black'
  225. style={{backgroundColor: 'white', margin: 0}}
  226. onPress={postComment}/>
  227. </View> : null }
  228. </View>
  229. </View>
  230. </View>
  231. </View>
  232. )
  233. }
  234. const styles = StyleSheet.create({
  235. menuModal: {
  236. backgroundColor: '#df3f3f',
  237. height: 400,
  238. },
  239. commentContainer: {
  240. margin: 10,
  241. },
  242. commentHeader: {
  243. flexDirection: 'row',
  244. justifyContent: 'space-between'
  245. },
  246. commentBody: {
  247. padding: 10
  248. },
  249. commentInputContainer: {
  250. marginHorizontal: 10,
  251. borderTopWidth: 1,
  252. borderColor: 'grey',
  253. justifyContent: 'space-between',
  254. backgroundColor: 'white',
  255. flexDirection: 'row',
  256. },
  257. addComment: {
  258. width: '80%',
  259. backgroundColor: 'white',
  260. },
  261. postBtn: {
  262. marginVertical: 10,
  263. justifyContent: 'center',
  264. alignItems: 'center',
  265. width: '20%'
  266. },
  267. })
  268. export default PlaceDetails;