You need to filter trayItems when you map them in decre function. This will remove the items that have a quantity less than or equal to 1.
You need to calculate the total. There are 2 ways to go about it. Either create a new state that stores a calculated total or calculate it on the fly while managing/manipulating the trayItems's totalPrice. I have created a new state rather than making it more complex. You will need to re-render when the total changes so do that calculation inside a useEffect hook for re-render when the total changes.
Here is my solution:
import { Fragment, useEffect, useState } from "react";
import "./styles.css";
const item = [
{
user_id: 1,
item: "biriani",
price: "50",
selling_price: "60",
created_at: "2022-08-29T10:12:58.000000Z",
updated_at: "2022-09-15T14:05:45.000000Z",
tax: "5%",
total_price: "80",
status: 0,
quantity: 0
},
{
user_id: 5,
item: "alfarm",
price: "100",
selling_price: "120",
created_at: "2022-09-07T11:06:23.000000Z",
updated_at: "2022-09-07T11:06:23.000000Z",
tax: "5%",
total_price: "140",
status: 0,
quantity: 0
},
{
user_id: 4,
item: "burger",
price: "80",
selling_price: "80",
created_at: "2022-08-29T10:21:13.000000Z",
updated_at: "2022-09-14T04:46:36.000000Z",
tax: "5%",
total_price: "100",
status: 0,
quantity: 0
},
{
user_id: 8,
item: "sandwich",
price: "100",
selling_price: "120",
created_at: "2022-09-07T13:27:33.000000Z",
updated_at: "2022-09-14T04:46:37.000000Z",
tax: "5",
total_price: "140",
status: 0,
quantity: 0
},
{
user_id: 9,
item: "pizza",
price: "200",
selling_price: "220",
created_at: "2022-09-07T13:27:33.000000Z",
updated_at: "2022-09-07T13:27:33.000000Z",
tax: "5",
total_price: "240",
status: 0,
quantity: 0
},
{
user_id: 10,
item: "chicken sadwich",
price: "200",
selling_price: "220",
created_at: "2022-09-07T13:27:33.000000Z",
updated_at: "2022-09-07T13:27:33.000000Z",
tax: "5",
total_price: "250",
status: 0,
quantity: 0
},
{
user_id: 11,
item: "sharja shake",
price: "60",
selling_price: "70",
created_at: "2022-09-09T06:58:45.000000Z",
updated_at: "2022-09-15T14:05:53.000000Z",
tax: "5%",
total_price: "80",
status: 0,
quantity: 0
},
{
user_id: 1,
item: "biriani",
price: "50",
selling_price: "60",
created_at: "2022-08-29T10:12:58.000000Z",
updated_at: "2022-09-15T06:17:20.000000Z",
tax: "5%",
total_price: "80",
status: 1,
quantity: 0
},
{
user_id: 5,
item: "alfarm",
price: "100",
selling_price: "120",
created_at: "2022-09-07T11:06:23.000000Z",
updated_at: "2022-09-07T11:06:23.000000Z",
tax: "5%",
total_price: "140",
status: 0,
quantity: 0
}
];
export default function App() {
const [trayItems, settrayItems] = useState([]);
console.log(trayItems);
const [total, setTotal] = useState(0);
useEffect(() => {
let newTotal = 0;
trayItems.forEach((stat) => {
newTotal += stat.price * stat.quantity;
});
setTotal(newTotal);
}, [trayItems]);
const handleCheckClick = (ele) => {
const dupe = trayItems.find((obj) => obj.user_id === ele.user_id);
settrayItems(
dupe
? trayItems
: [...trayItems, { ...ele, quantity: (ele.quantity = 1) }]
);
};
const incre = (idd) => {
settrayItems(
trayItems.map((stat) =>
stat.user_id === idd ? { ...stat, quantity: stat.quantity + 1 } : stat
)
);
};
const deleteFromTray = (currentTray, id) => {
return currentTray.filter((item) => item.user_id !== id);
};
const decre = (idd) => {
trayItems.every((stat, index) => {
if (stat.user_id === idd) {
stat.quantity > 1
? settrayItems([
...trayItems.slice(0, index),
{
...stat,
quantity: stat.quantity - 1
},
...trayItems.slice(index + 1, trayItems.length)
])
: settrayItems([
...trayItems.slice(0, index),
...trayItems.slice(index + 1, trayItems.length)
]);
return false;
}
return true;
});
console.log("trashcoder", trayItems);
};
function Tray({ trayItems }) {
return (
<>
<div className="foo">
<table>
<tbody>
<tr>
<th>item</th> <th>price</th> <th>qty</th> <th>total_price</th>
</tr>
{trayItems &&
trayItems.map((ele, index) => {
return (
<tr key={index}>
<td>{ele.item}</td>
<td>{ele.price}</td>
<td>{ele.quantity}</td>
<th>{ele.price * ele.quantity}</th>
<td>
<button onClick={() => incre(ele.user_id)}>+</button>
<button onClick={() => decre(ele.user_id)}>-</button>
</td>
</tr>
);
})}
</tbody>
</table>
</div>
</>
);
}
return (
<>
<div>
{item &&
item
.filter((person) => person.status === 0)
.map((ele, index) => {
return (
<div
style={{ cursor: "pointer", border: "1px solid black" }}
key={index}
className="newmo-card"
onClick={() => handleCheckClick(ele)}
>
{`${ele.item}`}
<br />
<br />
<span> {`${ele.total_price}`}</span>
</div>
);
})}
</div>
<Tray trayItems={trayItems} />
<h3> total amount {total}</h3>
</>
);
}
CodeSandBox link: https://codesandbox.io/s/cold-brook-sj207w?file=/src/dashbord.jsx:0-5500