filter boxes
This commit is contained in:
+53
-15
@@ -4,10 +4,11 @@ import Box from './box';
|
|||||||
import './styles/app.css';
|
import './styles/app.css';
|
||||||
import api from './api';
|
import api from './api';
|
||||||
import Modal from './modal';
|
import Modal from './modal';
|
||||||
|
import item from './item';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
const [data, setData] = useState();
|
const [data, setData] = useState([]);
|
||||||
const [boxID, setBoxID] = useState();
|
const [boxID, setBoxID] = useState();
|
||||||
const [boxName, setBoxName] = useState('');
|
const [boxName, setBoxName] = useState('');
|
||||||
const [boxDescription, setBoxDescription] = useState('');
|
const [boxDescription, setBoxDescription] = useState('');
|
||||||
@@ -21,6 +22,11 @@ function App() {
|
|||||||
const [itemName, setItemName] = useState('');
|
const [itemName, setItemName] = useState('');
|
||||||
const [itemDescription, setItemDescription] = useState('');
|
const [itemDescription, setItemDescription] = useState('');
|
||||||
|
|
||||||
|
const [filter, setFilter] = useState('');
|
||||||
|
const [filteredData, setFilteredData] = useState([]);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
const handleName = (e) => {
|
const handleName = (e) => {
|
||||||
setItemName(e.target.value)
|
setItemName(e.target.value)
|
||||||
}
|
}
|
||||||
@@ -36,10 +42,20 @@ function App() {
|
|||||||
fetchData();
|
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 () => {
|
const fetchDataAsync = async () => {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(api.baseApiUrl);
|
const resp = await axios.get(api.baseApiUrl);
|
||||||
setData(resp.data);
|
setData(resp.data);
|
||||||
|
setFilteredData(resp.data);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
@@ -89,7 +105,6 @@ function App() {
|
|||||||
const handleSubmitBox = (event) => {
|
const handleSubmitBox = (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
setShowModalBox(false);
|
setShowModalBox(false);
|
||||||
clearForm();
|
|
||||||
const boxData = {
|
const boxData = {
|
||||||
id: boxID,
|
id: boxID,
|
||||||
name: boxName,
|
name: boxName,
|
||||||
@@ -99,6 +114,8 @@ function App() {
|
|||||||
editBox(boxData);
|
editBox(boxData);
|
||||||
else
|
else
|
||||||
createBox(boxData);
|
createBox(boxData);
|
||||||
|
|
||||||
|
clearFormBox();
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -116,13 +133,14 @@ function App() {
|
|||||||
setBoxID(id);
|
setBoxID(id);
|
||||||
setShowModalBox(true);
|
setShowModalBox(true);
|
||||||
}
|
}
|
||||||
const clearForm = () => {
|
const clearFormBox = () => {
|
||||||
setEditBoxBool(false);
|
setEditBoxBool(false);
|
||||||
setBoxName('');
|
setBoxName('');
|
||||||
setBoxDescription('');
|
setBoxDescription('');
|
||||||
setBoxID(null);
|
setBoxID(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
const boxHandler = {
|
const boxHandler = {
|
||||||
handleSubmit: handleSubmitBox,
|
handleSubmit: handleSubmitBox,
|
||||||
handleRemove: handleRemoveBox,
|
handleRemove: handleRemoveBox,
|
||||||
@@ -130,6 +148,10 @@ function App() {
|
|||||||
handleDescription: handleBoxDescription,
|
handleDescription: handleBoxDescription,
|
||||||
handleEdit: handleEditBox
|
handleEdit: handleEditBox
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleModalCloseBox = () => {
|
||||||
|
setShowModalBox(false);
|
||||||
|
}
|
||||||
// ITEM FUNCTIONS ///////////////////////////////////////////////////////////////
|
// ITEM FUNCTIONS ///////////////////////////////////////////////////////////////
|
||||||
|
|
||||||
const createItem = async (boxID, itemData) => {
|
const createItem = async (boxID, itemData) => {
|
||||||
@@ -201,11 +223,19 @@ function App() {
|
|||||||
else
|
else
|
||||||
createItem(boxID, itemData);
|
createItem(boxID, itemData);
|
||||||
|
|
||||||
clearForm();
|
clearFormItem();
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const handleModalCloseItem = () => {
|
||||||
|
setShowModalItem(false);
|
||||||
|
}
|
||||||
|
const clearFormItem = () => {
|
||||||
|
setItemID(undefined);
|
||||||
|
setItemName('');
|
||||||
|
setItemDescription('');
|
||||||
|
}
|
||||||
|
|
||||||
const itemHandler = {
|
const itemHandler = {
|
||||||
handleSubmit: handleSubmitItem,
|
handleSubmit: handleSubmitItem,
|
||||||
handleRemove: handleRemoveItem,
|
handleRemove: handleRemoveItem,
|
||||||
@@ -213,6 +243,11 @@ function App() {
|
|||||||
handleEdit: handleEditItem,
|
handleEdit: handleEditItem,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const handleFilterChange = (event) => {
|
||||||
|
const filterText = event.target.value;
|
||||||
|
setFilter(filterText);
|
||||||
|
}
|
||||||
|
|
||||||
const boxForm =
|
const boxForm =
|
||||||
<form onSubmit={boxHandler.handleSubmit}>
|
<form onSubmit={boxHandler.handleSubmit}>
|
||||||
<input id="id" name="id" value={boxID} readOnly hidden></input>
|
<input id="id" name="id" value={boxID} readOnly hidden></input>
|
||||||
@@ -228,9 +263,9 @@ function App() {
|
|||||||
<button type="submit">submit</button>
|
<button type="submit">submit</button>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
const itemForm = <form onSubmit={itemHandler.handleSubmit}>
|
const itemForm = <form onSubmit={itemHandler.handleSubmit}>
|
||||||
<legend>New Item</legend>
|
<legend>New Item</legend>
|
||||||
<fieldset>
|
<fieldset>
|
||||||
<input id="id" name="id" value={itemID} readOnly hidden></input>
|
<input id="id" name="id" value={itemID} readOnly hidden></input>
|
||||||
<label htmlFor="item-name">name
|
<label htmlFor="item-name">name
|
||||||
<input onChange={handleName} value={itemName} id="item-name" name="name" type="text"></input>
|
<input onChange={handleName} value={itemName} id="item-name" name="name" type="text"></input>
|
||||||
@@ -238,15 +273,19 @@ const itemForm = <form onSubmit={itemHandler.handleSubmit}>
|
|||||||
<label htmlFor="item-description">description
|
<label htmlFor="item-description">description
|
||||||
<input onChange={handleDescription} value={itemDescription} id="item-description" name="description" type="text"></input>
|
<input onChange={handleDescription} value={itemDescription} id="item-description" name="description" type="text"></input>
|
||||||
</label>
|
</label>
|
||||||
</fieldset>
|
</fieldset>
|
||||||
<button type="submit">submit</button>
|
<button type="submit">submit</button>
|
||||||
</form>
|
</form>
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{<Modal show={showModalItem} edit={editItemBool} content={itemForm} />}
|
{<Modal show={showModalItem} edit={editItemBool} content={itemForm} handleClose={handleModalCloseItem} />}
|
||||||
{<Modal show={showModalBox} edit={editBoxBool} content={boxForm} />}
|
{<Modal show={showModalBox} edit={editBoxBool} content={boxForm} handleClose={handleModalCloseBox} />}
|
||||||
|
|
||||||
|
<label htmlFor="box-filter">filter:</label>
|
||||||
|
<input type="search" id="box-filter" name="filter" value={filter} onChange={handleFilterChange} />
|
||||||
<ul className="list-box">
|
<ul className="list-box">
|
||||||
{data && data.map((box, index) => {
|
{filter}
|
||||||
|
{filteredData.map((box, index) => {
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
key={index}
|
key={index}
|
||||||
@@ -260,7 +299,6 @@ const itemForm = <form onSubmit={itemHandler.handleSubmit}>
|
|||||||
{<button onClick={handleAddBox}>add box</button>}
|
{<button onClick={handleAddBox}>add box</button>}
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
|||||||
+10
-2
@@ -1,12 +1,20 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
import './styles/modal.css';
|
import './styles/modal.css';
|
||||||
|
|
||||||
const Modal = ({show, content}) => {
|
const Modal = ({show, content, handleClose}) => {
|
||||||
|
const handleClickOff = (event) => {
|
||||||
|
if(event.target.classList.contains('modal-container'))
|
||||||
|
handleClose();
|
||||||
|
|
||||||
|
}
|
||||||
return show ?
|
return show ?
|
||||||
<div className='modal-container'>
|
<div className='modal-container' onClick={handleClickOff}>
|
||||||
<div className='modal-content'>
|
<div className='modal-content'>
|
||||||
{content}
|
{content}
|
||||||
</div>
|
</div>
|
||||||
|
<div className='modal-actions'>
|
||||||
|
<button type='button' onClick={handleClose}>close</button>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
: null;
|
: null;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/*
|
||||||
form > fieldset > label {
|
form > fieldset > label {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
@@ -53,3 +54,4 @@ ul.list-box-item {
|
|||||||
p {
|
p {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
Reference in New Issue
Block a user