From 04c079c1a27ce594c88b6de5fa42d8497189bcff Mon Sep 17 00:00:00 2001 From: UnluckyBird Date: Fri, 24 Nov 2023 11:07:52 +0100 Subject: [PATCH 1/4] Added edit on long press to list view --- app/(tabs)/(index)/index.tsx | 67 +++++++++++++++++++++++++++--------- 1 file changed, 51 insertions(+), 16 deletions(-) diff --git a/app/(tabs)/(index)/index.tsx b/app/(tabs)/(index)/index.tsx index ce86d39..e2ccfd7 100644 --- a/app/(tabs)/(index)/index.tsx +++ b/app/(tabs)/(index)/index.tsx @@ -2,7 +2,7 @@ import { Button, Pressable, StyleSheet, TextInput, useColorScheme } from 'react- import Modal from "react-native-modal"; import { Text, View, } from '../../../components/Themed'; import { useState } from 'react'; -import { FlatList } from 'react-native-gesture-handler'; +import { FlatList, Gesture, GestureDetector } from 'react-native-gesture-handler'; import { FontAwesome5 } from '@expo/vector-icons'; import CheckBox from 'expo-checkbox'; import Swipeable from 'react-native-gesture-handler/Swipeable'; @@ -78,7 +78,7 @@ function rightSwipeAction() { export default function ToBeBoughtScreen() { const colorScheme = useColorScheme() ?? 'light'; - const [isModalAddItemVisible, setIsModalAddItemVisible] = useState(false); + const [isModalAddOrEditItemVisible, setIsModalAddOrEditItemVisible] = useState(false); const [isModalDeleteVisible, setIsModalDeleteVisible] = useState(false); const [products, setProducts] = useState(seedData) @@ -87,20 +87,49 @@ export default function ToBeBoughtScreen() { const [alreadyBought, setAlreadyBought] = useState(false); const [price, setPrice] = useState('0'); + const [itemToEdit, setItemToEdit] = useState(-1) const [itemToDelete, setItemToDelete] = useState(0) const swipeableRows : Swipeable[] = [] - const handleModalAddItem = () => setIsModalAddItemVisible(() => !isModalAddItemVisible); + const handleModalAddOrEditItem = () => setIsModalAddOrEditItemVisible(() => !isModalAddOrEditItemVisible); const handleModalDelete = () => setIsModalDeleteVisible(() => !isModalDeleteVisible); const handleCheckbox = () => setAlreadyBought(() => !alreadyBought); + const clearProduct = () => { setProductName('') setSelectedUsers(usersDropdown.map((user) => user.value)) setAlreadyBought(false) setPrice('0') + setItemToEdit(-1) + } + + const editProduct = (index : number) => { + setProductName(products[index].name) + setSelectedUsers(products[index].data.users.map((user) => user.name)) + } + + const saveEditedProduct = () => { + setProducts(products.map((product, index) => { + if (product === products[itemToEdit]) { + return { ...product, + name: productName, + data: { + ...product.data, + users: selectedUsers.map(user => ({name: user})), + bought: alreadyBought ? {user: "Me", date: moment().format('YYYY.MM.DD'), price: Number(price)} : undefined + } + } + } + else { + return product; + } + })) + + handleModalAddOrEditItem() } - const addProduct = () => { + + const saveAddedProduct = () => { const time = moment().format('YYYY.MM.DD'); const newProduct = { name: productName, @@ -110,17 +139,17 @@ export default function ToBeBoughtScreen() { bought: alreadyBought ? {user: "Me", date: time, price: Number(price)} : undefined } } - if(newProduct.data.bought === undefined){ + if(newProduct.data.bought === undefined) { //Add to GUN here console.log(newProduct.name + ' added') } - else{ + else { //Move to GUN bought list here console.log(newProduct.name + ' bought by ' + newProduct.data.bought.user) } setProducts(products => [...products, newProduct]) - handleModalAddItem() + handleModalAddOrEditItem() } function swipeHandler(dir: 'left' | 'right', index: number) { @@ -162,13 +191,19 @@ export default function ToBeBoughtScreen() { renderLeftActions={leftSwipeAction} renderRightActions={rightSwipeAction} onSwipeableOpen={(dir) => swipeHandler(dir, index)}> - - - {item.name} + { + setItemToEdit(index) + editProduct(index) + handleModalAddOrEditItem() + })}> + + + {item.name} + + Added by {item.data.added.user} {item.data.added.date} - Added by {item.data.added.user} {item.data.added.date} - + ) } @@ -180,7 +215,7 @@ export default function ToBeBoughtScreen() { data={products} renderItem={renderItem} /> - + Product : null} -