Skip to content

Commit

Permalink
Improvements
Browse files Browse the repository at this point in the history
- Image lazy loading
- Loading properly added on category pages
- Scroll position reset to top on "page" change
- Smooth CSS animation on "page" change
  • Loading branch information
Felipe Spengler committed Apr 6, 2024
1 parent d8a0924 commit b3cb774
Show file tree
Hide file tree
Showing 9 changed files with 55 additions and 42 deletions.
10 changes: 7 additions & 3 deletions src/App.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useEffect, lazy, Suspense } from 'react';
import { Routes, Route } from "react-router-dom";
import { useEffect, lazy, Suspense, useLayoutEffect } from 'react';
import { Routes, Route, useLocation } from "react-router-dom";
import { useDispatch } from 'react-redux';

import { authChangedListener, createUserDocFromAuth } from './utils/firebase/firebase.utils.js';
Expand All @@ -13,7 +13,7 @@ const Shop = lazy(() => import('./routes/shop/shop.component'));
const CheckoutComponent = lazy(() => import('./routes/checkout/checkout.component'));

const App = () => {

const location = useLocation();
const dispatch = useDispatch();

useEffect(() => {
Expand All @@ -27,6 +27,10 @@ const App = () => {
return unsubscribe;
}, []); // eslint-disable-line react-hooks/exhaustive-deps

useLayoutEffect(() => {
window.scrollTo({ top:0, left:0, behavior: "instant" });
}, [location.pathname]);

return (
<Suspense fallback={<SpinnerComponent />}>

Expand Down
2 changes: 1 addition & 1 deletion src/components/cart/cart-item.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const CartItem = memo(({ cartItem }) => {
const { name, imageUrl, price, quantity } = cartItem;
return (
<div className='minicart-item'>
<img src={imageUrl} alt={`${name} image`} />
<img src={imageUrl} alt={`${name} image`} loading='lazy' />
<div className='item-details'>
<h3 className='name'>{name}</h3>
<span className='info'>
Expand Down
2 changes: 1 addition & 1 deletion src/components/cart/minicart.styles.scss
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
width: 280px;
height: auto;
padding: 20px;
border: 1px solid black;
border: 1px solid #ececec;
background-color: white;
top: 60px;
right: 22px;
Expand Down
7 changes: 4 additions & 3 deletions src/components/product-card/product-card.component.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { memo } from 'react';
import { useDispatch } from 'react-redux';
import { addItemToCart, toggleMinicart } from '../../store/minicart.reducer';

import Button from '../button/button.component';
import './product-card.styles.scss';

const ProductCard = ({ product }) => {
const ProductCard = memo(({ product }) => {
const { name, imageUrl, price } = product;
const dispatch = useDispatch();

Expand All @@ -16,7 +17,7 @@ const ProductCard = ({ product }) => {
return (
<div className='product-card-container'>
<div className='img-container'>
<img src={imageUrl} alt={name} />
<img src={imageUrl} alt={name} loading='lazy' />
<Button type='button' style='' label='Add to cart' onClick={addProductToCart} />
</div>
<div className='product-footer'>
Expand All @@ -25,6 +26,6 @@ const ProductCard = ({ product }) => {
</div>
</div>
);
}
})

export default ProductCard;
2 changes: 1 addition & 1 deletion src/components/spinner/spinner.styles.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.spinner-overlay {
height: 60vh;
height: 100vh;
width: 100%;
display: flex;
justify-content: center;
Expand Down
11 changes: 11 additions & 0 deletions src/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ body {
margin-right: auto;
padding: 0 20px 25px;
width: 100%;
opacity: 0;
animation: 0.40s ease-out forwards fade-in;

@media only screen and (min-width: 768px ) {
padding: 0 20px 50px;
Expand All @@ -43,6 +45,15 @@ body {
}
}

@keyframes fade-in {
from {
opacity: 0;
}
to {
opacity: 100;
}
}

a {
text-decoration: none;
color: #000;
Expand Down
47 changes: 25 additions & 22 deletions src/routes/category-list-page/category-list-page.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@ const CategoryListPage = () => {
const isLoading = useSelector(selectCategoriesIsLoading);

return (
<div className='container page-container'>
{isLoading ? (
<SpinnerComponent />
<>
{
isLoading ? (
<SpinnerComponent />
) : (
Object.keys(categoriesMap).map(title => {
return (
<div className='cat-wrapper' key={title}>
<h2 className='cat-name'>
{title}
<Link className='see-link' to={title}>
See all {categoriesMap[title].length} products »
</Link>
</h2>
<div className='shop-container'>
{categoriesMap[title].slice(0, 4).map((product) => {
return (
<ProductCard key={product.id} product={product} />
)
})}
<div className='container page-container'>
{ Object.keys(categoriesMap).map(title => {
return (
<div className='cat-wrapper' key={title}>
<h2 className='cat-name'>
{title}
<Link className='see-link' to={title}>
See all {categoriesMap[title].length} products »
</Link>
</h2>
<div className='shop-container'>
{categoriesMap[title].slice(0, 4).map((product) => {
return (
<ProductCard key={product.id} product={product} />
)
})}
</div>
</div>
</div>
)
})
)
}) }
</div>
)
}
</div>
</>
);
}

Expand Down
8 changes: 4 additions & 4 deletions src/routes/category/category.component.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ const Category = () => {
}, [category, categoriesMap]);

return (
<div className='container page-container'>
<>
{
isLoading ? (
<SpinnerComponent />
) : (
<>
<div className='container page-container'>
<h2 className='cat-page-name'>
{category}
<span className='product-count'>
Expand All @@ -36,10 +36,10 @@ const Category = () => {
)
})}
</div>
</>
</div>
)
}
</div>
</>
);
}

Expand Down
8 changes: 1 addition & 7 deletions src/routes/checkout/checkout.component.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { useLayoutEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { Link, useLocation } from 'react-router-dom';
import { Link } from 'react-router-dom';

import { addItemToCart, removeOrDecreaseItem } from '../../store/minicart.reducer';
import { selectBagTotalPrice, selectCartItems } from '../../store/minicart.selector';
Expand All @@ -11,15 +10,10 @@ const CheckoutComponent = () => {
const dispatch = useDispatch();
const cartItems = useSelector(selectCartItems);
const bagTotalPrice = useSelector(selectBagTotalPrice);
const location = useLocation();

const handleAddToCart = (product) => dispatch(addItemToCart(product));
const handleRemoveOrDecrease = (product, directRemove = false) => dispatch(removeOrDecreaseItem({ product, directRemove }));

useLayoutEffect(() => {
window.scrollTo({ top:0, left:0, behavior: "instant" });
}, [location.pathname]);

return (
<div className={`container checkout-container ${!cartItems.length && 'empty-container'}`}>
<h1>Checkout</h1>
Expand Down

0 comments on commit b3cb774

Please sign in to comment.