-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from JuzerShakir/edit-page
Create Edit Page
- Loading branch information
Showing
15 changed files
with
304 additions
and
115 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,27 @@ | ||
import { useState } from "react"; | ||
// import assets | ||
// import editIcon from "./assets/edit.svg"; | ||
// import homeIcon from "./assets/home.svg"; | ||
// components | ||
import Header from "./components/Header"; | ||
import ScrapItems from "./components/ScrapItems"; | ||
import GrandTotal from "./components/GrandTotal"; | ||
import Footer from "./components/Footer"; | ||
import { RouterProvider, createBrowserRouter } from "react-router-dom"; | ||
import { scrapItemsList } from "./scrapItemsList"; | ||
|
||
const scrapItems = [ | ||
{ id: 1, name: "plastic", earningPerKg: 15, earnings: 0 }, | ||
{ id: 2, name: "cardboard", earningPerKg: 8, earnings: 0 }, | ||
{ id: 3, name: "paper", earningPerKg: 10, earnings: 0 }, | ||
{ id: 4, name: "steel", earningPerKg: 40, earnings: 0 }, | ||
{ id: 5, name: "iron", earningPerKg: 30, earnings: 0 }, | ||
{ id: 6, name: "german", earningPerKg: 120, earnings: 0 }, | ||
]; | ||
|
||
function roundToNearestTenth(number) { | ||
return Math.round(number * 10) / 10; | ||
} | ||
// pages | ||
import HomePage from "./pages/HomePage"; | ||
import Edit from "./pages/Edit"; | ||
|
||
function App() { | ||
const [items, setItems] = useState(scrapItems); | ||
const newTotalEarnings = items.reduce( | ||
(accumulator, item) => roundToNearestTenth(accumulator + item.earnings), | ||
0 | ||
); | ||
|
||
function handleItemEarnings(id, weight) { | ||
// rescue against negative values | ||
if (weight < 0) return; | ||
const [items, setItems] = useState(scrapItemsList); | ||
|
||
setItems((items) => | ||
items.map((item) => | ||
item.id === id | ||
? { | ||
...item, | ||
earnings: roundToNearestTenth(weight * item.earningPerKg), | ||
} | ||
: item | ||
) | ||
); | ||
} | ||
// routing | ||
const router = createBrowserRouter([ | ||
{ | ||
path: "/", | ||
element: <HomePage items={items} setItems={setItems} />, | ||
}, | ||
{ | ||
path: "/edit", | ||
element: <Edit items={items} setItems={setItems} />, | ||
}, | ||
]); | ||
|
||
return ( | ||
<> | ||
<Header /> | ||
<ScrapItems items={items} onHandleItemEarnings={handleItemEarnings} /> | ||
<GrandTotal totalEarnings={newTotalEarnings} /> | ||
<Footer /> | ||
</> | ||
); | ||
return <RouterProvider router={router} />; | ||
} | ||
|
||
export default App; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { Link } from "react-router-dom"; | ||
import { ValidateAsideProps } from "./../propValidations"; | ||
|
||
Aside.propTypes = ValidateAsideProps; | ||
|
||
export default function Aside({ icon, alt, linkTo }) { | ||
return ( | ||
<aside className="w-1/4 self-end"> | ||
<Link to={linkTo}> | ||
<img src={icon} alt={alt} className="w-7 cursor-pointer" /> | ||
</Link> | ||
</aside> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import { | ||
ValidateItemsStateProps, | ||
ValidateItemEarningsPerKgProps, | ||
} from "../propValidations"; | ||
import smallRupeeIcon from "../assets/small_rupee.svg"; | ||
|
||
ScrapItemsEditEarning.propTypes = ValidateItemsStateProps; | ||
Item.propTypes = ValidateItemEarningsPerKgProps; | ||
|
||
export default function ScrapItemsEditEarning({ items, setItems }) { | ||
function handleItemEarningPerKg(id, amount) { | ||
// rescue against negative values | ||
if (amount < 1) return; | ||
|
||
const roundingAmount = Math.round(amount); | ||
|
||
setItems((items) => | ||
items.map((item) => | ||
item.id === id | ||
? { | ||
...item, | ||
earningPerKg: roundingAmount, | ||
earnings: roundingAmount * item.weight, | ||
} | ||
: item | ||
) | ||
); | ||
} | ||
|
||
return ( | ||
<main> | ||
<ul | ||
id="scrapItems" | ||
className="grow flex flex-col justify-center gap-5 px-6" | ||
> | ||
{items.map((item) => ( | ||
<Item | ||
key={item.id} | ||
item={item} | ||
onHandleItemEarningPerKg={handleItemEarningPerKg} | ||
/> | ||
))} | ||
</ul> | ||
</main> | ||
); | ||
} | ||
|
||
function Item({ item, onHandleItemEarningPerKg }) { | ||
return ( | ||
<li className="flex justify-end"> | ||
<form | ||
className="flex items-center gap-x-12" | ||
onSubmit={(e) => e.preventDefault()} | ||
> | ||
{/* title */} | ||
<label | ||
htmlFor={item.name} | ||
className="patrick-hand-regular text-3xl text-green-900" | ||
> | ||
{item.name} | ||
</label> | ||
|
||
{/* weight input */} | ||
<span className="flex gap-x-1 items-end"> | ||
<img | ||
src={smallRupeeIcon} | ||
className="h-5 self-center" | ||
alt="rupee icon" | ||
/> | ||
<input | ||
type="number" | ||
id={item.name} | ||
name={item.name} | ||
className="nunito-sans-semibold bg-amber-100 w-14 text-end text-amber-900 pr-1 border-b-2 border-yellow-600 focus:border-yellow-900 focus:outline-none" | ||
placeholder={item.earningPerKg} | ||
min="1" | ||
step="1" | ||
onChange={(e) => | ||
onHandleItemEarningPerKg(item.id, Number(e.target.value)) | ||
} | ||
></input> | ||
<strong className="roboto-condensed-normal">per kg</strong> | ||
</span> | ||
</form> | ||
</li> | ||
); | ||
} |
Oops, something went wrong.