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

Checkout #8

Open
wants to merge 3 commits into
base: master
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
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"prettier"
],
"rules": {
"react/jsx-props-no-spreading": 0,
"semi": 0,
"jsx-a11y/click-events-have-key-events": 0,
"jsx-a11y/no-static-element-interactions": 0,
Expand All @@ -25,4 +26,4 @@
}
]
}
}
}
4 changes: 1 addition & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ const App = () => {
setIsLoading(false);
})
.catch((e) => {
// eslint-disable-next-line
console.log(e);
setIsLoading(false);
});
Expand All @@ -79,9 +80,6 @@ const App = () => {
<li>
<Link to="/">Home</Link>
</li>
<li>
<Link to="/shopping-cart">ShoppingCart</Link>
</li>
<li>
<Link to="/order-completed">Order Completed</Link>
</li>
Expand Down
6 changes: 5 additions & 1 deletion src/Checkout/Checkout.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import Form from "./components/Form";

export function Checkout() {
return <div>Checkout</div>;
return <div>
<Form />
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

something wrong with indentation. And check it in all the files and code, because there are some places below when it's broken.

</div>;
}
80 changes: 80 additions & 0 deletions src/Checkout/components/Form.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import React, {useState} from 'react'
import { useHistory } from "react-router-dom";
import styled from 'styled-components'
import {setCustomerDetail} from "../../common/customerInfo"

const FormWrapper = styled.div`
margin-bottom: 2rem;
max-width: 1000px;
margin: auto;
background-color: black;
color: white;
justify-content: centre;
align-item: center;
`;
const PanelStyle = styled.div`
margin: 2rem;
background-color: white;
padding: 2rem;
display: block;
`;
const FormLayout = styled.form`
display: block;
margin: 2px;
`;
const Label = styled.label`
font-weight: 20px;
color: black;
display: block;
`;
const Input = styled.input`
width: 100px;
height: 15px;
`;
const Address = styled.textarea`
width: 100px;
height: 15px
`;
const SubmitButton = styled.button`
width : 60px;
height: 30px;
display: block;
margin: 5px;
`;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not needed empty line


function Form() {
const [customer, setCustomer] = useState();
const history = useHistory();
const handleChange = (e)=>{
const name=e.target.value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pay attention to the code style, this is super important when you're submitting your code for review.
Here you don't put spaces around = and =>.
Suggestion (one more time): install Prettier so it can help you

setCustomer(name);
setCustomerDetail(name);
}
const handleSubmit = () =>{
history.push(`/order-completed`);
}

return (
<FormWrapper>
<h1>CHECKOUT</h1>
<PanelStyle>
<FormLayout onSubmit={handleSubmit}>
<Label>NAME</Label>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need to use caps in the field names. Name them just with first capital letter, that would be enough

<Input type="text" maxLength="20" minLength="3" value={customer} onChange={(e)=>handleChange(e)}/>
<Label>EMAIL</Label>
<Input type="email" />
<Label>CITY</Label>
<Input type="text-area" />
<Label>ADDRESS</Label>
<Address />
<Label>CARD</Label>
<Input type="number"/>
<SubmitButton>SUBMIT</SubmitButton>
</FormLayout>
</PanelStyle>
</FormWrapper>
)
}
export default Form

4 changes: 2 additions & 2 deletions src/Details/components/MetaSection/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const SubTitle = styled.h3`
border-bottom: 1px solid grey;
`;

const MetaSection = ({ type, name, price, stats, abilities }) => (
const MetaSection = ({ type, name, price, stats, abilities, onAddToCart }) => (
<Wrapper>
<Type>{titleCase(type)}</Type>
<Title>{titleCase(name)}</Title>
Expand All @@ -67,7 +67,7 @@ const MetaSection = ({ type, name, price, stats, abilities }) => (
{price} ¥ <PriceTax>VAT included</PriceTax>
</Price>

<BuyButton>Add to bag</BuyButton>
<BuyButton onClick={onAddToCart}>Add to bag</BuyButton>

<SubTitle>Stats</SubTitle>
<Stats data={stats} />
Expand Down
16 changes: 15 additions & 1 deletion src/Details/index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
/* eslint-disable react/prop-types */
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import { useParams, useHistory } from "react-router-dom";
import styled from "styled-components";
import { DetailsLayout } from "./components/DetailsLayout";
import { ImageSection } from "./components/ImageSection";
import { ThumbnailSection } from "./components/ThumbnailSection";
import { MetaSection } from "./components/MetaSection";
import { addToCart } from "../common/pokemonStorage";

const CrossSellingSection = styled.div`
grid-area: crossselling;
Expand All @@ -19,6 +20,7 @@ export function Details() {
const [data, setData] = useState(null);
const [selectedImage, setSelectedImage] = useState("");

const history = useHistory();
useEffect(() => {
fetch(`https://pokeapi.co/api/v2/pokemon/${id}`)
.then((res) => res.json())
Expand All @@ -31,6 +33,17 @@ export function Details() {
});
}, [id]);

const handleAdd = () => {
addToCart({
img: data.sprites.other["official-artwork"].front_default,
name: data.name,
type: data.types[0].type.name,
price: data.base_experience,
quantity: 1,
})
history.push(`/shopping-cart`);
};

if (!data) {
return <span>Loading</span>;
}
Expand Down Expand Up @@ -59,6 +72,7 @@ export function Details() {
price={data.base_experience}
stats={data.stats}
abilities={data.abilities}
onAddToCart={handleAdd}
/>

<CrossSellingSection>
Expand Down
67 changes: 66 additions & 1 deletion src/OrderCompleted/OrderCompleted.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,68 @@
import styled from 'styled-components'
import {getCustomerDetail} from '../common/customerInfo'
import {getCartItems} from '../common/pokemonStorage'

const Wrapper = styled.div`
margin: 5px;
width: 550px;
background-color: #ffffcc ;
`;
const Message = styled.h4`
padding: 10px;
display: flex;
`;
const ScrollStyle = styled.div`
overflow-y: scroll;
max-height: 500px;
width: 500px;
`;
const CardStyle = styled.div`
display: grid;
grid-template-columns: 1fr 3fr 1fr;
img {
width: 100px;
background-color: cream;
}
div {
padding: 1rem;
}
`;

const CardPriceStyle = styled.div`
padding: 1rem;
display: flex;
flex-direction: column;
align-items: flex-end;
`;
const Card = ({name, img, type, price, quantity}) =>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should not this Card component come from "./components/Card"; same as in ShoppingCart?

<CardStyle>
<img src={img} alt={name} />
<div>
<strong>{name}</strong>
<p>{type}</p>
</div>
<CardPriceStyle>
<p>{quantity}</p> <p>{price}</p>
</CardPriceStyle>
</CardStyle>
);
export function OrderCompleted() {
return <div>OrderCompleted</div>;
const customerName = getCustomerDetail();
const cart= getCartItems();
return (
<Wrapper>
<Message>Hello {customerName} your order was successfully placed, Thank You for Shopping with us!!</Message>
<br />
<Message>ORDERED LIST</Message>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here: don't use caps text in the UI, this is not a good practice.

<ScrollStyle>
{cart.map((item) => (
<Card
key={item.name}
{...item}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

better would be to not just spread whatever is inside item object, but explicitely pass props one by one. This will increase quality of the code (and you want need to turn off eslint rules)

/>
))}
<h5>TOTAL AMOUNT={cart.reduce((acc, current) => acc + current.price, 0)}</h5>
</ScrollStyle>
</Wrapper>
);
}
66 changes: 65 additions & 1 deletion src/ShoppingCart/ShoppingCart.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,67 @@
import styled from "styled-components";
import { useState } from "react";
import { useHistory } from "react-router-dom";
import { GRAY } from "./constants";
import { Card } from "./components/Card";
import { Total } from "./components/Total";
import { getCartItems, removeFromCart } 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 ShoppingCart() {
return <div>ShoppingCart</div>;
const [cart, setCart] = useState(getCartItems());
const history = useHistory();
const handleRemove = (name) => {
setCart(removeFromCart(name));
};

const handleCheckout = () =>{
history.push(`/checkout`);
}
if (cart.length === 0) {
return (
<LayoutStyle>
<PanelStyle>
<h2>Your cart is empty</h2>{" "}
</PanelStyle>
</LayoutStyle>
);
}

return (
<LayoutStyle>
<PanelStyle>
<h2>Place you order ({cart.length} article)</h2>
<ScrollStyle>
{cart.map((item) => (
<Card
key={item.name}
{...item}
onRemove={() => handleRemove(item.name)}
/>
))}
</ScrollStyle>
</PanelStyle>
<PanelStyle>
<Total total={cart.reduce((acc, current) => acc + current.price, 0)} handleCheckout= {handleCheckout} />
</PanelStyle>
</LayoutStyle>
);
}
48 changes: 48 additions & 0 deletions src/ShoppingCart/components/Card.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
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;
`;

const RemoveStyle = styled.a`
color: black;
cursor: pointer;
&:hover {
text-decoration: underline;
}
`;

export const Card = ({ name, img, type, price, quantity, onRemove }) => (
<>
<CardStyle>
<img src={img} alt={name} />
<div>
<strong>{name}</strong>
<p>{type}</p>
<RemoveStyle onClick={onRemove}>Remove</RemoveStyle>
</div>
<CardPriceStyle>
<p>{quantity}</p>
<p>{price}</p>
</CardPriceStyle>
</CardStyle>
<HRStyle />
</>
);
6 changes: 6 additions & 0 deletions src/ShoppingCart/components/Hr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import styled from "styled-components";
import { GRAY } from "../constants";

export const HRStyle = styled.hr`
border: 0.5px solid ${GRAY};
`;
Loading