edit box name/desc
This commit is contained in:
+48
-18
@@ -8,10 +8,16 @@ import Modal from './modal';
|
|||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
const [data, setData] = useState();
|
const [data, setData] = useState();
|
||||||
|
const [boxID, setBoxID] = useState();
|
||||||
const [boxName, setBoxName] = useState('');
|
const [boxName, setBoxName] = useState('');
|
||||||
const [boxDescription, setBoxDescription] = useState('');
|
const [boxDescription, setBoxDescription] = useState('');
|
||||||
const [showFormBox, setShowFormBox] = useState(false);
|
const [showModalBox, setShowModalBox] = useState(false);
|
||||||
const [showBox, setShowBox] = useState(false);
|
const [showModalItem, setShowModalItem] = useState(false);
|
||||||
|
|
||||||
|
const [editBoxBool, setEditBoxBool] = useState(false);
|
||||||
|
const [editDataBox, setEditDataBox] = useState({});
|
||||||
|
|
||||||
|
|
||||||
//fetch all box data
|
//fetch all box data
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchData = async () => {
|
const fetchData = async () => {
|
||||||
@@ -39,7 +45,15 @@ function App() {
|
|||||||
console.log(error);
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
const editBox = async (boxData) => {
|
||||||
|
try {
|
||||||
|
const response = await axios.put(`${api.baseApiUrl}/${boxData.id}`, boxData);
|
||||||
|
setData(response.data);
|
||||||
|
}
|
||||||
|
catch (error) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
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}`);
|
||||||
@@ -52,7 +66,7 @@ function App() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const handleBoxName = (e) => {
|
const handleNameBox = (e) => {
|
||||||
setBoxName(e.target.value);
|
setBoxName(e.target.value);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,12 +76,16 @@ function App() {
|
|||||||
|
|
||||||
const handleSubmitBox = (event) => {
|
const handleSubmitBox = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setShowFormBox(false);
|
setShowModalBox(false);
|
||||||
clearForm();
|
clearForm();
|
||||||
const boxData = {
|
const boxData = {
|
||||||
|
id: boxID,
|
||||||
name: boxName,
|
name: boxName,
|
||||||
description: boxDescription
|
description: boxDescription
|
||||||
};
|
};
|
||||||
|
if (editBoxBool)
|
||||||
|
editBox(boxData);
|
||||||
|
else
|
||||||
createBox(boxData);
|
createBox(boxData);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@@ -76,24 +94,36 @@ function App() {
|
|||||||
removeBox(id);
|
removeBox(id);
|
||||||
}
|
}
|
||||||
const handleAddBox = () => {
|
const handleAddBox = () => {
|
||||||
setShowFormBox(true);
|
//open modal
|
||||||
|
setShowModalBox(true);
|
||||||
}
|
}
|
||||||
const clearForm = () => {
|
const clearForm = () => {
|
||||||
setBoxName();
|
setEditBoxBool(false);
|
||||||
setBoxDescription();
|
setBoxName('');
|
||||||
|
setBoxDescription('');
|
||||||
|
setBoxID(null);
|
||||||
|
}
|
||||||
|
const handleEditBox = (id) => {
|
||||||
|
const box = data.find(box => box._id === id);
|
||||||
|
setEditBoxBool(true);
|
||||||
|
setBoxName(box.name);
|
||||||
|
setBoxDescription(box.description);
|
||||||
|
setBoxID(id);
|
||||||
|
setShowModalBox(true);
|
||||||
}
|
}
|
||||||
const boxHandler = {
|
const boxHandler = {
|
||||||
handleSubmit: handleSubmitBox,
|
handleSubmit: handleSubmitBox,
|
||||||
handleRemove: handleRemoveBox,
|
handleRemove: handleRemoveBox,
|
||||||
handleName: handleBoxName,
|
handleName: handleNameBox,
|
||||||
handleDescription: handleBoxDescription
|
handleDescription: handleBoxDescription,
|
||||||
|
handleEdit: handleEditBox
|
||||||
};
|
};
|
||||||
|
|
||||||
const createItem = async (id, itemData) => {
|
const createItem = async (id, itemData) => {
|
||||||
try {
|
try {
|
||||||
const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData);
|
const response = await axios.post(`${api.baseApiUrl}/${id}/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 === id), 1, response.data);
|
||||||
setData(boxData);
|
setData(boxData);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
@@ -105,7 +135,7 @@ function App() {
|
|||||||
try {
|
try {
|
||||||
const boxData = [...data];
|
const boxData = [...data];
|
||||||
const response = await axios.delete(`${api.baseApiUrl}/${boxID}/item/${itemID}`);
|
const response = await axios.delete(`${api.baseApiUrl}/${boxID}/item/${itemID}`);
|
||||||
boxData.splice(boxData.findIndex(box => box._id === boxID),1,response.data);
|
boxData.splice(boxData.findIndex(box => box._id === boxID), 1, response.data);
|
||||||
setData(boxData);
|
setData(boxData);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
@@ -122,6 +152,7 @@ function App() {
|
|||||||
removeItem(boxID, itemID);
|
removeItem(boxID, itemID);
|
||||||
}
|
}
|
||||||
const handleSubmitItem = (event) => {
|
const handleSubmitItem = (event) => {
|
||||||
|
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
clearForm();
|
clearForm();
|
||||||
const formData = new FormData(event.target);
|
const formData = new FormData(event.target);
|
||||||
@@ -140,12 +171,13 @@ function App() {
|
|||||||
handleAddItem: handleAddItem,
|
handleAddItem: handleAddItem,
|
||||||
};
|
};
|
||||||
|
|
||||||
const formNewBox =
|
const boxForm =
|
||||||
<form onSubmit={boxHandler.handleSubmit}>
|
<form onSubmit={boxHandler.handleSubmit}>
|
||||||
|
<input id="id" name="id" value={boxID} readOnly hidden></input>
|
||||||
<legend>New Box</legend>
|
<legend>New Box</legend>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<label htmlFor="box-name">name
|
<label htmlFor="box-name">name
|
||||||
<input onChange={boxHandler.handleName} value={boxName} id="box-name" name="name" type="text" />
|
<input autoFocus onChange={boxHandler.handleName} value={boxName} id="box-name" name="name" type="text" />
|
||||||
</label>
|
</label>
|
||||||
<label htmlFor="box-description">description
|
<label htmlFor="box-description">description
|
||||||
<input onChange={boxHandler.handleDescription} value={boxDescription} id="box-description" name="description" type="text" />
|
<input onChange={boxHandler.handleDescription} value={boxDescription} id="box-description" name="description" type="text" />
|
||||||
@@ -156,22 +188,20 @@ function App() {
|
|||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{showBox && <Modal />}
|
{<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 (
|
||||||
<Box
|
<Box
|
||||||
key={index}
|
key={index}
|
||||||
boxData={box}
|
boxData={box}
|
||||||
handleRemoveBox={boxHandler.handleRemove}
|
boxHandler={boxHandler}
|
||||||
itemHandler={itemHandler}
|
itemHandler={itemHandler}
|
||||||
/>
|
/>
|
||||||
);
|
);
|
||||||
})}
|
})}
|
||||||
</ul>
|
</ul>
|
||||||
{!showFormBox && <button onClick={handleAddBox}>add box</button>}
|
{<button onClick={handleAddBox}>add box</button>}
|
||||||
{showFormBox && formNewBox}
|
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
+7
-3
@@ -1,7 +1,8 @@
|
|||||||
import React, { useState } from 'react';
|
import React, { useState } from 'react';
|
||||||
import Item from './item';
|
import Item from './item';
|
||||||
|
import Modal from './modal';
|
||||||
|
|
||||||
const Box = ({ boxData, handleRemoveBox, itemHandler }) => {
|
const Box = ({ boxData, boxHandler, itemHandler }) => {
|
||||||
const [itemName, setItemName] = useState('');
|
const [itemName, setItemName] = useState('');
|
||||||
const [itemDescription, setItemDescription] = useState('');
|
const [itemDescription, setItemDescription] = useState('');
|
||||||
|
|
||||||
@@ -26,6 +27,8 @@ const Box = ({ boxData, handleRemoveBox, itemHandler }) => {
|
|||||||
</form>
|
</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">
|
||||||
@@ -42,9 +45,10 @@ const Box = ({ boxData, handleRemoveBox, itemHandler }) => {
|
|||||||
handleRemove={itemHandler.handleRemove} />
|
handleRemove={itemHandler.handleRemove} />
|
||||||
}))}
|
}))}
|
||||||
</ul>
|
</ul>
|
||||||
<button className="remove-box" onClick={() => handleRemoveBox(boxData._id)}>remove box</button>
|
<button className="edit-box" onClick={() => boxHandler.handleEdit(boxData._id)}>edit</button>
|
||||||
{boxData.showItemForm && formNewItem}
|
<button className="remove-box" onClick={() => boxHandler.handleRemove(boxData._id)}>remove</button>
|
||||||
</li>
|
</li>
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -1,9 +1,9 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const item = ({ data, handleRemove, boxID }) => {
|
const item = ({ data, handleRemove, handleEdit, boxID }) => {
|
||||||
console.log(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={() => handleRemove(boxID, data._id)}>remove</button>
|
<button onClick={() => handleRemove(boxID, data._id)}>remove</button>
|
||||||
</li>);
|
</li>);
|
||||||
};
|
};
|
||||||
|
|||||||
+4
-4
@@ -1,14 +1,14 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import './styles/modal.css';
|
import './styles/modal.css';
|
||||||
|
|
||||||
const Modal = () => {
|
const Modal = ({show, content}) => {
|
||||||
return (
|
return show ?
|
||||||
<div className='modal-container'>
|
<div className='modal-container'>
|
||||||
<div className='modal-content'>
|
<div className='modal-content'>
|
||||||
<h1>modal title</h1>
|
{content}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
: null;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Modal;
|
export default Modal;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
form > fieldset > label {
|
form > fieldset > label {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -12,9 +12,10 @@ li {
|
|||||||
}
|
}
|
||||||
li.item {
|
li.item {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 1fr 4em;
|
grid-template-columns: 1fr auto auto;
|
||||||
column-gap: 1em;
|
column-gap: 0.1em;
|
||||||
margin: 0.4em 0 0 0;
|
margin: 0.4em 0 0 0;
|
||||||
|
padding: 0;
|
||||||
}
|
}
|
||||||
h1, h2, h3, h4 {
|
h1, h2, h3, h4 {
|
||||||
font-weight:normal;
|
font-weight:normal;
|
||||||
@@ -26,7 +27,7 @@ border: none;
|
|||||||
font-size: 0.8em;
|
font-size: 0.8em;
|
||||||
padding: 0.1em;
|
padding: 0.1em;
|
||||||
}
|
}
|
||||||
button.remove-box {
|
button.remove-box, button.edit-box {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
}
|
}
|
||||||
button:hover {
|
button:hover {
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
.modal-container {
|
.modal-container {
|
||||||
|
top: 0;
|
||||||
|
left: 0;
|
||||||
|
position: absolute;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
||||||
justify-content: center;
|
justify-content: center;
|
||||||
align-items: center;
|
align-items: center;
|
||||||
position: absolute;
|
|
||||||
height: 100%;
|
height: 100%;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
background-color: rgba(0,0,0,0.2);
|
background-color: rgba(0,0,0,0.2);
|
||||||
|
|||||||
+7
-1
@@ -55,7 +55,13 @@ app.post('/box', async (request, response) => {
|
|||||||
const updatedBoxes = await getAllBoxes();
|
const updatedBoxes = await getAllBoxes();
|
||||||
response.send(updatedBoxes);
|
response.send(updatedBoxes);
|
||||||
});
|
});
|
||||||
|
//edit box
|
||||||
|
app.put('/box/:id', async (request, response) => {
|
||||||
|
const id = request.params.id;
|
||||||
|
await Box.updateOne({_id: id},{ $set: request.body});
|
||||||
|
const updatedBoxes = await getAllBoxes();
|
||||||
|
response.send(updatedBoxes);
|
||||||
|
});
|
||||||
//delete box
|
//delete box
|
||||||
app.delete('/box/:id', async (request, response) => {
|
app.delete('/box/:id', async (request, response) => {
|
||||||
await Box.deleteOne({_id: request.params.id});
|
await Box.deleteOne({_id: request.params.id});
|
||||||
|
|||||||
Reference in New Issue
Block a user