diff --git a/client/src/app.js b/client/src/app.js index b249b35..8476133 100644 --- a/client/src/app.js +++ b/client/src/app.js @@ -4,10 +4,11 @@ import Box from './box'; import './styles/app.css'; import api from './api'; import Modal from './modal'; +import item from './item'; function App() { - const [data, setData] = useState(); + const [data, setData] = useState([]); const [boxID, setBoxID] = useState(); const [boxName, setBoxName] = useState(''); const [boxDescription, setBoxDescription] = useState(''); @@ -21,11 +22,16 @@ function App() { const [itemName, setItemName] = useState(''); const [itemDescription, setItemDescription] = useState(''); + const [filter, setFilter] = useState(''); + const [filteredData, setFilteredData] = useState([]); + + + const handleName = (e) => { - setItemName(e.target.value) + setItemName(e.target.value) } const handleDescription = (e) => { - setItemDescription(e.target.value) + setItemDescription(e.target.value) } //fetch all box data @@ -36,10 +42,20 @@ function App() { fetchData(); }, []); + useEffect(() => { + const filtered = data.filter((box) => + box.name.includes(filter) || + box.description.includes(filter) || + box.item.filter(item => item.name.includes(filter) || + item.description.includes(filter)).length) + setFilteredData(filtered); + }, [filter, data]); + const fetchDataAsync = async () => { try { const resp = await axios.get(api.baseApiUrl); setData(resp.data); + setFilteredData(resp.data); } catch (error) { console.log(error); @@ -89,7 +105,6 @@ function App() { const handleSubmitBox = (event) => { event.preventDefault(); setShowModalBox(false); - clearForm(); const boxData = { id: boxID, name: boxName, @@ -99,6 +114,8 @@ function App() { editBox(boxData); else createBox(boxData); + + clearFormBox(); return false; } @@ -116,13 +133,14 @@ function App() { setBoxID(id); setShowModalBox(true); } - const clearForm = () => { + const clearFormBox = () => { setEditBoxBool(false); setBoxName(''); setBoxDescription(''); setBoxID(null); } + const boxHandler = { handleSubmit: handleSubmitBox, handleRemove: handleRemoveBox, @@ -130,6 +148,10 @@ function App() { handleDescription: handleBoxDescription, handleEdit: handleEditBox }; + + const handleModalCloseBox = () => { + setShowModalBox(false); + } // ITEM FUNCTIONS /////////////////////////////////////////////////////////////// const createItem = async (boxID, itemData) => { @@ -167,7 +189,7 @@ function App() { console.log(error); } } - + const handleAddItem = (id) => { setBoxID(id); setEditItemBool(false); @@ -181,7 +203,7 @@ function App() { setBoxID(boxID); setItemName(item.name); setItemDescription(item.description); - + setEditItemBool(true); setShowModalItem(true); } @@ -201,11 +223,19 @@ function App() { else createItem(boxID, itemData); - clearForm(); - + clearFormItem(); return false; } + const handleModalCloseItem = () => { + setShowModalItem(false); + } + const clearFormItem = () => { + setItemID(undefined); + setItemName(''); + setItemDescription(''); + } + const itemHandler = { handleSubmit: handleSubmitItem, handleRemove: handleRemoveItem, @@ -213,6 +243,11 @@ function App() { handleEdit: handleEditItem, }; + const handleFilterChange = (event) => { + const filterText = event.target.value; + setFilter(filterText); + } + const boxForm =
-const itemForm = return ( <> - {