Skip to content

Commit

Permalink
Merge pull request #1 from Chandan9898Kumar/NewRepo
Browse files Browse the repository at this point in the history
Feat: Added new repo which depicts update and remove of data.
  • Loading branch information
Chandan9898Kumar authored Oct 29, 2022
2 parents 6cf7d38 + 0766011 commit 88a1cb3
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 21 deletions.
21 changes: 10 additions & 11 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import './App.css';
import { BrowserRouter, Route, Routes } from "react-router-dom";
import Homes from './Home';
import ObjectForm from './Components/ObjectsChanges';
import StateList from './Components/StateChanges'
import StateList from './Components/StateChanges';
import ShoppingCart from './Components/UpdateCart'
function App() {
return (
<div className="App">
<BrowserRouter>
<Routes>
<Route exact path='/' element={<Homes />} />
<Route exact path='/ObjectChange' element={<ObjectForm />} />
<BrowserRouter>
<Routes>
<Route exact path='/' element={<Homes />} />
<Route exact path='/ObjectChange' element={<ObjectForm />} />
<Route exact path='/StateChange' element={<StateList />} />
<Route exact path='/ShpingCart' element={<ShoppingCart />} />
</Routes>
</BrowserRouter>

<Route exact path='/StateChange' element={<StateList />} />


</Routes>
</BrowserRouter>

</div>
);
}
Expand Down
93 changes: 93 additions & 0 deletions src/Components/UpdateCart.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
import { useState } from 'react';

const initialProducts = [{
id: 0,
name: 'Baklava',
count: 1,
}, {
id: 1,
name: 'Cheese',
count: 5,
}, {
id: 2,
name: 'Spaghetti',
count: 2,
}];

const ShoppingCart = () => {
const [products, setProducts] = useState(initialProducts)


// Update an item in the shopping cart
function handleIncreaseClick(productId) {
setProducts(products.map((item) =>{
if(item.id===productId){
return {...item,count:item.count+1}
}else{
return item
}
}))

// OR We can do it like below as well .

// const data = products.map((item) => {
// if (item.id === productId) {
// return { ...item, count: item.count + 1 }
// } else {
// return item
// }
// })
// setProducts(data)

}

//
const handleDecreaseClick =(ProductId)=>{
const data=products.map((item)=>{
if(item.id===ProductId){
return {...item,count:item.count-1}
}else{
return item
}
})
// if your count is 0 then that item should be removed, uncomment below and check.

// const result=data.filter((item)=> item.count>0)
// setProducts(result)
setProducts(data)



// setProducts(products.map((item) =>{
// if(item.id===productId){
// return {...item,count:item.count+1}
// }else{
// return item
// }

// }))
}

return (
<ul>
{products.map(product => (
<li key={product.id}>
{product.name}
{' '}
(<b>{product.count}</b>)
<button onClick={() => {
handleIncreaseClick(product.id);
}}>
+
</button>
<button onClick={() => {
handleDecreaseClick(product.id);
}}>
-
</button>
</li>
))}
</ul>
);
}
export default ShoppingCart;
19 changes: 9 additions & 10 deletions src/Home.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import React from 'react'
import { NavLink } from "react-router-dom"
const Homes=()=>{
return(
<>
<label>Home page</label><br /><br />
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br />
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br />


</>
)
const Homes = () => {
return (
<>
<label>Home page</label><br /><br />
<NavLink to='/ObjectChange'>Change Nested Object</NavLink><br /><br />
<NavLink to='/StateChange'>Change Nested State</NavLink><br /><br />
<NavLink to='/ShpingCart'>Change Nested State of Cart</NavLink><br /><br />
</>
)
}
export default Homes

0 comments on commit 88a1cb3

Please sign in to comment.