diff --git a/client/src/app.js b/client/src/app.js index 56f0024..b249b35 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -15,8 +15,18 @@ function App() { const [showModalItem, setShowModalItem] = useState(false); const [editBoxBool, setEditBoxBool] = useState(false); - const [editDataBox, setEditDataBox] = useState({}); + const [editItemBool, setEditItemBool] = useState(false); + const [itemID, setItemID] = useState(); + const [itemName, setItemName] = useState(''); + const [itemDescription, setItemDescription] = useState(''); + + const handleName = (e) => { + setItemName(e.target.value) + } + const handleDescription = (e) => { + setItemDescription(e.target.value) + } //fetch all box data useEffect(() => { @@ -36,6 +46,7 @@ function App() { } } + // BOX FUNCTIONS /////////////////////////////////////////////////////////////// const createBox = async (boxData) => { try { const response = await axios.post(api.baseApiUrl, boxData); @@ -54,6 +65,7 @@ function App() { } } + const removeBox = async (id) => { try { const response = await axios.delete(`${api.baseApiUrl}/${id}`); @@ -94,15 +106,8 @@ function App() { removeBox(id); } const handleAddBox = () => { - //open modal setShowModalBox(true); } - const clearForm = () => { - setEditBoxBool(false); - setBoxName(''); - setBoxDescription(''); - setBoxID(null); - } const handleEditBox = (id) => { const box = data.find(box => box._id === id); setEditBoxBool(true); @@ -111,6 +116,13 @@ function App() { setBoxID(id); setShowModalBox(true); } + const clearForm = () => { + setEditBoxBool(false); + setBoxName(''); + setBoxDescription(''); + setBoxID(null); + } + const boxHandler = { handleSubmit: handleSubmitBox, handleRemove: handleRemoveBox, @@ -118,12 +130,13 @@ function App() { handleDescription: handleBoxDescription, handleEdit: handleEditBox }; + // ITEM FUNCTIONS /////////////////////////////////////////////////////////////// - const createItem = async (id, itemData) => { + const createItem = async (boxID, itemData) => { try { - const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData); + const response = await axios.post(`${api.baseApiUrl}/${boxID}/item`, itemData); const boxData = [...data]; - boxData.splice(boxData.findIndex(box => box._id === id), 1, response.data); + boxData.splice(boxData.findIndex(box => box._id === boxID), 1, response.data); setData(boxData); } catch (error) { @@ -131,6 +144,18 @@ function App() { } } + const editItem = async (boxID, itemID, itemData) => { + try { + const response = await axios.put(`${api.baseApiUrl}/${boxID}/item/${itemID}`, itemData); + const boxData = [...data]; + boxData.splice(boxData.findIndex(box => box._id === boxID), 1, response.data); + setData(boxData); + } + catch (error) { + + } + } + const removeItem = async (boxID, itemID) => { try { const boxData = [...data]; @@ -142,33 +167,50 @@ function App() { console.log(error); } } + const handleAddItem = (id) => { - const boxData = [...data]; - const index = boxData.findIndex(box => box._id === id); - boxData.forEach((box, boxIndex) => box.showItemForm = index === boxIndex); - setData(boxData); + setBoxID(id); + setEditItemBool(false); + setShowModalItem(true); + } + const handleEditItem = (boxID, itemID) => { + const box = data.find(box => box._id === boxID); + const item = box.item.find(item => item._id === itemID); + + setItemID(itemID); + setBoxID(boxID); + setItemName(item.name); + setItemDescription(item.description); + + setEditItemBool(true); + setShowModalItem(true); } const handleRemoveItem = (boxID, itemID) => { removeItem(boxID, itemID); } const handleSubmitItem = (event) => { - event.preventDefault(); - clearForm(); - const formData = new FormData(event.target); - const id = formData.get('id'); + setShowModalItem(false); const itemData = { - name: formData.get('name'), - description: formData.get('description') + id: itemID, + name: itemName, + description: itemDescription } - createItem(id, itemData); + if (editItemBool) + editItem(boxID, itemID, itemData); + else + createItem(boxID, itemData); + + clearForm(); + return false; } const itemHandler = { handleSubmit: handleSubmitItem, handleRemove: handleRemoveItem, - handleAddItem: handleAddItem, + handleAdd: handleAddItem, + handleEdit: handleEditItem, }; const boxForm = @@ -186,9 +228,23 @@ function App() { +const itemForm =
+New Item +
+ + + +
+ +
return ( <> - { } + {} + {}
    {data && data.map((box, index) => { return ( diff --git a/client/src/box.js b/client/src/box.js index d35d158..6bd5377 100644 --- a/client/src/box.js +++ b/client/src/box.js @@ -3,46 +3,21 @@ import Item from './item'; import Modal from './modal'; const Box = ({ boxData, boxHandler, itemHandler }) => { - const [itemName, setItemName] = useState(''); - const [itemDescription, setItemDescription] = useState(''); - - const handleName = (e) => { - setItemName(e.target.value) - } - const handleDescription = (e) => { - setItemDescription(e.target.value) - } - const formNewItem =
    - New Item -
    - - - -
    - -
    - return ( <> - {}
  • {boxData.name}

    {boxData.description}

    - +
      - {boxData.item.map(((item, index) => { return + itemHandler={itemHandler}/> }))}
    diff --git a/client/src/item.js b/client/src/item.js index e0ce3d4..03ba4ef 100644 --- a/client/src/item.js +++ b/client/src/item.js @@ -1,10 +1,10 @@ import React from 'react'; -const item = ({ data, handleRemove, handleEdit, boxID }) => { +const item = ({ data, itemHandler, boxID }) => { return (
  • {data.name}

    - - + +
  • ); }; diff --git a/server/server.js b/server/server.js index 85d8dbd..30806f9 100644 --- a/server/server.js +++ b/server/server.js @@ -80,14 +80,22 @@ app.post('/box/:id/item', async (request, response) => { const updatedBox = await getBox(id); response.send(updatedBox); }); - +//edit item +app.put('/box/:boxId/item/:itemId', async (request, response) => { + const boxID = request.params.boxId; + const itemID = request.params.itemId; + await Box.updateOne({ 'item._id': itemID }, { + $set: { item: request.body } + }) + const updatedBox = await getBox(boxID); + response.send(updatedBox); +}); //delete item app.delete('/box/:boxId/item/:itemId', async (request, response) => { const boxID = request.params.boxId; const itemID = request.params.itemId; await Box.update({ _id: boxID }, { $pull: { item: { _id: itemID } } }); const box = await getBox(boxID); - console.log(box); response.send(box); });