diff --git a/client/src/app.js b/client/src/app.js index 147a99e..5947d2c 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -7,6 +7,12 @@ import Modal from './modal'; function App() { + const [data, setData] = useState(); + const [boxName, setBoxName] = useState(''); + const [boxDescription, setBoxDescription] = useState(''); + const [showFormBox, setShowFormBox] = useState(false); + const [showBox, setShowBox] = useState(false); + //fetch all box data useEffect(() => { const fetchData = async () => { @@ -15,13 +21,6 @@ function App() { fetchData(); }, []); - const [data, setData] = useState(); - - const [boxName, setBoxName] = useState(''); - const [boxDescription, setBoxDescription] = useState(''); - - const [showFormBox, setShowFormBox] = useState(false); - const fetchDataAsync = async () => { try { const resp = await axios.get(api.baseApiUrl); @@ -45,7 +44,9 @@ function App() { const removeBox = async (id) => { try { const response = await axios.delete(`${api.baseApiUrl}/${id}`); - setData(data.filter(box => box._id !== id)); + const storageData = response.data; + console.log(storageData); + setData(storageData); } catch (error) { console.log(error); @@ -88,6 +89,56 @@ function App() { handleName: handleBoxName, handleDescription: handleBoxDescription }; + + + + const [itemName, setItemName] = useState(''); + const [itemDescription, setItemDescription] = useState(''); + + + const createItem = async (id, itemData) => { + try { + const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData); + } + catch (error) { + console.log(error); + } + } + + const removeItem = async (id) => { + try { + const response = await axios.delete(`${api.baseApiUrl}/${data._id}/item/${id}`); + //box data copy + let boxData = Object.assign({}, data); + boxData = Object.assign(boxData, {item: response.data}) + } + catch (error) { + console.log(error); + } + } + const handleAddItem = () => { + //setShowFormItem(true); + } + const handleRemoveItem = (id) => { + removeItem(id); + } + const handleSubmitItem = (event) => { + event.preventDefault(); + clearForm(); + const id = (event.target).querySelector("#id").value; + const itemData = { + name: itemName, + description: itemDescription + } + createItem(id, itemData); + return false; + } + + const itemHandler = { + handleSubmit: handleSubmitItem, + handleRemove: handleRemoveItem, + }; + const formNewBox =