import React, { useState, useEffect, useRef } from 'react'; import { Button, TextInput, Image, ScrollView, View, Text, StyleSheet, TouchableOpacity, Keyboard, Alert, Dimensions} from 'react-native'; import { format } from 'date-fns'; import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu'; 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 PlaceDetails = ({closeModal, addLandmark}) => { 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 placePicker = useRef(); const placeMenu = useRef(); const commentInput = useRef() const _keyboardDidShow = (e) => { setKeyboard(true); console.log(mapState.selectedPlace); } const _keyboardDidHide = (e) => { setKeyboard(false); setOffset({}); commentInput.current.blur(); // 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 currentPlace; // api call // dispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => { // if (place.id === mapState.selectedPlace.id) { // const newTip = { // id: uuidv4(), // dateAdded: new Date(), // name: "cdmoss", // text: commentText, // rating: 0, // } // currentPlace = {...place, tips: [...place.tips, newTip]}; // dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace}) // return currentPlace; // } // return place; // })}); setComment(''); Keyboard.dismiss(); } const setSelectedPlace = (place) => { mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: { id: place.id, name: place.properties.name, desc: place.properties.desc, tips: place.properties.tips, dateAdded: place.properties.dateAdded, postedBy: place.properties.postedBy, rating: place.properties.rating, }}); } const likeTip = (tip) => { console.log("Tip rating before: " + tip.rating); let currentPlace = {}; let currentTip = {}; // api call // mapDispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => { // if (place.id == mapState.selectedPlace.id) { // currentPlace = {...place, tips: // place.tips.map(t => { // if (t.id == tip.id) { // currentTip = {...t, rating: t.rating + 1, liked: true}; // console.log("Tip rating after " + currentTip.rating); // return currentTip; // } // return t; // } // )} // mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace}) // return currentPlace; // } // return place; // })}) } const unlikeTip = (tip) => { console.log('test') let currentPlace = {}; let currentTip = {}; //api call // dispatch({type: "UPDATE_PLACES", payload: mapState.places.map(place => { // if (place.id == mapState.selectedPlace.id) { // currentPlace = {...place, tips: // place.tips.map(t => { // if (t.id == tip.id) { // currentTip = {...t, rating: t.rating - 1, liked: false}; // console.log("Tip rating after " + currentTip.rating); // return currentTip; // } // return t; // } // )} // mapDispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace}) // return currentPlace; // } // return place; // })}) } const notifyError = (errors) => { Alert.alert('Comment must include text.'); } return ( {mapState.selectedPlaces.length > 1 ? placePicker.current.show()}> {mapState.selectedPlace.name} }> {mapState.selectedPlaces.map(p => { return( { setSelectedPlace(p); placePicker.current.hide(); }} >{p.properties.name} ) })} : {mapState.selectedPlaces.map(p => p.properties.name)[0]} } placeMenu.current.show()} name="ellipsis-h" style={{paddingLeft: 10, paddingRight: 0, backgroundColor: '#df3f3f'}} />}> { addLandmark(); placeMenu.current.hide(); }} >Add landmark here { placeMenu.current.hide() }} >Cancel Tips {mapState.selectedPlace.tips ? mapState.selectedPlace.tips.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(tip => { console.log(tip.dateAdded) return( {tip.name} {tip.dateAdded.toString()} {tip.text} {tip.rating} {tip.liked ? You like this unlikeTip(tip)}> (Unlike) : likeTip(tip)} color="black" name="thumbs-up"/> } ); }) : There are no tips for this place.} setComment(text)} value={commentText} > {keyboardShown && commentText != '' ? : null } ) } const styles = StyleSheet.create({ menuModal: { backgroundColor: '#df3f3f', height: 400, }, commentContainer: { margin: 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 PlaceDetails;