import React, { useEffect, useRef, useState } from 'react'; import { ScrollView, View, Text, StyleSheet, TouchableOpacity, Dimensions, Image, Alert} from 'react-native'; import { TextInput } from 'react-native-paper'; import { Controller, useForm } from 'react-hook-form'; import { useMockState } from '../contexts/MockContext'; import Modal from 'react-native-modalbox'; import { Icons } from '../globals'; import Icon from 'react-native-vector-icons/FontAwesome'; import { v4 as uuidv4 } from 'uuid'; import { useMemo } from 'react/cjs/react.production.min'; const editTheme = { colors: { primary: 'black', text: 'black' } } const LandmarkForm = ({navigation}) => { const {dispatch, state} = useMockState(); const { control, isDirty, handleSubmit, setValue, formState } = useForm({ mode: 'all' }); const [selectedIcon, setIcon] = useState(state.selectedLandmark.icon); const iconSelector = useRef(); useEffect(() => { setValue('title', state.selectedLandmark.title); setValue('desc', state.selectedLandmark.desc); setIcon(state.selectedLandmark.icon) }, [state]); const saveLandmark = (formData) => { let currentLandmark = state.landmarks.filter(l => l.id === state.selectedLandmark.id)[0]; console.log(currentLandmark) if (currentLandmark != null) { // we're updating an existing landmark dispatch({type: "UPDATE_LANDMARKS", payload: state.landmarks.map(l => { if (l.id === state.selectedLandmark.id) { currentLandmark = {...l, title: formData.title, desc: formData.desc, icon: selectedIcon}; dispatch({type: "UPDATE_SELECTED_LANDMARK", payload: currentLandmark}); return currentLandmark; } return l; })}); console.log('here') navigation.navigate('Map'); } else { // we're adding a new landmark const newLandmark = { id: uuidv4(), postedBy: 'cdmoss', dateAdded: Date.now(), title: formData.title, desc: formData.desc, icon: selectedIcon, longitude: state.selectedLandmark.longitude, latitude: state.selectedLandmark.latitude, comments: [] }; dispatch({type: "UPDATE_LANDMARKS", payload: [...state.landmarks, newLandmark]}) navigation.navigate('Map'); } } const openSelector = () => { iconSelector.current.open(); } const updateIcon = (icon) => { setIcon(icon); iconSelector.current.close(); } const closeModal = () => { iconSelector.current.close(); } return ( Title ( onBlur(value)} onChangeText={value => onChange(value)} > )} name="title" rules={{ required: true, maxLength: 100 }} /> {formState.errors.title?.type === "required" && Title is required.} ( onBlur(value)} onChangeText={value => onChange(value)} > )} name="desc" rules={{ required: true, maxLength: 500 }} /> {formState.errors.desc?.type === "required" && Description is required.} {formState.errors.desc?.type === "maxLength" && Description must be less than 500 characters.} Type {selectedIcon} Save Changes {Object.entries(Icons).map(icon => { return( updateIcon(icon[0])} > {icon[0]} ) })} ) } const styles = StyleSheet.create({ container: { position: 'absolute', width: Dimensions.get('window').width, height: Dimensions.get('window').height, flex: 1, backgroundColor: '#df3f3f', }, inputContainer: { height: 500, paddingHorizontal: 20, paddingBottom: 20, paddingTop: 70, }, label: { color: 'white', marginTop: 20, marginBottom: 8 }, title: { fontSize: 20, marginBottom: 20, }, input: { backgroundColor: 'white', }, desc: { color: 'white', }, iconPicker: { color: 'white', }, selectedIconContainer: { backgroundColor: 'white', paddingHorizontal: 30, padding: 15, flexDirection: 'row', alignItems: 'center', justifyContent: 'space-between' }, errorText: { alignSelf: 'flex-end', color: 'black', fontSize: 15 }, selectedIcon: { backgroundColor: 'white', flexDirection: 'row', alignItems: 'center', }, iconChoice: { marginHorizontal: 30, paddingVertical: 10, flexDirection: 'row', alignItems: 'center', borderBottomWidth: 1, }, chevronRotate: { transform: [{ rotate: '180deg'}] } }) export default LandmarkForm;