Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: Added new repo which depicts update and remove of data. #1

Merged
merged 1 commit into from
Oct 29, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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