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 { useMockState } from '../contexts/MockContext'; import Icon from 'react-native-vector-icons/FontAwesome' import { Icons } from '../globals'; import { v4 as uuidv4 } from 'uuid'; const LandmarkDetails = ({closeModal, editLandmark}) => { const {dispatch, state} = useMockState(); const [ownedByUser, setOwned] = useState(state.username === state.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); setOffset({ position: 'absolute', bottom: e.endCoordinates.height / 2 - 22, marginHorizontal: -20, paddingLeft: 10, width: Dimensions.get('window').width }); } 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; dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => { if (l.id === state.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(''); setPosted(true); Keyboard.dismiss(); setPosted(false); } const notifyError = (errors) => { Alert.alert('Comment must include text.'); } const endorseLandmark = () => { dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => { if (l.id == state.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 = () => { dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => { if (l.id == state.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 = () => { dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.filter(l => l.id !== state.selectedLandmark.id)}); closeModal('Successfully deleted landmark.'); } return ( closeModal()} name="times"/> {state.selectedLandmark.rating} {ownedByUser ? : null} {!ownedByUser && state.selectedLandmark.rating == 0 ? : null} {!ownedByUser && state.selectedLandmark.rating > 0 ? You like this landmark (Undo) : null} {state.selectedLandmark.title} {state.selectedLandmark.desc} {format(state.selectedLandmark.dateAdded, "MMMM do, yyyy h:mma")} Comments: {state.selectedLandmark.comments.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(comment => { return( {comment.name} {format(comment.dateAdded, "MMMM do, yyyy h:mma")} {comment.text} ); })} setComment(text)} value={commentText} > {keyboardShown && commentText != '' ? : null } ) } 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: { marginBottom: 30, backgroundColor: '#df3f3f', }, detailsContainer: { marginHorizontal: 20 }, titleContainer: { marginHorizontal: 20, marginBottom: 20, flexDirection: 'row', justifyContent: 'space-between' }, titleIcon: { height: 30, width: 22, }, title: { color: 'white', fontSize: 20, marginBottom: 20, }, rating: { color: 'white', }, desc: { color: 'white', height: 50, }, date: { color: 'lightgray', marginBottom: 10, alignSelf: "flex-end", }, commentsBox: { height: 275, backgroundColor: 'white', marginTop: 10, padding: 10, marginBottom: 20, borderColor: 'black', }, commentContainer: { padding: 10 }, commentHeader: { flexDirection: 'row', justifyContent: 'space-between' }, commentBody: { padding: 10 }, commentInputContainer: { backgroundColor: 'white', flexDirection: 'row', borderTopWidth: 1, borderColor: 'grey', justifyContent: 'space-between', alignItems: 'center', width: 300 }, addComment: { backgroundColor: 'white', width: 270, }, postBtn: { justifyContent: 'center', alignItems: 'center', borderRadius: 10, width: 75, height: 60 }, btnContainer: { backgroundColor: '#df3f3f', marginTop: 10, height: 70, flexDirection: 'row', justifyContent: 'space-evenly', alignItems: 'center', padding: 20, }, btn: { borderColor: 'white', borderRadius: 25, justifyContent: 'center', alignItems: 'center', marginHorizontal: 20, marginVertical: 20, borderWidth: 1, width: 170, height: 50 }, btnText: { color: 'white', } }) export default LandmarkDetails;