add edit items
This commit is contained in:
+80
-24
@@ -15,8 +15,18 @@ function App() {
|
|||||||
const [showModalItem, setShowModalItem] = useState(false);
|
const [showModalItem, setShowModalItem] = useState(false);
|
||||||
|
|
||||||
const [editBoxBool, setEditBoxBool] = 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
|
//fetch all box data
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@@ -36,6 +46,7 @@ function App() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// BOX FUNCTIONS ///////////////////////////////////////////////////////////////
|
||||||
const createBox = async (boxData) => {
|
const createBox = async (boxData) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(api.baseApiUrl, boxData);
|
const response = await axios.post(api.baseApiUrl, boxData);
|
||||||
@@ -54,6 +65,7 @@ function App() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const removeBox = async (id) => {
|
const removeBox = async (id) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.delete(`${api.baseApiUrl}/${id}`);
|
const response = await axios.delete(`${api.baseApiUrl}/${id}`);
|
||||||
@@ -94,15 +106,8 @@ function App() {
|
|||||||
removeBox(id);
|
removeBox(id);
|
||||||
}
|
}
|
||||||
const handleAddBox = () => {
|
const handleAddBox = () => {
|
||||||
//open modal
|
|
||||||
setShowModalBox(true);
|
setShowModalBox(true);
|
||||||
}
|
}
|
||||||
const clearForm = () => {
|
|
||||||
setEditBoxBool(false);
|
|
||||||
setBoxName('');
|
|
||||||
setBoxDescription('');
|
|
||||||
setBoxID(null);
|
|
||||||
}
|
|
||||||
const handleEditBox = (id) => {
|
const handleEditBox = (id) => {
|
||||||
const box = data.find(box => box._id === id);
|
const box = data.find(box => box._id === id);
|
||||||
setEditBoxBool(true);
|
setEditBoxBool(true);
|
||||||
@@ -111,6 +116,13 @@ function App() {
|
|||||||
setBoxID(id);
|
setBoxID(id);
|
||||||
setShowModalBox(true);
|
setShowModalBox(true);
|
||||||
}
|
}
|
||||||
|
const clearForm = () => {
|
||||||
|
setEditBoxBool(false);
|
||||||
|
setBoxName('');
|
||||||
|
setBoxDescription('');
|
||||||
|
setBoxID(null);
|
||||||
|
}
|
||||||
|
|
||||||
const boxHandler = {
|
const boxHandler = {
|
||||||
handleSubmit: handleSubmitBox,
|
handleSubmit: handleSubmitBox,
|
||||||
handleRemove: handleRemoveBox,
|
handleRemove: handleRemoveBox,
|
||||||
@@ -118,12 +130,13 @@ function App() {
|
|||||||
handleDescription: handleBoxDescription,
|
handleDescription: handleBoxDescription,
|
||||||
handleEdit: handleEditBox
|
handleEdit: handleEditBox
|
||||||
};
|
};
|
||||||
|
// ITEM FUNCTIONS ///////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const createItem = async (id, itemData) => {
|
const createItem = async (boxID, itemData) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData);
|
const response = await axios.post(`${api.baseApiUrl}/${boxID}/item`, itemData);
|
||||||
const boxData = [...data];
|
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);
|
setData(boxData);
|
||||||
}
|
}
|
||||||
catch (error) {
|
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) => {
|
const removeItem = async (boxID, itemID) => {
|
||||||
try {
|
try {
|
||||||
const boxData = [...data];
|
const boxData = [...data];
|
||||||
@@ -142,33 +167,50 @@ function App() {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleAddItem = (id) => {
|
const handleAddItem = (id) => {
|
||||||
const boxData = [...data];
|
setBoxID(id);
|
||||||
const index = boxData.findIndex(box => box._id === id);
|
setEditItemBool(false);
|
||||||
boxData.forEach((box, boxIndex) => box.showItemForm = index === boxIndex);
|
setShowModalItem(true);
|
||||||
setData(boxData);
|
}
|
||||||
|
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) => {
|
const handleRemoveItem = (boxID, itemID) => {
|
||||||
removeItem(boxID, itemID);
|
removeItem(boxID, itemID);
|
||||||
}
|
}
|
||||||
const handleSubmitItem = (event) => {
|
const handleSubmitItem = (event) => {
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearForm();
|
setShowModalItem(false);
|
||||||
const formData = new FormData(event.target);
|
|
||||||
const id = formData.get('id');
|
|
||||||
const itemData = {
|
const itemData = {
|
||||||
name: formData.get('name'),
|
id: itemID,
|
||||||
description: formData.get('description')
|
name: itemName,
|
||||||
|
description: itemDescription
|
||||||
}
|
}
|
||||||
createItem(id, itemData);
|
if (editItemBool)
|
||||||
|
editItem(boxID, itemID, itemData);
|
||||||
|
else
|
||||||
|
createItem(boxID, itemData);
|
||||||
|
|
||||||
|
clearForm();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
const itemHandler = {
|
const itemHandler = {
|
||||||
handleSubmit: handleSubmitItem,
|
handleSubmit: handleSubmitItem,
|
||||||
handleRemove: handleRemoveItem,
|
handleRemove: handleRemoveItem,
|
||||||
handleAddItem: handleAddItem,
|
handleAdd: handleAddItem,
|
||||||
|
handleEdit: handleEditItem,
|
||||||
};
|
};
|
||||||
|
|
||||||
const boxForm =
|
const boxForm =
|
||||||
@@ -186,9 +228,23 @@ function App() {
|
|||||||
<button type="submit">submit</button>
|
<button type="submit">submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
|
const itemForm = <form onSubmit={itemHandler.handleSubmit}>
|
||||||
|
<legend>New Item</legend>
|
||||||
|
<fieldset>
|
||||||
|
<input id="id" name="id" value={itemID} readOnly hidden></input>
|
||||||
|
<label htmlFor="item-name">name
|
||||||
|
<input onChange={handleName} value={itemName} id="item-name" name="name" type="text"></input>
|
||||||
|
</label>
|
||||||
|
<label htmlFor="item-description">description
|
||||||
|
<input onChange={handleDescription} value={itemDescription} id="item-description" name="description" type="text"></input>
|
||||||
|
</label>
|
||||||
|
</fieldset>
|
||||||
|
<button type="submit">submit</button>
|
||||||
|
</form>
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{<Modal show={showModalBox} edit={editBoxBool} content={boxForm} /> }
|
{<Modal show={showModalItem} edit={editItemBool} content={itemForm} />}
|
||||||
|
{<Modal show={showModalBox} edit={editBoxBool} content={boxForm} />}
|
||||||
<ul className="list-box">
|
<ul className="list-box">
|
||||||
{data && data.map((box, index) => {
|
{data && data.map((box, index) => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
+2
-27
@@ -3,46 +3,21 @@ import Item from './item';
|
|||||||
import Modal from './modal';
|
import Modal from './modal';
|
||||||
|
|
||||||
const Box = ({ boxData, boxHandler, itemHandler }) => {
|
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 = <form onSubmit={itemHandler.handleSubmit}>
|
|
||||||
<legend>New Item</legend>
|
|
||||||
<fieldset>
|
|
||||||
<input id="id" name="id" value={boxData._id} readOnly hidden></input>
|
|
||||||
<label htmlFor="item-name">name
|
|
||||||
<input onChange={handleName} value={itemName} id="item-name" name="name" type="text"></input>
|
|
||||||
</label>
|
|
||||||
<label htmlFor="item-description">description
|
|
||||||
<input onChange={handleDescription} value={itemDescription} id="item-description" name="description" type="text"></input>
|
|
||||||
</label>
|
|
||||||
</fieldset>
|
|
||||||
<button type="submit">submit</button>
|
|
||||||
</form>
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{<Modal content={formNewItem} show={boxData.showItemForm} />}
|
|
||||||
<li className="box">
|
<li className="box">
|
||||||
<h2 className="box-title">{boxData.name}</h2>
|
<h2 className="box-title">{boxData.name}</h2>
|
||||||
<div className="box-subtitle">
|
<div className="box-subtitle">
|
||||||
<h3>{boxData.description}</h3>
|
<h3>{boxData.description}</h3>
|
||||||
<button onClick={() => itemHandler.handleAddItem(boxData._id)}>add item</button>
|
<button onClick={() => itemHandler.handleAdd(boxData._id)}>add item</button>
|
||||||
</div>
|
</div>
|
||||||
<ul className="list-box-item">
|
<ul className="list-box-item">
|
||||||
|
|
||||||
{boxData.item.map(((item, index) => {
|
{boxData.item.map(((item, index) => {
|
||||||
return <Item
|
return <Item
|
||||||
key={index}
|
key={index}
|
||||||
data={item}
|
data={item}
|
||||||
boxID={boxData._id}
|
boxID={boxData._id}
|
||||||
handleRemove={itemHandler.handleRemove} />
|
itemHandler={itemHandler}/>
|
||||||
}))}
|
}))}
|
||||||
</ul>
|
</ul>
|
||||||
<button className="edit-box" onClick={() => boxHandler.handleEdit(boxData._id)}>edit</button>
|
<button className="edit-box" onClick={() => boxHandler.handleEdit(boxData._id)}>edit</button>
|
||||||
|
|||||||
+3
-3
@@ -1,10 +1,10 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const item = ({ data, handleRemove, handleEdit, boxID }) => {
|
const item = ({ data, itemHandler, boxID }) => {
|
||||||
return (<li className="item">
|
return (<li className="item">
|
||||||
<p className="item-name">{data.name}</p>
|
<p className="item-name">{data.name}</p>
|
||||||
<button onClick={() => console.log('edit')}>edit</button>
|
<button onClick={() => itemHandler.handleEdit(boxID, data._id)}>edit</button>
|
||||||
<button onClick={() => handleRemove(boxID, data._id)}>remove</button>
|
<button onClick={() => itemHandler.handleRemove(boxID, data._id)}>remove</button>
|
||||||
</li>);
|
</li>);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
+10
-2
@@ -80,14 +80,22 @@ app.post('/box/:id/item', async (request, response) => {
|
|||||||
const updatedBox = await getBox(id);
|
const updatedBox = await getBox(id);
|
||||||
response.send(updatedBox);
|
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
|
//delete item
|
||||||
app.delete('/box/:boxId/item/:itemId', async (request, response) => {
|
app.delete('/box/:boxId/item/:itemId', async (request, response) => {
|
||||||
const boxID = request.params.boxId;
|
const boxID = request.params.boxId;
|
||||||
const itemID = request.params.itemId;
|
const itemID = request.params.itemId;
|
||||||
await Box.update({ _id: boxID }, { $pull: { item: { _id: itemID } } });
|
await Box.update({ _id: boxID }, { $pull: { item: { _id: itemID } } });
|
||||||
const box = await getBox(boxID);
|
const box = await getBox(boxID);
|
||||||
console.log(box);
|
|
||||||
response.send(box);
|
response.send(box);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user