123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286 |
- 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 { useMockState } from '../contexts/MockContext';
- import Icon from 'react-native-vector-icons/FontAwesome'
- import { Icons } from '../globals';
- import { v4 as uuidv4 } from 'uuid';
- const PlaceDetails = ({closeModal, addLandmark}) => {
- 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 placePicker = useRef();
- const placeMenu = useRef();
- const commentInput = useRef()
- const _keyboardDidShow = (e) => {
- setKeyboard(true);
- console.log(state.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;
- dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
- if (place.id === state.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) => {
- dispatch({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 = {};
- dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
- if (place.id == state.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;
- }
- )}
- dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
- return currentPlace;
- }
- return place;
- })})
- }
- const unlikeTip = (tip) => {
- console.log('test')
- let currentPlace = {};
- let currentTip = {};
- dispatch({type: "UPDATE_PLACES", payload: state.places.map(place => {
- if (place.id == state.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;
- }
- )}
- dispatch({type: "UPDATE_SELECTED_PLACE", payload: currentPlace})
- return currentPlace;
- }
- return place;
- })})
- }
- const notifyError = (errors) => {
- Alert.alert('Comment must include text.');
- }
- return (
- <View style={{backgroundColor: '#df3f3f',}}>
- <View style={{flexDirection: 'row', height: 35, justifyContent: 'space-between', alignContent: 'center', borderBottomColor: 'white', borderBottomWidth: 1}}>
- <Icon.Button name="times" style={{width: 40, backgroundColor: '#df3f3f', }} onPress={closeModal}/>
- {state.selectedPlaces.length > 1 ?
- <Menu
- ref={placePicker}
- contentStyle={{backgroundColor: 'white', zIndex: 10}}anchor
- button={
- <TouchableOpacity style={{flexDirection: 'row', height: '100%', alignItems: 'center'}} onPress={() => placePicker.current.show()}>
- <Text style={{color: 'white'}}>{state.selectedPlace.name}</Text>
- <Icon style={{marginLeft: 5, color: 'white'}} name="chevron-down" />
- </TouchableOpacity>}>
- {state.selectedPlaces.map(p => {
- return(
- <MenuItem
- key={p.id}
- onPress={() => {
- setSelectedPlace(p);
- placePicker.current.hide();
- }} >{p.properties.name}</MenuItem>
- )
- })}
- </Menu> : <Text style={{textAlignVertical: 'center', color: 'white'}}>{state.selectedPlaces.map(p => p.properties.name)[0]}</Text> }
- <Menu
- ref={placeMenu}
- button={<Icon.Button onPress={() => placeMenu.current.show()} name="ellipsis-h" style={{paddingLeft: 10, paddingRight: 0, backgroundColor: '#df3f3f'}} />}>
- <MenuItem onPress={() => {
- addLandmark();
- placeMenu.current.hide();
- }} >Add landmark here</MenuItem>
- <MenuItem onPress={() => {
- placeMenu.current.hide()
- }} >Cancel</MenuItem>
- </Menu>
- </View>
- <View style={{margin: 20}}>
- <Text style={{marginBottom: 20, fontSize: 15, color: 'white'}}>Tips</Text>
- <View style={{backgroundColor: 'white',}}>
- <View style={{height: 220}}>
- <ScrollView>
- <TouchableOpacity activeOpacity={1}>
- {state.selectedPlace.tips ?
- state.selectedPlace.tips.sort((a, b) => {return b.dateAdded - a.dateAdded}).map(tip => {
- console.log(tip.dateAdded)
- return(
- <View style={styles.commentContainer} key={tip.id}>
- <View style={styles.commentHeader}>
- <Text style={{fontWeight: 'bold'}} >{tip.name}</Text>
- <Text style={{fontSize: 12, color: 'lightgrey'}}>{tip.dateAdded.toString()}</Text>
- </View>
- <Text>{tip.text}</Text>
- <View style={{flexDirection: 'row', justifyContent: 'flex-end'}}>
- <Text style={{padding: 5, textAlign: 'right', color: 'black'}}>{tip.rating}</Text>
- {tip.liked ?
- <View style={{flexDirection: 'row', alignItems: 'center'}}>
- <Icon size={20} style={{alignSelf: 'center', marginLeft: 2, opacity: .5, zIndex: 5}} color="black" name="thumbs-up"/>
- <Text style={{alignSelf: 'center', marginLeft: 5, opacity: .5, zIndex: 5, color: "black"}} >You like this</Text>
- <TouchableOpacity style={{marginLeft: 5}} onPress={() => unlikeTip(tip)}>
- <Text style={{color: '#A9A9A9'}}>(Unlike)</Text>
- </TouchableOpacity>
- </View> :
- <TouchableOpacity style={{justifyContent: 'center', }}>
- <Icon size={20} style={{marginLeft: 2, zIndex: 5}} onPress={() => likeTip(tip)} color="black" name="thumbs-up"/>
- </TouchableOpacity>}
- </View>
- </View>
- );
- }) :
- <Text>There are no tips for this place.</Text>}
- </TouchableOpacity>
- </ScrollView>
- </View>
- <View style={[styles.commentInputContainer, commentInputOffset]}>
- <TextInput
- ref={commentInput}
- multiline={true}
- style={[styles.addComment]}
- placeholder="Add tip..."
- onChangeText={text => setComment(text)}
- value={commentText}
- ></TextInput>
- {keyboardShown && commentText != '' ?
- <View style={styles.postBtn}>
- <Icon.Button
- name='paper-plane'
- color='black'
- style={{backgroundColor: 'white', margin: 0}}
- onPress={postComment}/>
- </View> : null }
- </View>
- </View>
- </View>
- </View>
- )
- }
- 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;
|