From 293f9bf932d568146feccbdaf535f9f7bbb878b7 Mon Sep 17 00:00:00 2001 From: Huda Mabkhoot Date: Sat, 12 Oct 2024 18:04:36 +0300 Subject: [PATCH] fin al chnages and fixing the issue of listName being null --- src/App.jsx | 2 +- src/components/ListItem.jsx | 68 +++++++++---------- .../ManageListForms/EditItemForm.jsx | 41 +++++------ src/components/ui/dialog.jsx | 2 +- 4 files changed, 57 insertions(+), 56 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 3853b0c..982d3d7 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -21,7 +21,7 @@ export function App() { null, ); - const listName = listPath.split('/').pop(); + const listName = listPath ? listPath.split('/').pop() : null; /** * This custom hook holds info about the current signed in user. diff --git a/src/components/ListItem.jsx b/src/components/ListItem.jsx index daf3ec1..cddd20e 100644 --- a/src/components/ListItem.jsx +++ b/src/components/ListItem.jsx @@ -94,7 +94,7 @@ export function ListItem({ > @@ -105,7 +105,7 @@ export function ListItem({ )} -
+
@@ -113,68 +113,68 @@ export function ListItem({ {indicator}

-
+
+ handleOpenModal(id)}> + + + + + + Edit {name} + + Modify the details of the item you'd like to edit. + + + + + + - + Are you absolutely sure? - + This action cannot be undone. Do you really want to delete{' '} {name}? setIsAlertOpen(false)} > Cancel - + Delete - handleOpenModal(id)}> - - - - - - Edit {name} - - Modify the details of the item you'd like to edit. - - - - - -
diff --git a/src/components/ManageListForms/EditItemForm.jsx b/src/components/ManageListForms/EditItemForm.jsx index 462c2d9..c51fe3e 100644 --- a/src/components/ManageListForms/EditItemForm.jsx +++ b/src/components/ManageListForms/EditItemForm.jsx @@ -26,7 +26,7 @@ export default function EditItemForm({ itemQuantity: quantity, daysUntilNextPurchase: dateNextPurchased, }); - }, [name]); + }, []); const handleSubmit = async (event) => { event.preventDefault(); @@ -45,31 +45,32 @@ export default function EditItemForm({ resetItemName(); return; } - + // check if any of the values have been edited const hasChanged = formattedItemName !== name || formQuantity !== quantity || newDateNextPurchased !== dateNextPurchased; + + // if no changes were made exit early if (!hasChanged) { toast.error('No changes were made.'); handleOpenModal(); return; } - try { - let updatedData = { - itemName: formattedItemName, - itemQuantity: formQuantity, - }; + let updatedData = { + itemName: formattedItemName, + itemQuantity: formQuantity, + }; + // check if dateNextPurchased have been edited to use getFutureDate if not use the same date + if (newDateNextPurchased !== dateNextPurchased) { + const futureDate = parseInt(newDateNextPurchased); + updatedData.dateNextPurchased = getFutureDate(futureDate); + } else { + updatedData.dateNextPurchased = dateNextPurchased; + } - if (newDateNextPurchased !== dateNextPurchased) { - const futureDate = parseInt(newDateNextPurchased); - const formattedFutureDate = getFutureDate(futureDate); - updatedData.dateNextPurchased = formattedFutureDate; - } else { - updatedData.dateNextPurchased = dateNextPurchased; - } - console.log(); + try { handleOpenModal(); await editItem(listPath, id, updatedData); toast.success(`${formattedItemName} has been updated!`); @@ -118,18 +119,18 @@ export default function EditItemForm({ className="flex my-2 items-center justify-center gap-4" id="daysUntilNextPurchase" > -
+
-
+
-
+
diff --git a/src/components/ui/dialog.jsx b/src/components/ui/dialog.jsx index 8154999..386a3a0 100644 --- a/src/components/ui/dialog.jsx +++ b/src/components/ui/dialog.jsx @@ -16,7 +16,7 @@ const DialogOverlay = React.forwardRef(({ className, ...props }, ref) => (