diff --git a/client/src/app.js b/client/src/app.js index d2596a7..56f0024 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -8,10 +8,16 @@ import Modal from './modal'; function App() { const [data, setData] = useState(); + const [boxID, setBoxID] = useState(); const [boxName, setBoxName] = useState(''); const [boxDescription, setBoxDescription] = useState(''); - const [showFormBox, setShowFormBox] = useState(false); - const [showBox, setShowBox] = useState(false); + const [showModalBox, setShowModalBox] = useState(false); + const [showModalItem, setShowModalItem] = useState(false); + + const [editBoxBool, setEditBoxBool] = useState(false); + const [editDataBox, setEditDataBox] = useState({}); + + //fetch all box data useEffect(() => { const fetchData = async () => { @@ -39,7 +45,15 @@ function App() { 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) => { try { const response = await axios.delete(`${api.baseApiUrl}/${id}`); @@ -52,23 +66,27 @@ function App() { } } - const handleBoxName = (e) => { + const handleNameBox = (e) => { setBoxName(e.target.value); } - + const handleBoxDescription = (e) => { setBoxDescription(e.target.value); } - + const handleSubmitBox = (event) => { event.preventDefault(); - setShowFormBox(false); + setShowModalBox(false); clearForm(); const boxData = { + id: boxID, name: boxName, description: boxDescription }; - createBox(boxData); + if (editBoxBool) + editBox(boxData); + else + createBox(boxData); return false; } @@ -76,41 +94,53 @@ function App() { removeBox(id); } const handleAddBox = () => { - setShowFormBox(true); + //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); + setBoxName(box.name); + setBoxDescription(box.description); + setBoxID(id); + setShowModalBox(true); } - const clearForm = () => { - setBoxName(); - setBoxDescription(); - } const boxHandler = { - handleSubmit: handleSubmitBox, - handleRemove: handleRemoveBox, - handleName: handleBoxName, - handleDescription: handleBoxDescription + handleSubmit: handleSubmitBox, + handleRemove: handleRemoveBox, + handleName: handleNameBox, + handleDescription: handleBoxDescription, + handleEdit: handleEditBox }; - + const createItem = async (id, itemData) => { - try { - const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData); - const boxData = [...data]; - boxData.splice(boxData.findIndex(box => box._id === id),1,response.data); - setData(boxData); - } - catch (error) { - console.log(error); - } + try { + const response = await axios.post(`${api.baseApiUrl}/${id}/item`, itemData); + const boxData = [...data]; + boxData.splice(boxData.findIndex(box => box._id === id), 1, response.data); + setData(boxData); + } + catch (error) { + console.log(error); + } } const removeItem = async (boxID, itemID) => { - try { - const boxData = [...data]; - const response = await axios.delete(`${api.baseApiUrl}/${boxID}/item/${itemID}`); - boxData.splice(boxData.findIndex(box => box._id === boxID),1,response.data); - setData(boxData); - } - catch (error) { - console.log(error); - } + try { + const boxData = [...data]; + const response = await axios.delete(`${api.baseApiUrl}/${boxID}/item/${itemID}`); + boxData.splice(boxData.findIndex(box => box._id === boxID), 1, response.data); + setData(boxData); + } + catch (error) { + console.log(error); + } } const handleAddItem = (id) => { const boxData = [...data]; @@ -119,33 +149,35 @@ function App() { setData(boxData); } const handleRemoveItem = (boxID, itemID) => { - removeItem(boxID, itemID); + removeItem(boxID, itemID); } const handleSubmitItem = (event) => { - event.preventDefault(); - clearForm(); - const formData = new FormData(event.target); - const id = formData.get('id'); - const itemData = { - name: formData.get('name'), - description: formData.get('description') - } - createItem(id, itemData); - return false; + + event.preventDefault(); + clearForm(); + const formData = new FormData(event.target); + const id = formData.get('id'); + const itemData = { + name: formData.get('name'), + description: formData.get('description') + } + createItem(id, itemData); + return false; } const itemHandler = { - handleSubmit: handleSubmitItem, - handleRemove: handleRemoveItem, - handleAddItem: handleAddItem, + handleSubmit: handleSubmitItem, + handleRemove: handleRemoveItem, + handleAddItem: handleAddItem, }; - const formNewBox = + const boxForm =
+ New Box