Skip to content

Thomas' React Greengrocers #131

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
Binary file added diagram.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 10 additions & 10 deletions index.html
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Greengrocers</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.jsx"></script>
</body>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>React Greengrocers</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
3,219 changes: 1,449 additions & 1,770 deletions package-lock.json

Large diffs are not rendered by default.

28 changes: 15 additions & 13 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
{
"name": "react-greengrocers",
"name": "files-for-ts-react",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "vite build",
"lint": "eslint . --ext js,jsx --report-unused-disable-directives --max-warnings 0",
"build": "tsc -b && vite build",
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
"preview": "vite preview"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0"
"react": "^18.3.1",
"react-dom": "^18.3.1"
},
"devDependencies": {
"@types/react": "^18.2.15",
"@types/react-dom": "^18.2.7",
"@vitejs/plugin-react": "^4.0.3",
"eslint": "^8.45.0",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"eslint-plugin-react-refresh": "^0.4.3",
"vite": "^4.4.5"
"@types/react": "^18.3.3",
"@types/react-dom": "^18.3.0",
"@typescript-eslint/eslint-plugin": "^7.13.1",
"@typescript-eslint/parser": "^7.13.1",
"@vitejs/plugin-react": "^4.3.1",
"eslint": "^8.57.0",
"eslint-plugin-react-hooks": "^4.6.2",
"eslint-plugin-react-refresh": "^0.4.7",
"typescript": "^5.2.2",
"vite": "^5.3.1"
}
}
44 changes: 0 additions & 44 deletions src/App.jsx

This file was deleted.

41 changes: 41 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import './styles/reset.css'
import './styles/index.css'
import { useState } from 'react'

import { initialStoreItems, StoreItemType } from './store-items'
import StoreList from './StoreList'
import CartList from './CartList'

export type CartItemType = {
count: number
} & StoreItemType

export default function App() {
const [storeItems, setStoreItems] =
useState<StoreItemType[]>(initialStoreItems)
const [cartItems, setCartItems] = useState<CartItemType[]>([])

return (
<>
<StoreList
storeItems={storeItems}
cartItems={cartItems}
setCartItems={setCartItems}
/>
<CartList cartItems={cartItems} setCartItems={setCartItems} />
<div>
Icons made by
<a
href='https://www.flaticon.com/authors/icongeek26'
title='Icongeek26'
>
Icongeek26
</a>
from
<a href='https://www.flaticon.com/' title='Flaticon'>
www.flaticon.com
</a>
</div>
</>
)
}
74 changes: 74 additions & 0 deletions src/CartItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { CartItemType } from './App'
import { CartListProps } from './CartList'

type CartItemProps = {
cartItem: CartItemType
} & CartListProps

export default function CartItem({
cartItem,
cartItems,
setCartItems,
}: CartItemProps) {
const addToCount = () => {
setCartItems(
cartItems.map((cartItemFromState) => {
if (cartItem.id === cartItemFromState.id) {
return {
...cartItem,
count: cartItem.count + 1,
}
} else {
return cartItemFromState
}
})
)
}

const removeFromCount = () => {
if (cartItem.count <= 1) {
setCartItems(
cartItems.filter(
(cartItemFromState) => cartItemFromState.id != cartItem.id
)
)
} else {
setCartItems(
cartItems.map((cartItemFromState) => {
if (cartItem.id === cartItemFromState.id) {
return {
...cartItem,
count: cartItem.count - 1,
}
} else {
return cartItemFromState
}
})
)
}
}

return (
<li>
<img
className='cart--item-icon'
src={`assets/icons/${cartItem.id}.svg`}
alt={cartItem.id}
/>
<p>{cartItem.name[0].toUpperCase() + cartItem.name.slice(1)}</p>
<button
className='quantity-btn remove-btn center'
onClick={removeFromCount}
>
-
</button>
<span className='quantity-text center'>{cartItem.count}</span>
<button
className='quantity-btn add-btn center'
onClick={addToCount}
>
+
</button>
</li>
)
}
44 changes: 44 additions & 0 deletions src/CartList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { SetStateAction } from 'react'
import { CartItemType } from './App'
import CartItem from './CartItem'

export type CartListProps = {
cartItems: CartItemType[]
setCartItems: React.Dispatch<SetStateAction<CartItemType[]>>
}

export default function CartList({ cartItems, setCartItems }: CartListProps) {
const calculateTotal = () => {
let totalAmount = 0
for (let cartItem of cartItems) {
totalAmount += cartItem.count * cartItem.price
}
return totalAmount.toFixed(2)
}

return (
<main id='cart'>
<h2>Your Cart</h2>
<div className='cart--item-list-container'>
<ul className='item-list cart--item-list'>
{cartItems.map((cartItem) => (
<CartItem
key={cartItem.id}
cartItem={cartItem}
cartItems={cartItems}
setCartItems={setCartItems}
/>
))}
</ul>
</div>
<div className='total-section'>
<div>
<h3>Total</h3>
</div>
<div>
<span className='total-number'>{`£${calculateTotal()}`}</span>
</div>
</div>
</main>
)
}
60 changes: 60 additions & 0 deletions src/StoreItem.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { CartItemType } from './App'
import { StoreItemType } from './store-items'
import { StoreProps } from './StoreList'

type StoreItemProps = {
storeItem: StoreItemType
} & StoreProps

export default function StoreItem({
storeItem,
cartItems,
setCartItems,
}: StoreItemProps) {
const addToCart = (): void => {
const matchingItem = cartItems.find((cartItem: CartItemType) =>
cartItem.id.match(storeItem.id)
)
if (!matchingItem && !cartItems) {
setCartItems([
{
...storeItem,
count: 1,
},
])
} else if (!matchingItem) {
setCartItems([
...cartItems,
{
...storeItem,
count: 1,
},
])
} else {
setCartItems(
cartItems.map((cartItem) => {
if (cartItem.id === storeItem.id) {
return {
...storeItem,
count: cartItem.count + 1,
}
} else {
return cartItem
}
})
)
}
}

return (
<li>
<div className='store--item-icon'>
<img
src={`/assets/icons/${storeItem.id}.svg`}
alt={storeItem.name}
/>
</div>
<button onClick={() => addToCart()}>Add to cart</button>
</li>
)
}
35 changes: 35 additions & 0 deletions src/StoreList.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { SetStateAction } from 'react'
import { CartItemType } from './App'
import { StoreItemType } from './store-items'
import StoreItem from './StoreItem'

export type StoreProps = {
cartItems: CartItemType[]
setCartItems: React.Dispatch<SetStateAction<CartItemType[]>>
}

type StoreListProps = {
storeItems: StoreItemType[]
} & StoreProps

export default function StoreList({
storeItems,
cartItems,
setCartItems,
}: StoreListProps) {
return (
<header id='store'>
<h1>Greengrocers</h1>
<ul className='item-list store--item-list'>
{storeItems.map((storeItem) => (
<StoreItem
key={storeItem.id}
storeItem={storeItem}
cartItems={cartItems}
setCartItems={setCartItems}
/>
))}
</ul>
</header>
)
}
9 changes: 0 additions & 9 deletions src/main.jsx

This file was deleted.

9 changes: 9 additions & 0 deletions src/main.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import React from 'react'
import ReactDOM from 'react-dom/client'
import App from './App.jsx'

ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
<React.StrictMode>
<App />
</React.StrictMode>
)
Loading