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

Create custom image component #64

Merged
merged 6 commits into from
Jan 25, 2021
Merged
Show file tree
Hide file tree
Changes from 3 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
10 changes: 9 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const workBoxOptions = {
}
};

const backend_hostname = new URL(process.env.NEXT_PUBLIC_WOO_SITE_URL).hostname;

module.exports = withOffline(
withCss(
withSass({
Expand All @@ -21,7 +23,13 @@ module.exports = withOffline(
generateSw: false,
globPatterns: ['static/**/*'],
globDirectory: '.',
target: 'serverless'
target: 'serverless',
images: {
domains: [
backend_hostname,
'https://via.placeholder.com'
],
},
})
)
);
6 changes: 3 additions & 3 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Link from 'next/link';
import client from '../src/apollo/ApolloClient';
import AddToCartButton from '../src/components/cart/AddToCartButton';
import Hero from '../src/components/home/Hero';
import Image from '../src/components/Image';
import { PRODUCTS_QUERY } from '../src/queries';

const NewProducts = ({ products }) => {
Expand All @@ -20,10 +21,9 @@ const NewProducts = ({ products }) => {
<Link href={`/product/${item.slug}`}>
<a>
<span className="product-link">
<img
className="product-image"
<Image
// className="product-image"
src={item.image.sourceUrl}
srcSet={item.image.srcSet}
alt={item.name}
/>
<h5 className="product-name">{item.name}</h5>
Expand Down
5 changes: 2 additions & 3 deletions pages/product/[slug].js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Layout from '../../src/components/layouts/Layout';
import AddToCartButton from '../../src/components/cart/AddToCartButton';
import client from '../../src/apollo/ApolloClient';
import Image from '../../src/components/Image';
import {
PRODUCT_QUERY,
PRODUCT_SLUGS
Expand All @@ -16,10 +17,8 @@ const Product = (props) => {
<div className="mx-auto mt-5">
<div className="row">
<div className="col-md-6">
<img
className="product-image"
<Image
src={product?.image?.sourceUrl}
srcSet={product?.image?.srcSet}
alt={product?.name}
/>
</div>
Expand Down
3 changes: 2 additions & 1 deletion src/apollo/ApolloClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const link = createHttpLink({
const client = new ApolloClient({
link,
cache,
defaultOptions
defaultOptions,
connectToDevTools: true
});

export default client;
41 changes: 41 additions & 0 deletions src/components/Image.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { useState } from 'react';
import Img from 'next/image';

const Image = (props) => {

const [error, setError] = useState(false);

const {
src,
width = 240,
imranhsayed marked this conversation as resolved.
Show resolved Hide resolved
height = 240,
alt = '',
...otherProps
} = props;

// URL to use if the actual image fails to load.
const fallBackUrl = `https://via.placeholder.com/${width}x${height}?text=${alt}`;
VijayaRaghavanM marked this conversation as resolved.
Show resolved Hide resolved

/**
* Handles any error when loading the image.
*
* @return {void}
*/
const errorHandler = () => {
setError(true);
}

return (
<>
imranhsayed marked this conversation as resolved.
Show resolved Hide resolved
<Img
src={error ? fallBackUrl : src}
imranhsayed marked this conversation as resolved.
Show resolved Hide resolved
width={width}
height={height}
onError={errorHandler}
alt={alt}
{...otherProps}/>
</>
);
};

export default Image;
8 changes: 4 additions & 4 deletions src/components/cart/cart-page/CartItem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { useState } from 'react';
import { updateCart } from '../../../utils/cart-functions';

import Image from '../../Image';
const CartItem = ({ item, handleRemoveProductClick, setCart }) => {
const [productCount, setProductCount] = useState(item.qty);

Expand Down Expand Up @@ -40,10 +40,10 @@ const CartItem = ({ item, handleRemoveProductClick, setCart }) => {
</span>
</th>
<td className="wd-cart-element">
<img
width="64"
<Image
width={64}
height={64}
src={item.image.sourceUrl}
imranhsayed marked this conversation as resolved.
Show resolved Hide resolved
srcSet={item.image.srcSet}
alt={item.image.title}
imranhsayed marked this conversation as resolved.
Show resolved Hide resolved
/>
</td>
Expand Down
6 changes: 3 additions & 3 deletions src/components/checkout/CheckoutCartItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const CheckoutCartItem = ({ item }) => {
return (
<tr className="wd-cart-item" key={item.databaseId}>
<td className="wd-cart-element">
<img
width="64"
<Image
width={64}
height={64}
src={item.image.sourceUrl}
srcSet={item.image.srcSet}
alt={item.image.title}
/>
</td>
Expand Down
21 changes: 9 additions & 12 deletions src/components/home/Categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ const Categories = () => {
<li className="product-category product first col-md-4">
<Link as={`/`} href={`/`}>
<a className="">
<img
<Image
src="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/accessories.jpg"
alt="Accessories"
width="324"
height="324"
srcSet="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/accessories.jpg 801w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/accessories-150x150.jpg 150w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/accessories-300x300.jpg 300w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/accessories-768x768.jpg 768w"
width={324}
height={324}
sizes="(max-width: 324px) 100vw, 324px"
/>
<h2 className="woocommerce-loop-category__title">
Expand All @@ -26,12 +25,11 @@ const Categories = () => {
<li className="product-category product col-md-4">
<Link as={`/`} href={`/`}>
<a className="">
<img
<Image
src="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/hoodies.jpg"
alt="Hoodies"
width="324"
height="324"
srcSet="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/hoodies.jpg 800w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/hoodies-150x150.jpg 150w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/hoodies-300x300.jpg 300w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/hoodies-768x768.jpg 768w"
width={324}
height={324}
sizes="(max-width: 324px) 100vw, 324px"
/>
<h2 className="woocommerce-loop-category__title">
Expand All @@ -43,12 +41,11 @@ const Categories = () => {
<li className="product-category product last col-md-4">
<Link as={`/`} href={`/`}>
<a className="">
<img
<Image
src="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/tshirts.jpg"
alt="Tshirts"
width="324"
height="324"
srcSet="https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/tshirts.jpg 801w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/tshirts-150x150.jpg 150w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/tshirts-300x300.jpg 300w, https://woo-vsf.dev5.rt.gw/wp-content/uploads/2019/05/tshirts-768x768.jpg 768w"
width={324}
height={324}
sizes="(max-width: 324px) 100vw, 324px"
/>
<h2 className="woocommerce-loop-category__title">
Expand Down
4 changes: 1 addition & 3 deletions src/styles/sass/products.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@
color: #333333;
}

.product-image {
max-width: 240px;
}

.product-name {
margin: 16px;
}
Expand Down