-
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?
Code to add checkout functionality. #9
Conversation
Added the ability to update the cart items quantity. Linked the "Checkout" button to the checkout page. Created a form in the Checkout page. The "Place order" button has been linked to the Order completed page. Order complete page shows user info and order info. Cart clear implemented on Order complete.
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.
Nice work! I checked the code but not how it works, so hope that you checked that it's working.
Left some comment and improvement sugessions, please take a look.
import styled from "styled-components"; | ||
import { addOrderDetails } from "../common/pokemonStorage"; | ||
|
||
const SubmitButton = styled.button` |
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 :)
const { name, value } = event.target; | ||
|
||
setContact((prevValue) => { | ||
if (name === "fName") { |
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.
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 :)
|
||
export function CheckOut() { | ||
const [contact, setContact] = useState({ | ||
fName: "", |
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.
suggestion: name property as it is - meaning write it in full words firstName
, lastName
. Because you're not saving any space here but readability would be way better.
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 comment
The reason will be displayed to describe this comment to others. Learn more.
no need for console.log
import { useHistory } from "react-router-dom"; | ||
import styled from "styled-components"; | ||
import { HRStyle } from "./Hr"; | ||
import {} from "module"; |
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.
this is probably some leftover code
try { | ||
const counterStorage = localStorage.getItem(COUNTER_KEY); | ||
if (counterStorage) { | ||
/* eslint-disable */ |
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.
don't use this generic disabling syntax - because it's not telling to others what exactly you're trying to fix with it. In case you need to do it - disable exact rule which is failing
flex-direction: column; | ||
align-items: flex-end; | ||
`; | ||
|
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.
too many empty lines
|
||
export const getCartItems = () => { | ||
try { | ||
const cartStorage = localStorage.getItem(STORAGE_KEY); | ||
return JSON.parse(cartStorage); | ||
return cartStorage ? JSON.parse(cartStorage) : []; |
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.
you can do it shorter: JSON.parse(cartStorage) || []
.
} catch (e) { | ||
return []; | ||
} | ||
}; | ||
|
||
export const addToCart = (item) => { | ||
const cartStorage = getCartItems(); | ||
const currentCounter = getCounter(); | ||
const foundIndex = cartStorage.findIndex((itm) => itm.name === item.name); |
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.
same here about names: itm
is not really a good name. In such case I'll go with just i
as a short from item, or with full cartItem
which probably better because you see instantly that you're trying to find same nae in cart
localStorage.setItem(STORAGE_KEY, JSON.stringify(filteredCart)); | ||
return filteredCart; | ||
const currentCounter = getCounter(); | ||
console.log("currentCounter", currentCounter); |
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.
console.log
Added the ability to update the cart items quantity. Linked the "Checkout" button to the checkout page. Created a form in the Checkout page. The "Place order" button has been linked to the Order completed page. Order complete page shows user info and order info. Cart clear implemented on Order complete.