LandmarkDetails.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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 { useMockState } from '../contexts/MockContext';
  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 {dispatch, state} = useMockState();
  11. const [ownedByUser, setOwned] = useState(state.username === state.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. dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => {
  58. if (l.id === state.selectedLandmark.id) {
  59. const newComment = {
  60. id: uuidv4(),
  61. dateAdded: new Date(),
  62. name: "cdmoss",
  63. text: commentText
  64. }
  65. currentLandmark = {...l, comments: [...l.comments, newComment]};
  66. dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: currentLandmark});
  67. return currentLandmark;
  68. }
  69. return l;
  70. })});
  71. setComment('');
  72. Keyboard.dismiss();
  73. }
  74. const notifyError = (errors) => {
  75. Alert.alert('Comment must include text.');
  76. }
  77. const endorseLandmark = () => {
  78. dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => {
  79. if (l.id == state.selectedLandmark.id) {
  80. const newRating = l.rating + 1;
  81. const updatedLandmark = {...l, rating: newRating};
  82. dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
  83. return updatedLandmark;
  84. }
  85. return l;
  86. })})
  87. }
  88. const removeEndorsement = () => {
  89. dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => {
  90. if (l.id == state.selectedLandmark.id) {
  91. const newRating = l.rating - 1;
  92. const updatedLandmark = {...l, rating: newRating};
  93. dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
  94. return updatedLandmark;
  95. }
  96. return l;
  97. })})
  98. }
  99. const deleteLandmark = () => {
  100. dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.filter(l => l.id !== state.selectedLandmark.id)});
  101. closeModal('Successfully deleted landmark.');
  102. }
  103. return (
  104. <View style={styles.container}>
  105. <View style={styles.detailsHeader}>
  106. <Icon.Button style={styles.headerBtn} onPress={() => closeModal()} name="times"/>
  107. <Text style={styles.rating}><Icon style={styles.headerBtn} name="thumbs-up" size={18}/> {state.selectedLandmark.rating}</Text>
  108. {ownedByUser ?
  109. <View style={{flexDirection: 'row'}}>
  110. <Icon.Button style={styles.headerBtn} onPress={editLandmark} name="edit"/>
  111. <Icon.Button style={styles.headerBtn} onPress={deleteLandmark} name="trash"/>
  112. </View>
  113. : null}
  114. {!ownedByUser && state.selectedLandmark.rating == 0 ? <Icon.Button style={styles.headerBtn} onPress={endorseLandmark} name="thumbs-up"/> : null}
  115. {!ownedByUser && state.selectedLandmark.rating > 0 ?
  116. <View style={{flexDirection: 'row'}}>
  117. <Text style={{marginRight: 5, color: 'white'}}>
  118. Liked by you
  119. </Text>
  120. <TouchableOpacity onPress={removeEndorsement} style={{alignItems: 'center', padding: 0}}>
  121. <Text style={{marginRight: 10, color: '#A9A9A9'}}>(Unlike)</Text>
  122. </TouchableOpacity>
  123. </View> : null}
  124. </View>
  125. <View style={styles.titleContainer}>
  126. <Text style={styles.title}>{state.selectedLandmark.title}</Text>
  127. <View style={{backgroundColor: 'white', borderRadius: 25, height: 40, width: 40 ,justifyContent: 'center', alignItems: 'center'}}>
  128. <Image style={styles.titleIcon} source={Icons[state.selectedLandmark.icon]} />
  129. </View>
  130. </View>
  131. <View style={styles.detailsContainer}>
  132. <ScrollView><Text style={styles.desc}>{state.selectedLandmark.desc}</Text></ScrollView>
  133. <Text style={styles.date}>{format(state.selectedLandmark.dateAdded, "MMMM do, yyyy h:mma")}</Text>
  134. <Text style={{fontSize: 15, color: 'white'}}>Comments:</Text>
  135. <View style={styles.commentsBox}>
  136. <ScrollView>
  137. <TouchableOpacity>
  138. {state.selectedLandmark.comments.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(comment => {
  139. return(
  140. <View style={styles.commentContainer} key={comment.id}>
  141. <View style={styles.commentHeader}>
  142. <Text style={{fontWeight: 'bold'}} >{comment.name}</Text>
  143. <Text style={{fontSize: 12, color: 'lightgrey'}}>{format(comment.dateAdded, "MMMM do, yyyy h:mma")}</Text>
  144. </View>
  145. <Text style={{padding: 10}}>{comment.text}</Text>
  146. </View>
  147. );
  148. })}
  149. </TouchableOpacity>
  150. </ScrollView>
  151. <View style={styles.commentInputContainer}>
  152. <TextInput
  153. ref={commentInput}
  154. multiline={true}
  155. style={[styles.addComment]}
  156. placeholder="Add comment..."
  157. onChangeText={text => setComment(text)}
  158. value={commentText}
  159. ></TextInput>
  160. {keyboardShown && commentText != '' ?
  161. <View style={styles.postBtn}>
  162. <Icon.Button
  163. name='paper-plane'
  164. color='black'
  165. style={{backgroundColor: 'white'}}
  166. onPress={postComment}/>
  167. </View> : null }
  168. </View>
  169. </View>
  170. </View>
  171. </View>
  172. )
  173. }
  174. const styles = StyleSheet.create({
  175. detailsHeader: {
  176. borderBottomWidth: 1,
  177. borderColor: 'lightgrey',
  178. flexDirection: 'row',
  179. justifyContent: 'space-between',
  180. alignItems: 'center',
  181. backgroundColor: '#df3f3f',
  182. marginBottom: 10,
  183. },
  184. headerBtn: {
  185. backgroundColor: '#df3f3f',
  186. padding: 10,
  187. justifyContent: 'center',
  188. },
  189. container: {
  190. backgroundColor: '#df3f3f',
  191. },
  192. detailsContainer: {
  193. marginHorizontal: 20
  194. },
  195. titleContainer: {
  196. marginHorizontal: 20,
  197. marginBottom: 20,
  198. flexDirection: 'row',
  199. justifyContent: 'space-between'
  200. },
  201. titleIcon: {
  202. height: 30,
  203. width: 22,
  204. },
  205. title: {
  206. flex: 1,
  207. flexWrap: 'wrap',
  208. color: 'white',
  209. fontSize: 20,
  210. marginBottom: 20,
  211. marginRight: 20,
  212. },
  213. rating: {
  214. color: 'white',
  215. },
  216. desc: {
  217. color: 'white',
  218. height: 50,
  219. },
  220. date: {
  221. color: 'lightgray',
  222. marginBottom: 10,
  223. alignSelf: "flex-end",
  224. },
  225. commentsBox: {
  226. height: 275,
  227. backgroundColor: 'white',
  228. marginTop: 10,
  229. marginBottom: 20,
  230. borderColor: 'black',
  231. },
  232. commentContainer: {
  233. padding: 10
  234. },
  235. commentHeader: {
  236. flexDirection: 'row',
  237. justifyContent: 'space-between'
  238. },
  239. commentBody: {
  240. padding: 10
  241. },
  242. commentInputContainer: {
  243. marginHorizontal: 10,
  244. borderTopWidth: 1,
  245. borderColor: 'grey',
  246. justifyContent: 'space-between',
  247. backgroundColor: 'white',
  248. flexDirection: 'row',
  249. },
  250. addComment: {
  251. width: '80%',
  252. backgroundColor: 'white',
  253. },
  254. postBtn: {
  255. marginVertical: 10,
  256. justifyContent: 'center',
  257. alignItems: 'center',
  258. width: '20%'
  259. },
  260. })
  261. export default LandmarkDetails;