fix update item, lowercase filtering
This commit is contained in:
+17
-11
@@ -1,10 +1,9 @@
|
|||||||
import React, { useState, useEffect } from 'react';
|
import React, { useState, useEffect } from 'react';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import Box from './box';
|
import Box from './box';
|
||||||
import './styles/app.css';
|
|
||||||
import api from './api';
|
import api from './api';
|
||||||
import Modal from './modal';
|
import Modal from './modal';
|
||||||
import item from './item';
|
import './styles/app.css';
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
|
|
||||||
@@ -42,12 +41,17 @@ function App() {
|
|||||||
fetchData();
|
fetchData();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
//set filtered data on filter or original data change
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
const finalFilter = filter.trim().toLowerCase();
|
||||||
const filtered = data.filter((box) =>
|
const filtered = data.filter((box) =>
|
||||||
box.name.includes(filter) ||
|
box.name.toLowerCase().includes(finalFilter) ||
|
||||||
box.description.includes(filter) ||
|
box.description.toLowerCase().includes(finalFilter) ||
|
||||||
box.item.filter(item => item.name.includes(filter) ||
|
box.item.some(item =>
|
||||||
item.description.includes(filter)).length)
|
(item.name && item.name.toLowerCase().includes(finalFilter)) ||
|
||||||
|
(item.description && item.description.toLowerCase().includes(finalFilter))
|
||||||
|
)
|
||||||
|
)
|
||||||
setFilteredData(filtered);
|
setFilteredData(filtered);
|
||||||
}, [filter, data]);
|
}, [filter, data]);
|
||||||
|
|
||||||
@@ -174,7 +178,7 @@ function App() {
|
|||||||
setData(boxData);
|
setData(boxData);
|
||||||
}
|
}
|
||||||
catch (error) {
|
catch (error) {
|
||||||
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -198,9 +202,9 @@ function App() {
|
|||||||
const handleEditItem = (boxID, itemID) => {
|
const handleEditItem = (boxID, itemID) => {
|
||||||
const box = data.find(box => box._id === boxID);
|
const box = data.find(box => box._id === boxID);
|
||||||
const item = box.item.find(item => item._id === itemID);
|
const item = box.item.find(item => item._id === itemID);
|
||||||
|
|
||||||
setItemID(itemID);
|
|
||||||
setBoxID(boxID);
|
setBoxID(boxID);
|
||||||
|
setItemID(itemID);
|
||||||
setItemName(item.name);
|
setItemName(item.name);
|
||||||
setItemDescription(item.description);
|
setItemDescription(item.description);
|
||||||
|
|
||||||
@@ -218,8 +222,11 @@ function App() {
|
|||||||
name: itemName,
|
name: itemName,
|
||||||
description: itemDescription
|
description: itemDescription
|
||||||
}
|
}
|
||||||
if (editItemBool)
|
if (editItemBool) {
|
||||||
|
console.log('edititem');
|
||||||
editItem(boxID, itemID, itemData);
|
editItem(boxID, itemID, itemData);
|
||||||
|
|
||||||
|
}
|
||||||
else
|
else
|
||||||
createItem(boxID, itemData);
|
createItem(boxID, itemData);
|
||||||
|
|
||||||
@@ -284,7 +291,6 @@ function App() {
|
|||||||
<label htmlFor="box-filter">filter:</label>
|
<label htmlFor="box-filter">filter:</label>
|
||||||
<input type="search" id="box-filter" name="filter" value={filter} onChange={handleFilterChange} />
|
<input type="search" id="box-filter" name="filter" value={filter} onChange={handleFilterChange} />
|
||||||
<ul className="list-box">
|
<ul className="list-box">
|
||||||
{filter}
|
|
||||||
{filteredData.map((box, index) => {
|
{filteredData.map((box, index) => {
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
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, boxHandler, itemHandler }) => {
|
const Box = ({ boxData, boxHandler, itemHandler }) => {
|
||||||
return (
|
return (
|
||||||
|
|||||||
+2
-2
@@ -1,6 +1,6 @@
|
|||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
|
||||||
const item = ({ data, itemHandler, boxID }) => {
|
const Item = ({ data, itemHandler, 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={() => itemHandler.handleEdit(boxID, data._id)}>edit</button>
|
<button onClick={() => itemHandler.handleEdit(boxID, data._id)}>edit</button>
|
||||||
@@ -8,4 +8,4 @@ const item = ({ data, itemHandler, boxID }) => {
|
|||||||
</li>);
|
</li>);
|
||||||
};
|
};
|
||||||
|
|
||||||
export default item;
|
export default Item;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
/*
|
|
||||||
form > fieldset > label {
|
form > fieldset > label {
|
||||||
display: flex;
|
display: flex;
|
||||||
}
|
}
|
||||||
@@ -11,6 +11,7 @@ li {
|
|||||||
list-style: none;
|
list-style: none;
|
||||||
font-size: 11px;
|
font-size: 11px;
|
||||||
}
|
}
|
||||||
|
|
||||||
li.item {
|
li.item {
|
||||||
display: grid;
|
display: grid;
|
||||||
grid-template-columns: 1fr auto auto;
|
grid-template-columns: 1fr auto auto;
|
||||||
@@ -24,9 +25,9 @@ h1, h2, h3, h4 {
|
|||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
button {
|
button {
|
||||||
|
margin: 0.2em;
|
||||||
border: none;
|
border: none;
|
||||||
font-size: 0.8em;
|
padding: 0.4em;
|
||||||
padding: 0.1em;
|
|
||||||
}
|
}
|
||||||
button.remove-box, button.edit-box {
|
button.remove-box, button.edit-box {
|
||||||
width: 100%;
|
width: 100%;
|
||||||
@@ -54,4 +55,3 @@ ul.list-box-item {
|
|||||||
p {
|
p {
|
||||||
margin: 0;
|
margin: 0;
|
||||||
}
|
}
|
||||||
*/
|
|
||||||
+5
-3
@@ -66,7 +66,6 @@ app.put('/box/:id', async (request, response) => {
|
|||||||
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});
|
||||||
const updatedBoxes = await getAllBoxes();
|
const updatedBoxes = await getAllBoxes();
|
||||||
console.log(updatedBoxes);
|
|
||||||
response.send(updatedBoxes);
|
response.send(updatedBoxes);
|
||||||
});
|
});
|
||||||
|
|
||||||
@@ -84,8 +83,11 @@ app.post('/box/:id/item', async (request, response) => {
|
|||||||
app.put('/box/:boxId/item/:itemId', async (request, response) => {
|
app.put('/box/:boxId/item/:itemId', async (request, response) => {
|
||||||
const boxID = request.params.boxId;
|
const boxID = request.params.boxId;
|
||||||
const itemID = request.params.itemId;
|
const itemID = request.params.itemId;
|
||||||
await Box.updateOne({ 'item._id': itemID }, {
|
await Box.updateOne({ '_id': boxID, 'item._id': itemID }, {
|
||||||
$set: { item: request.body }
|
$set: {
|
||||||
|
'item.$.name': request.body.name,
|
||||||
|
'item.$.description': request.description
|
||||||
|
}
|
||||||
})
|
})
|
||||||
const updatedBox = await getBox(boxID);
|
const updatedBox = await getBox(boxID);
|
||||||
response.send(updatedBox);
|
response.send(updatedBox);
|
||||||
|
|||||||
Reference in New Issue
Block a user