Skip to content

Commit

Permalink
Merge pull request RafaelGoulartB#43 from RafaelGoulartB/product-page
Browse files Browse the repository at this point in the history
Product page
  • Loading branch information
RafaelGoulartB authored Sep 9, 2020
2 parents 93e2a4f + 715f463 commit fde373f
Show file tree
Hide file tree
Showing 3 changed files with 205 additions and 20 deletions.
23 changes: 7 additions & 16 deletions components/productItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,17 @@ import {
FaHeart,
} from 'react-icons/fa';
import StarRatings from 'react-star-ratings';
import { wishlistProductsVar, cartProductsVar } from '../apollo/client/cache';
import { toggleCart, toggleWishlist } from '../utils/toggleProductStates';
import { CART, WISHLIST } from '../apollo/client/queries';

export default function ProductSection({ id, name, rating, img_url, price }) {
const cart = useQuery(CART);
const wishlist = useQuery(WISHLIST);

function handleAddToWishlist() {
if (wishlistProductsVar().includes(id)) {
const newWishlist = wishlistProductsVar().filter((item) => item != id);
wishlistProductsVar(newWishlist);
} else wishlistProductsVar([...wishlistProductsVar(), id]);
}

function handleAddToCart() {
if (cartProductsVar().includes(id)) {
const newCartList = cartProductsVar().filter((item) => item != id);
cartProductsVar(newCartList);
} else cartProductsVar([...cartProductsVar(), id]);
}
return (
<article>
<div className="top-buttons">
<button className="add-wishlist" onClick={handleAddToWishlist}>
<button className="add-wishlist" onClick={() => toggleWishlist(id)}>
{wishlist.data.wishlist.products.includes(id) && (
<FaHeart size={20} color="#D8D8D8" />
)}
Expand Down Expand Up @@ -61,7 +48,7 @@ export default function ProductSection({ id, name, rating, img_url, price }) {

<div className="price">
<p className="price-value">${price}</p>
<button className="add-cart" onClick={handleAddToCart}>
<button className="add-cart" onClick={() => toggleCart(id)}>
{cart.data.cart.products.includes(id) && (
<FaCartArrowDown size={18} color="#D8D8D8" />
)}
Expand Down Expand Up @@ -109,6 +96,10 @@ export default function ProductSection({ id, name, rating, img_url, price }) {
color: #666666;
margin-bottom: 18px;
}
.product-name:hover {
text-decoration: underline;
font-weight: 600;
}
.rating {
margin-bottom: 24px;
}
Expand Down
187 changes: 183 additions & 4 deletions pages/product/[id].js
Original file line number Diff line number Diff line change
@@ -1,12 +1,191 @@
import { useRouter } from 'next/router';
import { useQuery } from '@apollo/client';
import {
FaCartArrowDown,
FaCartPlus,
FaRegHeart,
FaHeart,
} from 'react-icons/fa';
import StarRatings from 'react-star-ratings';
import { PRODUCTS_BY_IDS, CART, WISHLIST } from '../../apollo/client/queries';
import Page from '../../components/page';
import ErrorAlert from '../../components/alerts/error';
import { toggleCart, toggleWishlist } from '../../utils/toggleProductStates';

export default function Home() {
const router = useRouter();
const { id } = router.query;
const cart = useQuery(CART);
const wishlist = useQuery(WISHLIST);

const { data, loading, error } = useQuery(PRODUCTS_BY_IDS, {
variables: {
id,
},
});

if (error) {
return (
<Page title="Quantum E-commerce - Products">
<ErrorAlert message="This product is not found!"></ErrorAlert>
</Page>
);
} else if (loading) {
return (
<Page title="Quantum E-commerce - Products">
<p>Loading...</p>
</Page>
);
}

return (
<Page title="Quantum E-commerce - Products">
<div>
<h1>Home with Layout</h1>
<style jsx>{''}</style>
</div>
<article>
<div className="top-buttons">
<button
className="add-wishlist"
onClick={() => toggleWishlist(data.productsById[0].id)}
>
{wishlist.data.wishlist.products.includes(
data.productsById[0].id
) && <FaHeart size={20} color="#D8D8D8" />}
{!wishlist.data.wishlist.products.includes(
data.productsById[0].id
) && <FaRegHeart size={20} color="#D8D8D8" />}
</button>
</div>

<img className="product-img" src={data.productsById[0].img_url} />

<h1 className="product-name">{data.productsById[0].name}</h1>

<h3 className="product-description">
{data.productsById[0].description}
</h3>

<div className="rating">
<StarRatings
rating={parseFloat(data.productsById[0].rating)}
starRatedColor="#F9AD3D"
numberOfStars={5}
name="rating"
starDimension="20px"
starSpacing="1px"
/>
</div>

<div className="price">
<p className="price-value">${data.productsById[0].price}</p>
<button
className="add-cart"
onClick={() => toggleCart(data.productsById[0].id)}
>
{cart.data.cart.products.includes(data.productsById[0].id) && (
<FaCartArrowDown size={24} color="#D8D8D8" />
)}
{!cart.data.cart.products.includes(data.productsById[0].id) && (
<FaCartPlus size={24} color="#D8D8D8" />
)}
</button>
</div>

<style jsx>{`
article {
display: flex;
align-items: center;
flex-direction: column;
box-sizing: border-box;
height: 100%;
width: 100%;
padding: 24px;
background: white;
box-shadow: 0px 2px 5px rgba(0, 0, 0, 0.05);
border-radius: 6px;
}
.top-buttons {
margin-bottom: 24px;
align-self: flex-end;
}
.top-buttons .add-wishlist {
background: none;
border: none;
}
.top-buttons .add-wishlist:focus {
outline: none;
}
.product-img {
width: 320px;
height: 230px;
margin-bottom: 28px;
}
.product-name {
width: 80%;
line-height: 20px;
text-decoration: none;
font-weight: 500;
font-size: 22px;
text-align: center;
color: #666666;
margin-bottom: 28px;
}
.product-description {
width: 40%;
line-height: 22px;
text-decoration: none;
font-weight: 400;
font-size: 14px;
text-align: center;
color: #666666;
margin-bottom: 24px;
}
.rating {
margin-bottom: 18px;
}
.price {
display: flex;
align-items: center;
font-weight: 900;
font-size: 20px;
color: #666666;
margin-bottom: 20px;
}
.price .add-cart {
background: none;
border: none;
margin-left: 5px;
}
.price .add-cart:focus {
outline: none;
}
@media (max-width: 1000px) {
.product-img {
width: 225px;
height: 180px;
margin-bottom: 28px;
}
.product-name {
width: 80%;
line-height: 20px;
text-decoration: none;
font-weight: 500;
font-size: 18px;
text-align: center;
color: #666666;
margin-bottom: 18px;
}
.product-description {
width: 80%;
line-height: 22px;
text-decoration: none;
font-weight: 400;
font-size: 14px;
text-align: center;
color: #666666;
margin-bottom: 18px;
}
}
`}</style>
</article>
</Page>
);
}
15 changes: 15 additions & 0 deletions utils/toggleProductStates.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { wishlistProductsVar, cartProductsVar } from '../apollo/client/cache';

export function toggleWishlist(id) {
if (wishlistProductsVar().includes(id)) {
const newWishlist = wishlistProductsVar().filter((item) => item != id);
wishlistProductsVar(newWishlist);
} else wishlistProductsVar([...wishlistProductsVar(), id]);
}

export function toggleCart(id) {
if (cartProductsVar().includes(id)) {
const newCartList = cartProductsVar().filter((item) => item != id);
cartProductsVar(newCartList);
} else cartProductsVar([...cartProductsVar(), id]);
}

0 comments on commit fde373f

Please sign in to comment.