PlaceDetails.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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 { useMockState } from '../contexts/MockContext';
  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 {dispatch, state} = useMockState();
  12. const [ownedByUser, setOwned] = useState(state.username === state.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(state.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. dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
  60. if (place.id === state.selectedPlace.id) {
  61. const newTip = {
  62. id: uuidv4(),
  63. dateAdded: new Date(),
  64. name: "cdmoss",
  65. text: commentText,
  66. rating: 0,
  67. }
  68. currentPlace = {...place, tips: [...place.tips, newTip]};
  69. dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  70. return currentPlace;
  71. }
  72. return place;
  73. })});
  74. setComment('');
  75. Keyboard.dismiss();
  76. }
  77. const setSelectedPlace = (place) => {
  78. dispatch({type: "UPDATE_SELECTED_PLACE", payload: {
  79. id: place.id,
  80. name: place.properties.name,
  81. desc: place.properties.desc,
  82. tips: place.properties.tips,
  83. dateAdded: place.properties.dateAdded,
  84. postedBy: place.properties.postedBy,
  85. rating: place.properties.rating,
  86. }});
  87. }
  88. const likeTip = (tip) => {
  89. console.log("Tip rating before: " + tip.rating);
  90. let currentPlace = {};
  91. let currentTip = {};
  92. dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
  93. if (place.id == state.selectedPlace.id) {
  94. currentPlace = {...place, tips:
  95. place.tips.map(t => {
  96. if (t.id == tip.id) {
  97. currentTip = {...t, rating: t.rating + 1, liked: true};
  98. console.log("Tip rating after " + currentTip.rating);
  99. return currentTip;
  100. }
  101. return t;
  102. }
  103. )}
  104. dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  105. return currentPlace;
  106. }
  107. return place;
  108. })})
  109. }
  110. const unlikeTip = (tip) => {
  111. console.log('test')
  112. let currentPlace = {};
  113. let currentTip = {};
  114. dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
  115. if (place.id == state.selectedPlace.id) {
  116. currentPlace = {...place, tips:
  117. place.tips.map(t => {
  118. if (t.id == tip.id) {
  119. currentTip = {...t, rating: t.rating - 1, liked: false};
  120. console.log("Tip rating after " + currentTip.rating);
  121. return currentTip;
  122. }
  123. return t;
  124. }
  125. )}
  126. dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
  127. return currentPlace;
  128. }
  129. return place;
  130. })})
  131. }
  132. const notifyError = (errors) => {
  133. Alert.alert('Comment must include text.');
  134. }
  135. return (
  136. <View style={{backgroundColor: '#df3f3f',}}>
  137. <View style={{flexDirection: 'row', height: 35, justifyContent: 'space-between', alignContent: 'center', borderBottomColor: 'white', borderBottomWidth: 1}}>
  138. <Icon.Button name="times" style={{width: 40, backgroundColor: '#df3f3f', }} onPress={closeModal}/>
  139. {state.selectedPlaces.length > 1 ?
  140. <Menu
  141. ref={placePicker}
  142. contentStyle={{backgroundColor: 'white', zIndex: 10}}anchor
  143. button={
  144. <TouchableOpacity style={{flexDirection: 'row', height: '100%', alignItems: 'center'}} onPress={() => placePicker.current.show()}>
  145. <Text style={{color: 'white'}}>{state.selectedPlace.name}</Text>
  146. <Icon style={{marginLeft: 5, color: 'white'}} name="chevron-down" />
  147. </TouchableOpacity>}>
  148. {state.selectedPlaces.map(p => {
  149. return(
  150. <MenuItem
  151. key={p.id}
  152. onPress={() => {
  153. setSelectedPlace(p);
  154. placePicker.current.hide();
  155. }} >{p.properties.name}</MenuItem>
  156. )
  157. })}
  158. </Menu> : <Text style={{textAlignVertical: 'center', color: 'white'}}>{state.selectedPlaces.map(p => p.properties.name)[0]}</Text> }
  159. <Menu
  160. ref={placeMenu}
  161. button={<Icon.Button onPress={() => placeMenu.current.show()} name="ellipsis-h" style={{paddingLeft: 10, paddingRight: 0, backgroundColor: '#df3f3f'}} />}>
  162. <MenuItem onPress={() => {
  163. addLandmark();
  164. placeMenu.current.hide();
  165. }} >Add landmark here</MenuItem>
  166. <MenuItem onPress={() => {
  167. placeMenu.current.hide()
  168. }} >Cancel</MenuItem>
  169. </Menu>
  170. </View>
  171. <View style={{margin: 20}}>
  172. <Text style={{marginBottom: 20, fontSize: 15, color: 'white'}}>Tips</Text>
  173. <View style={{backgroundColor: 'white',}}>
  174. <View style={{height: 220}}>
  175. <ScrollView>
  176. <TouchableOpacity activeOpacity={1}>
  177. {state.selectedPlace.tips ?
  178. state.selectedPlace.tips.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(tip => {
  179. console.log(tip.dateAdded)
  180. return(
  181. <View style={styles.commentContainer} key={tip.id}>
  182. <View style={styles.commentHeader}>
  183. <Text style={{fontWeight: 'bold'}} >{tip.name}</Text>
  184. <Text style={{fontSize: 12, color: 'lightgrey'}}>{tip.dateAdded.toString()}</Text>
  185. </View>
  186. <Text>{tip.text}</Text>
  187. <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
  188. <Text style={{padding: 5, textAlign: 'right', color: 'black'}}>{tip.rating}</Text>
  189. {tip.liked ?
  190. <View style={{flexDirection: 'row', alignItems: 'center'}}>
  191. <Icon size={20} style={{alignSelf: 'center', marginLeft: 2, opacity: .5, zIndex: 5}} color="black" name="thumbs-up"/>
  192. <Text style={{alignSelf: 'center', marginLeft: 5, opacity: .5, zIndex: 5, color: "black"}} >You like this</Text>
  193. <TouchableOpacity style={{marginLeft: 5}} onPress={() => unlikeTip(tip)}>
  194. <Text style={{color: '#A9A9A9'}}>(Unlike)</Text>
  195. </TouchableOpacity>
  196. </View> :
  197. <TouchableOpacity style={{justifyContent: 'center', }}>
  198. <Icon size={20} style={{marginLeft: 2, zIndex: 5}} onPress={() => likeTip(tip)} color="black" name="thumbs-up"/>
  199. </TouchableOpacity>}
  200. </View>
  201. </View>
  202. );
  203. }) :
  204. <Text>There are no tips for this place.</Text>}
  205. </TouchableOpacity>
  206. </ScrollView>
  207. </View>
  208. <View style={[styles.commentInputContainer, commentInputOffset]}>
  209. <TextInput
  210. ref={commentInput}
  211. multiline={true}
  212. style={[styles.addComment]}
  213. placeholder="Add tip..."
  214. onChangeText={text => setComment(text)}
  215. value={commentText}
  216. ></TextInput>
  217. {keyboardShown && commentText != '' ?
  218. <View style={styles.postBtn}>
  219. <Icon.Button
  220. name='paper-plane'
  221. color='black'
  222. style={{backgroundColor: 'white', margin: 0}}
  223. onPress={postComment}/>
  224. </View> : null }
  225. </View>
  226. </View>
  227. </View>
  228. </View>
  229. )
  230. }
  231. const styles = StyleSheet.create({
  232. menuModal: {
  233. backgroundColor: '#df3f3f',
  234. height: 400,
  235. },
  236. commentContainer: {
  237. margin: 10,
  238. },
  239. commentHeader: {
  240. flexDirection: 'row',
  241. justifyContent: 'space-between'
  242. },
  243. commentBody: {
  244. padding: 10
  245. },
  246. commentInputContainer: {
  247. marginHorizontal: 10,
  248. borderTopWidth: 1,
  249. borderColor: 'grey',
  250. justifyContent: 'space-between',
  251. backgroundColor: 'white',
  252. flexDirection: 'row',
  253. },
  254. addComment: {
  255. width: '80%',
  256. backgroundColor: 'white',
  257. },
  258. postBtn: {
  259. marginVertical: 10,
  260. justifyContent: 'center',
  261. alignItems: 'center',
  262. width: '20%'
  263. },
  264. })
  265. export default PlaceDetails;