-
Notifications
You must be signed in to change notification settings - Fork 5
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
Code to add checkout functionality. #9
base: pokemon-cart
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,247 @@ | ||
export function Checkout() { | ||
return <div>Checkout</div>; | ||
import { useState } from "react"; | ||
import { useHistory } from "react-router-dom"; | ||
import styled from "styled-components"; | ||
import { addOrderDetails } from "../common/pokemonStorage"; | ||
|
||
const SubmitButton = styled.button` | ||
grid-column-start: first; | ||
grid-column-end: second; | ||
grid-row-start: r6; | ||
grid-row-end: r6; | ||
font-family: inherit; | ||
font-size: 100%; | ||
line-height: 1.15; | ||
font-weight: 700; | ||
margin: 14px 14px 14px 14px; | ||
background-color: black; | ||
color: white; | ||
outline: none; | ||
display: inline-flex; | ||
justify-content: center; | ||
border: 0; | ||
cursor: pointer; | ||
padding: 24px 0; | ||
|
||
align-self: end; | ||
&:hover { | ||
opacity: 0.8; | ||
} | ||
`; | ||
const LayoutStyle = styled.div` | ||
display: grid; | ||
grid-template-columns: [first] 50% [second] 50%; | ||
grid-template-rows: [r1] 65px [r2] 65px [r3] 65px [r4] 65px [r5] 65px [r6] 80px; | ||
max-width: 800px; | ||
margin: auto; | ||
`; | ||
|
||
const PanelStyle1 = styled.div` | ||
grid-column-start: first; | ||
grid-column-end: second; | ||
grid-row-start: r1; | ||
grid-row-end: r1; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const PanelStyle2 = styled.div` | ||
grid-column-start: first; | ||
grid-column-end: first; | ||
grid-row-start: r2; | ||
grid-row-end: r2; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const PanelStyle3 = styled.div` | ||
grid-column-start: second; | ||
grid-column-end: second; | ||
grid-row-start: r2; | ||
grid-row-end: r2; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const PanelStyle4 = styled.div` | ||
grid-column-start: first; | ||
grid-column-end: second; | ||
grid-row-start: r3; | ||
grid-row-end: r3; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const PanelStyle5 = styled.div` | ||
grid-column-start: first; | ||
grid-column-end: second; | ||
grid-row-start: r4; | ||
grid-row-end: r4; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const PanelStyle6 = styled.div` | ||
grid-column-start: first; | ||
grid-column-end: second; | ||
grid-row-start: r5; | ||
grid-row-end: r5; | ||
margin-right: 12px; | ||
margin-left: 12px; | ||
margin-top: 0; | ||
margin-bottom: 0; | ||
background-color: white; | ||
`; | ||
|
||
const Input = styled.input` | ||
width: 100%; | ||
font-family: "Source Sans Pro", sans-serif; | ||
padding: 8px 8px; | ||
margin: 5px auto 5px auto; | ||
display: block; | ||
text-align: center; | ||
font-size: 18px; | ||
color: black; | ||
font-weight: 300; | ||
`; | ||
|
||
export function CheckOut() { | ||
const [contact, setContact] = useState({ | ||
fName: "", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. suggestion: name property as it is - meaning write it in full words |
||
lName: "", | ||
email: "", | ||
address: "", | ||
creditcard: "", | ||
}); | ||
const history = useHistory(); | ||
const handleSubmit = () => { | ||
addOrderDetails({ | ||
fName: contact.fName, | ||
lName: contact.lName, | ||
email: contact.email, | ||
address: contact.address, | ||
creditcard: contact.creditcard, | ||
}); | ||
history.push(`/order-completed`); | ||
}; | ||
|
||
function handleChange(event) { | ||
const { name, value } = event.target; | ||
|
||
setContact((prevValue) => { | ||
if (name === "fName") { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All that could be written shorter: setContact((prevState) => {
...prevState,
[name]: value,
}); Let me know if you don't understand this structure so I can explain what it does :) |
||
return { | ||
fName: value, | ||
lName: prevValue.lName, | ||
email: prevValue.email, | ||
address: prevValue.address, | ||
creditcard: prevValue.creditcard, | ||
}; | ||
} | ||
if (name === "lName") { | ||
return { | ||
fName: prevValue.fName, | ||
lName: value, | ||
email: prevValue.email, | ||
address: prevValue.address, | ||
creditcard: prevValue.creditcard, | ||
}; | ||
} | ||
if (name === "email") { | ||
return { | ||
fName: prevValue.fName, | ||
lName: prevValue.lName, | ||
email: value, | ||
address: prevValue.address, | ||
creditcard: prevValue.creditcard, | ||
}; | ||
} | ||
if (name === "address") { | ||
return { | ||
fName: prevValue.fName, | ||
lName: prevValue.lName, | ||
email: prevValue.email, | ||
address: value, | ||
creditcard: prevValue.creditcard, | ||
}; | ||
} | ||
if (name === "creditcard") { | ||
return { | ||
fName: prevValue.fName, | ||
lName: prevValue.lName, | ||
email: prevValue.email, | ||
address: prevValue.address, | ||
creditcard: value, | ||
}; | ||
} | ||
return ""; | ||
}); | ||
} | ||
|
||
return ( | ||
<LayoutStyle> | ||
<PanelStyle1> | ||
<h1> | ||
Hello {contact.fName} {contact.lName} | ||
</h1> | ||
<p>{contact.email}</p> | ||
</PanelStyle1> | ||
<PanelStyle2> | ||
<Input | ||
onChange={handleChange} | ||
value={contact.fName} | ||
name="fName" | ||
placeholder="First Name" | ||
/> | ||
</PanelStyle2> | ||
<PanelStyle3> | ||
<Input | ||
onChange={handleChange} | ||
value={contact.lName} | ||
name="lName" | ||
placeholder="Last Name" | ||
/> | ||
</PanelStyle3> | ||
<PanelStyle4> | ||
<Input | ||
onChange={handleChange} | ||
value={contact.email} | ||
name="email" | ||
placeholder="Email" | ||
/> | ||
</PanelStyle4> | ||
<PanelStyle5> | ||
<Input | ||
onChange={handleChange} | ||
value={contact.creditcard} | ||
name="creditcard" | ||
placeholder="Credit Card" | ||
/> | ||
</PanelStyle5> | ||
<PanelStyle6> | ||
<Input | ||
onChange={handleChange} | ||
value={contact.address} | ||
name="address" | ||
placeholder="Address" | ||
/> | ||
</PanelStyle6> | ||
|
||
<SubmitButton onClick={handleSubmit}>Submit</SubmitButton> | ||
</LayoutStyle> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,60 @@ | ||
import styled from "styled-components"; | ||
import { GRAY } from "./constants"; | ||
import { Card } from "./components/Card"; | ||
import { Total } from "./components/Total"; | ||
import { | ||
getCartItems, | ||
getOrderDetails, | ||
getCounter, | ||
} from "../common/pokemonStorage"; | ||
|
||
const LayoutStyle = styled.div` | ||
display: grid; | ||
grid-template-columns: 2fr 1fr; | ||
max-width: 1024px; | ||
background-color: ${GRAY}; | ||
margin: auto; | ||
`; | ||
|
||
const PanelStyle = styled.div` | ||
margin: 1rem; | ||
background-color: white; | ||
padding: 1rem; | ||
`; | ||
|
||
const ScrollStyle = styled.div` | ||
overflow-y: scroll; | ||
max-height: 500px; | ||
`; | ||
|
||
export function OrderCompleted() { | ||
return <div>OrderCompleted</div>; | ||
const cart = getCartItems(); | ||
const order = getOrderDetails(); | ||
console.log("order", order); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no need for console.log |
||
const cartCounter = getCounter(); | ||
const total = cart.reduce( | ||
(acc, current) => acc + current.price * current.quantity, | ||
0 | ||
); | ||
|
||
return ( | ||
<div className="container"> | ||
<h1> | ||
{order.fName} {order.lName} | ||
</h1> | ||
<LayoutStyle> | ||
<PanelStyle> | ||
<h2>Order Details: (Total {cartCounter} articles)</h2> | ||
<ScrollStyle> | ||
{cart.map((item) => ( | ||
<Card key={item.name} {...item} /> | ||
))} | ||
</ScrollStyle> | ||
</PanelStyle> | ||
<PanelStyle> | ||
<Total total={total} /> | ||
</PanelStyle> | ||
</LayoutStyle> | ||
</div> | ||
); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import styled from "styled-components"; | ||
import { GRAY } from "../constants"; | ||
import { HRStyle } from "./Hr"; | ||
|
||
const CardStyle = styled.div` | ||
display: grid; | ||
grid-template-columns: 1fr 3fr 1fr; | ||
img { | ||
width: 100px; | ||
background-color: ${GRAY}; | ||
} | ||
div { | ||
padding: 1rem; | ||
} | ||
`; | ||
|
||
const CardPriceStyle = styled.div` | ||
padding: 1rem; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: flex-end; | ||
`; | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. too many empty lines |
||
|
||
|
||
export const Card = ({ name, img, price, quantity }) => ( | ||
<> | ||
<CardStyle> | ||
<img src={img} alt={name} /> | ||
<div> | ||
<strong>{name}</strong> | ||
</div> | ||
<CardPriceStyle> | ||
<p>{quantity}</p> | ||
<p>{price * quantity}</p> | ||
</CardPriceStyle> | ||
</CardStyle> | ||
<HRStyle /> | ||
</> | ||
); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think since it's too many styled components needed it make sense to move them into a separate file. Nothing wrong with this implementation just nice code splitting :)