import { Text, TextField, TextFieldRef, View } from "react-native-ui-lib"; import React, { useEffect, useRef, useState } from "react"; import { GroceryCategory, GroceryFrequency, useGroceryContext } from "@/contexts/GroceryContext"; import { Dropdown } from "react-native-element-dropdown"; import CloseXIcon from "@/assets/svgs/CloseXIcon"; import {Alert, findNodeHandle, StyleSheet, UIManager} from "react-native"; import DropdownIcon from "@/assets/svgs/DropdownIcon"; import { AntDesign } from "@expo/vector-icons"; import { ProfileType, useAuthContext } from "@/contexts/AuthContext"; import { IGrocery } from "@/hooks/firebase/types/groceryData"; const defaultGroceryItem = { id: "", title: "", category: GroceryCategory.None, approved: false, recurring: false, frequency: GroceryFrequency.Never, bought: false, }; const EditGroceryItem = ({ editGrocery, onInputFocus, closeEdit }: { editGrocery?: IGrocery; onInputFocus: (y: number) => void; closeEdit?: Function }) => { const { fuzzyMatchGroceryCategory } = useGroceryContext(); const inputRef = useRef(null); const containerRef = useRef(null); const { profileData } = useAuthContext(); const { updateGroceryItem, addGrocery, } = useGroceryContext(); const [grocery, setGrocery] = useState(editGrocery ?? defaultGroceryItem); const handleFocus = () => { if (containerRef.current) { const handle = findNodeHandle(containerRef.current); if (handle) { UIManager.measure( handle, ( x: number, y: number, width: number, height: number, pageX: number, pageY: number ) => { onInputFocus(pageY); } ); } } }; const groceryCategoryOptions = Object.values(GroceryCategory).map( (category) => ({ label: category, value: category, }) ); const handleClose = () => { closeEdit(); } const handleSubmit = () => { if (validateGrocery()) { if (grocery?.id === "") { addGrocery({...grocery, approved: profileData?.userType === ProfileType.PARENT || profileData?.userType === ProfileType.CAREGIVER}); } else { updateGroceryItem(grocery); } setGrocery(defaultGroceryItem); if (closeEdit) { closeEdit(); } } else { return; } } const validateGrocery = () => { if (!grocery?.title || grocery?.title === "") { Alert.alert('Alert', 'Title field cannot be empty'); return false; } else if (grocery.title?.length < 3 || grocery.title?.length > 25) { Alert.alert('Alert', 'Title should be between 3 and 25 characters'); return false; } if (!grocery?.category || grocery?.category === "") { Alert.alert('Alert', 'Category cannot be empty'); return false; } return true; } useEffect(() => { if (inputRef.current) { inputRef.current.focus(); } }, []); return ( { let groceryCategory = fuzzyMatchGroceryCategory(value); setGrocery((oldData) => ({...oldData, title: value, category: groceryCategory})); }} maxLength={25} /> {(grocery?.title !== "") && {grocery.title?.length > 2 && grocery.title?.length <= 25 && } { handleClose(); }} /> } ( )} renderItem={(item) => { return ( {item.label} ); }} onChange={(item) => { setGrocery((oldData) => ({...oldData, category: item.value})); }} /> ); }; const styles = StyleSheet.create({ itemText: { fontFamily: "Manrope_400Regular", fontSize: 15.42, paddingLeft: 15, }, selectedText: { fontFamily: "Manrope_500Medium", fontSize: 13.2, color: "#fd1775", }, dropdownStyle: { borderRadius: 6.61, height: 115.34, width: 187 }, itemStyle: { padding: 0, margin: 0 }, }); export default EditGroceryItem;