LandmarkDetails.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import React, { useState, useEffect, useRef } from 'react';
  2. import { TextInput, Image, ScrollView, View, Text, StyleSheet, TouchableOpacity, Keyboard, Alert, Dimensions} from 'react-native';
  3. import { format } from 'date-fns';
  4. import { Controller, useForm } from 'react-hook-form';
  5. import { useMapState } from '../contexts/MapContext';
  6. import Icon from 'react-native-vector-icons/FontAwesome'
  7. import { Icons } from '../globals';
  8. import { v4 as uuidv4 } from 'uuid';
  9. const LandmarkDetails = ({closeModal, editLandmark}) => {
  10. const {mapState, mapDispatch} = useMapState();
  11. const [ownedByUser, setOwned] = useState(mapState.username === mapState.selectedLandmark.postedBy);
  12. const [keyboardShown, setKeyboard] = useState(false);
  13. const [commentText, setComment] = useState('');
  14. const [commentPosted, setPosted] = useState(false);
  15. const [commentInputOffset, setOffset] = useState({});
  16. const commentInput = useRef()
  17. const _keyboardDidShow = (e) => {
  18. setKeyboard(true);
  19. }
  20. const _keyboardDidHide = (e) => {
  21. setKeyboard(false);
  22. setOffset({});
  23. commentInput.current.blur();
  24. console.log(commentPosted)
  25. console.log(commentText)
  26. // only show alert if comment wasn't posted
  27. if (!commentPosted && commentText != '') {
  28. Alert.alert(
  29. 'Discard comment?',
  30. '',
  31. [
  32. {
  33. text: 'Keep writing', onPress: () => commentInput.current.focus(),
  34. },
  35. {
  36. text: 'Discard', onPress: () => {
  37. setComment('');
  38. setOffset({});
  39. setPosted(false);
  40. },
  41. style: 'cancel'
  42. }
  43. ]
  44. )
  45. }
  46. }
  47. useEffect(() => {
  48. Keyboard.addListener('keyboardDidShow', _keyboardDidShow);
  49. Keyboard.addListener('keyboardDidHide', _keyboardDidHide);
  50. return () => {
  51. Keyboard.removeListener('keyboardDidShow', _keyboardDidShow);
  52. Keyboard.removeListener('keyboardDidHide', _keyboardDidHide);
  53. }
  54. })
  55. const postComment = () => {
  56. let currentLandmark;
  57. // api call
  58. // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
  59. // if (l.id === mapState.selectedLandmark.id) {
  60. // const newComment = {
  61. // id: uuidv4(),
  62. // dateAdded: new Date(),
  63. // name: "cdmoss",
  64. // text: commentText
  65. // }
  66. // currentLandmark = {...l, comments: [...l.comments, newComment]};
  67. // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: currentLandmark});
  68. // return currentLandmark;
  69. // }
  70. // return l;
  71. // })});
  72. setComment('');
  73. Keyboard.dismiss();
  74. }
  75. const notifyError = (errors) => {
  76. Alert.alert('Comment must include text.');
  77. }
  78. const endorseLandmark = () => {
  79. // api call
  80. // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
  81. // if (l.id == mapState.selectedLandmark.id) {
  82. // const newRating = l.rating + 1;
  83. // const updatedLandmark = {...l, rating: newRating};
  84. // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
  85. // return updatedLandmark;
  86. // }
  87. // return l;
  88. // })})
  89. }
  90. const removeEndorsement = () => {
  91. //api call
  92. // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
  93. // if (l.id == mapState.selectedLandmark.id) {
  94. // const newRating = l.rating - 1;
  95. // const updatedLandmark = {...l, rating: newRating};
  96. // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
  97. // return updatedLandmark;
  98. // }
  99. // return l;
  100. // })})
  101. }
  102. const deleteLandmark = () => {
  103. //api call
  104. // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.filter(l => l.id !== mapState.selectedLandmark.id)});
  105. // closeModal('Successfully deleted landmark.');
  106. }
  107. return (
  108. <View style={styles.container}>
  109. <View style={styles.detailsHeader}>
  110. <Icon.Button style={styles.headerBtn} onPress={() => closeModal()} name="times"/>
  111. <Text style={styles.rating}><Icon style={styles.headerBtn} name="thumbs-up" size={18}/> {mapState.selectedLandmark.rating}</Text>
  112. {ownedByUser ?
  113. <View style={{flexDirection: 'row'}}>
  114. <Icon.Button style={styles.headerBtn} onPress={editLandmark} name="edit"/>
  115. <Icon.Button style={styles.headerBtn} onPress={deleteLandmark} name="trash"/>
  116. </View>
  117. : null}
  118. {!ownedByUser && mapState.selectedLandmark.rating == 0 ? <Icon.Button style={styles.headerBtn} onPress={endorseLandmark} name="thumbs-up"/> : null}
  119. {!ownedByUser && mapState.selectedLandmark.rating > 0 ?
  120. <View style={{flexDirection: 'row'}}>
  121. <Text style={{marginRight: 5, color: 'white'}}>
  122. Liked by you
  123. </Text>
  124. <TouchableOpacity onPress={removeEndorsement} style={{alignItems: 'center', padding: 0}}>
  125. <Text style={{marginRight: 10, color: '#A9A9A9'}}>(Unlike)</Text>
  126. </TouchableOpacity>
  127. </View> : null}
  128. </View>
  129. <View style={styles.titleContainer}>
  130. <Text style={styles.title}>{mapState.selectedLandmark.title}</Text>
  131. <View style={{backgroundColor: 'white', borderRadius: 25, height: 40, width: 40 ,justifyContent: 'center', alignItems: 'center'}}>
  132. <Image style={styles.titleIcon} source={Icons[mapState.selectedLandmark.icon]} />
  133. </View>
  134. </View>
  135. <View style={styles.detailsContainer}>
  136. <ScrollView><Text style={styles.desc}>{mapState.selectedLandmark.desc}</Text></ScrollView>
  137. <Text style={styles.date}>{format(mapState.selectedLandmark.dateAdded, "MMMM do, yyyy h:mma")}</Text>
  138. <Text style={{fontSize: 15, color: 'white'}}>Comments:</Text>
  139. <View style={styles.commentsBox}>
  140. <ScrollView>
  141. <TouchableOpacity>
  142. {mapState.selectedLandmark.comments.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(comment => {
  143. return(
  144. <View style={styles.commentContainer} key={comment.id}>
  145. <View style={styles.commentHeader}>
  146. <Text style={{fontWeight: 'bold'}} >{comment.name}</Text>
  147. <Text style={{fontSize: 12, color: 'lightgrey'}}>{format(comment.dateAdded, "MMMM do, yyyy h:mma")}</Text>
  148. </View>
  149. <Text style={{padding: 10}}>{comment.text}</Text>
  150. </View>
  151. );
  152. })}
  153. </TouchableOpacity>
  154. </ScrollView>
  155. <View style={styles.commentInputContainer}>
  156. <TextInput
  157. ref={commentInput}
  158. multiline={true}
  159. style={[styles.addComment]}
  160. placeholder="Add comment..."
  161. onChangeText={text => setComment(text)}
  162. value={commentText}
  163. ></TextInput>
  164. {keyboardShown && commentText != '' ?
  165. <View style={styles.postBtn}>
  166. <Icon.Button
  167. name='paper-plane'
  168. color='black'
  169. style={{backgroundColor: 'white'}}
  170. onPress={postComment}/>
  171. </View> : null }
  172. </View>
  173. </View>
  174. </View>
  175. </View>
  176. )
  177. }
  178. const styles = StyleSheet.create({
  179. detailsHeader: {
  180. borderBottomWidth: 1,
  181. borderColor: 'lightgrey',
  182. flexDirection: 'row',
  183. justifyContent: 'space-between',
  184. alignItems: 'center',
  185. backgroundColor: '#df3f3f',
  186. marginBottom: 10,
  187. },
  188. headerBtn: {
  189. backgroundColor: '#df3f3f',
  190. padding: 10,
  191. justifyContent: 'center',
  192. },
  193. container: {
  194. backgroundColor: '#df3f3f',
  195. },
  196. detailsContainer: {
  197. marginHorizontal: 20
  198. },
  199. titleContainer: {
  200. marginHorizontal: 20,
  201. marginBottom: 20,
  202. flexDirection: 'row',
  203. justifyContent: 'space-between'
  204. },
  205. titleIcon: {
  206. height: 30,
  207. width: 22,
  208. },
  209. title: {
  210. flex: 1,
  211. flexWrap: 'wrap',
  212. color: 'white',
  213. fontSize: 20,
  214. marginBottom: 20,
  215. marginRight: 20,
  216. },
  217. rating: {
  218. color: 'white',
  219. },
  220. desc: {
  221. color: 'white',
  222. height: 50,
  223. },
  224. date: {
  225. color: 'lightgray',
  226. marginBottom: 10,
  227. alignSelf: "flex-end",
  228. },
  229. commentsBox: {
  230. height: 275,
  231. backgroundColor: 'white',
  232. marginTop: 10,
  233. marginBottom: 20,
  234. borderColor: 'black',
  235. },
  236. commentContainer: {
  237. padding: 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 LandmarkDetails;