123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282 |
- import React, { useState, useEffect, useRef } from 'react';
- import { TextInput, Image, ScrollView, View, Text, StyleSheet, TouchableOpacity, Keyboard, Alert, Dimensions} from 'react-native';
- import { format } from 'date-fns';
- import { Controller, useForm } from 'react-hook-form';
- import { useMapState } from '../contexts/MapContext';
- import Icon from 'react-native-vector-icons/FontAwesome'
- import { Icons } from '../globals';
- import { v4 as uuidv4 } from 'uuid';
- const LandmarkDetails = ({closeModal, editLandmark}) => {
- const {mapState, mapDispatch} = useMapState();
- const [ownedByUser, setOwned] = useState(mapState.username === mapState.selectedLandmark.postedBy);
- const [keyboardShown, setKeyboard] = useState(false);
- const [commentText, setComment] = useState('');
- const [commentPosted, setPosted] = useState(false);
- const [commentInputOffset, setOffset] = useState({});
- const commentInput = useRef()
-
- const _keyboardDidShow = (e) => {
- setKeyboard(true);
- }
- const _keyboardDidHide = (e) => {
- setKeyboard(false);
- setOffset({});
- commentInput.current.blur();
- console.log(commentPosted)
- console.log(commentText)
- // only show alert if comment wasn't posted
- if (!commentPosted && commentText != '') {
-
- Alert.alert(
- 'Discard comment?',
- '',
- [
- {
- text: 'Keep writing', onPress: () => commentInput.current.focus(),
- },
- {
- text: 'Discard', onPress: () => {
- setComment('');
- setOffset({});
- setPosted(false);
- },
- style: 'cancel'
- }
- ]
- )
- }
- }
-
- useEffect(() => {
- Keyboard.addListener('keyboardDidShow', _keyboardDidShow);
- Keyboard.addListener('keyboardDidHide', _keyboardDidHide);
- return () => {
- Keyboard.removeListener('keyboardDidShow', _keyboardDidShow);
- Keyboard.removeListener('keyboardDidHide', _keyboardDidHide);
- }
- })
- const postComment = () => {
- let currentLandmark;
- // api call
- // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
- // if (l.id === mapState.selectedLandmark.id) {
- // const newComment = {
- // id: uuidv4(),
- // dateAdded: new Date(),
- // name: "cdmoss",
- // text: commentText
- // }
- // currentLandmark = {...l, comments: [...l.comments, newComment]};
- // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: currentLandmark});
- // return currentLandmark;
- // }
- // return l;
- // })});
- setComment('');
- Keyboard.dismiss();
- }
- const notifyError = (errors) => {
- Alert.alert('Comment must include text.');
- }
- const endorseLandmark = () => {
- // api call
- // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
- // if (l.id == mapState.selectedLandmark.id) {
- // const newRating = l.rating + 1;
- // const updatedLandmark = {...l, rating: newRating};
- // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
- // return updatedLandmark;
- // }
- // return l;
- // })})
- }
- const removeEndorsement = () => {
- //api call
- // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.map(l => {
- // if (l.id == mapState.selectedLandmark.id) {
- // const newRating = l.rating - 1;
- // const updatedLandmark = {...l, rating: newRating};
- // dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: updatedLandmark});
- // return updatedLandmark;
- // }
- // return l;
- // })})
- }
-
- const deleteLandmark = () => {
- //api call
- // dispatch({type: "UPDATE_LANDMARKS", payload: mapState.landmarks.filter(l => l.id !== mapState.selectedLandmark.id)});
- // closeModal('Successfully deleted landmark.');
- }
- return (
- <View style={styles.container}>
- <View style={styles.detailsHeader}>
- <Icon.Button style={styles.headerBtn} onPress={() => closeModal()} name="times"/>
- <Text style={styles.rating}><Icon style={styles.headerBtn} name="thumbs-up" size={18}/> {mapState.selectedLandmark.rating}</Text>
- {ownedByUser ?
- <View style={{flexDirection: 'row'}}>
- <Icon.Button style={styles.headerBtn} onPress={editLandmark} name="edit"/>
- <Icon.Button style={styles.headerBtn} onPress={deleteLandmark} name="trash"/>
- </View>
- : null}
- {!ownedByUser && mapState.selectedLandmark.rating == 0 ? <Icon.Button style={styles.headerBtn} onPress={endorseLandmark} name="thumbs-up"/> : null}
- {!ownedByUser && mapState.selectedLandmark.rating > 0 ?
- <View style={{flexDirection: 'row'}}>
- <Text style={{marginRight: 5, color: 'white'}}>
- Liked by you
- </Text>
- <TouchableOpacity onPress={removeEndorsement} style={{alignItems: 'center', padding: 0}}>
- <Text style={{marginRight: 10, color: '#A9A9A9'}}>(Unlike)</Text>
- </TouchableOpacity>
- </View> : null}
- </View>
- <View style={styles.titleContainer}>
- <Text style={styles.title}>{mapState.selectedLandmark.title}</Text>
- <View style={{backgroundColor: 'white', borderRadius: 25, height: 40, width: 40 ,justifyContent: 'center', alignItems: 'center'}}>
- <Image style={styles.titleIcon} source={Icons[mapState.selectedLandmark.icon]} />
- </View>
- </View>
- <View style={styles.detailsContainer}>
- <ScrollView><Text style={styles.desc}>{mapState.selectedLandmark.desc}</Text></ScrollView>
- <Text style={styles.date}>{format(mapState.selectedLandmark.dateAdded, "MMMM do, yyyy h:mma")}</Text>
- <Text style={{fontSize: 15, color: 'white'}}>Comments:</Text>
- <View style={styles.commentsBox}>
- <ScrollView>
- <TouchableOpacity>
- {mapState.selectedLandmark.comments.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(comment => {
- return(
- <View style={styles.commentContainer} key={comment.id}>
- <View style={styles.commentHeader}>
- <Text style={{fontWeight: 'bold'}} >{comment.name}</Text>
- <Text style={{fontSize: 12, color: 'lightgrey'}}>{format(comment.dateAdded, "MMMM do, yyyy h:mma")}</Text>
- </View>
- <Text style={{padding: 10}}>{comment.text}</Text>
- </View>
- );
- })}
- </TouchableOpacity>
- </ScrollView>
- <View style={styles.commentInputContainer}>
- <TextInput
- ref={commentInput}
- multiline={true}
- style={[styles.addComment]}
- placeholder="Add comment..."
- onChangeText={text => setComment(text)}
- value={commentText}
- ></TextInput>
- {keyboardShown && commentText != '' ?
- <View style={styles.postBtn}>
- <Icon.Button
- name='paper-plane'
- color='black'
- style={{backgroundColor: 'white'}}
- onPress={postComment}/>
- </View> : null }
- </View>
- </View>
- </View>
- </View>
- )
- }
- const styles = StyleSheet.create({
- detailsHeader: {
- borderBottomWidth: 1,
- borderColor: 'lightgrey',
- flexDirection: 'row',
- justifyContent: 'space-between',
- alignItems: 'center',
- backgroundColor: '#df3f3f',
- marginBottom: 10,
- },
- headerBtn: {
- backgroundColor: '#df3f3f',
- padding: 10,
- justifyContent: 'center',
- },
- container: {
- backgroundColor: '#df3f3f',
- },
- detailsContainer: {
- marginHorizontal: 20
- },
- titleContainer: {
- marginHorizontal: 20,
- marginBottom: 20,
- flexDirection: 'row',
- justifyContent: 'space-between'
- },
- titleIcon: {
- height: 30,
- width: 22,
- },
- title: {
- flex: 1,
- flexWrap: 'wrap',
- color: 'white',
- fontSize: 20,
- marginBottom: 20,
- marginRight: 20,
- },
- rating: {
- color: 'white',
- },
- desc: {
- color: 'white',
- height: 50,
- },
- date: {
- color: 'lightgray',
- marginBottom: 10,
- alignSelf: "flex-end",
- },
- commentsBox: {
- height: 275,
- backgroundColor: 'white',
- marginTop: 10,
- marginBottom: 20,
- borderColor: 'black',
- },
- commentContainer: {
- padding: 10
- },
- commentHeader: {
- flexDirection: 'row',
- justifyContent: 'space-between'
- },
- commentBody: {
- padding: 10
- },
- commentInputContainer: {
- marginHorizontal: 10,
- borderTopWidth: 1,
- borderColor: 'grey',
- justifyContent: 'space-between',
- backgroundColor: 'white',
- flexDirection: 'row',
- },
- addComment: {
- width: '80%',
- backgroundColor: 'white',
- },
- postBtn: {
- marginVertical: 10,
- justifyContent: 'center',
- alignItems: 'center',
- width: '20%'
- },
- })
- export default LandmarkDetails;
|