Skip to content
This repository has been archived by the owner on Jul 14, 2022. It is now read-only.

Commit

Permalink
Merge pull request #165 from mirumee/151/empty_search_hide_filters
Browse files Browse the repository at this point in the history
[151] Hide filters, add home page collection upon empty search
  • Loading branch information
maarcingebala authored Dec 5, 2018
2 parents 7a0b2ab + 6a35eb8 commit afb118a
Show file tree
Hide file tree
Showing 14 changed files with 239 additions and 164 deletions.
43 changes: 13 additions & 30 deletions src/components/HomePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,22 @@ import * as React from "react";
import { Query } from "react-apollo";
import { Link } from "react-router-dom";

import { Button, Carousel, Loader, ProductListItem } from "..";
import {
Button,
Carousel,
Loader,
ProductListItem,
ProductsFeatured
} from "..";
import { ProductsList } from "../../core/types/saleor";
import { generateCategoryUrl, generateProductUrl } from "../../core/utils";
import { generateCategoryUrl, maybe } from "../../core/utils";
import { Error } from "../Error";
import { GET_PRODUCTS_AND_CATEGORIES } from "./queries";

import "./scss/index.scss";

const canDisplay = (data: ProductsList) =>
data &&
data.shop &&
data.shop.homepageCollection &&
data.categories &&
data.categories.edges;
maybe(() => !!data.shop.homepageCollection && !!data.categories.edges, false);

const HomePage: React.SFC = () => (
<div className="home-page">
Expand All @@ -26,17 +28,14 @@ const HomePage: React.SFC = () => (
>
{({ error, data, loading }) => {
if (canDisplay(data)) {
const { backgroundImg } = data.shop.homepageCollection;
return (
<>
<div
className="home-page__hero"
style={
data.shop.homepageCollection.backgroundImage
? {
backgroundImage: `url(${
data.shop.homepageCollection.backgroundImage.url
})`
}
backgroundImg
? { backgroundImage: `url(${backgroundImg.url})` }
: null
}
>
Expand Down Expand Up @@ -67,23 +66,7 @@ const HomePage: React.SFC = () => (
)}
</div>
</div>
<div className="home-page__featured">
<div className="container">
<h3>Featured</h3>
<Carousel>
{data.shop.homepageCollection.products.edges.map(
({ node: product }) => (
<Link
to={generateProductUrl(product.id, product.name)}
key={product.id}
>
<ProductListItem product={product} />
</Link>
)
)}
</Carousel>
</div>
</div>
<ProductsFeatured />
<div className="home-page__categories">
<div className="container">
<h3>Shop by category</h3>
Expand Down
19 changes: 0 additions & 19 deletions src/components/HomePage/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,6 @@ export const GET_PRODUCTS_AND_CATEGORIES = gql`
url
}
name
products(first: 20) {
edges {
node {
id
name
thumbnailUrl
thumbnailUrl2x: thumbnailUrl(size: 510)
category {
id
name
}
price {
currency
amount
localized
}
}
}
}
}
}
categories(level: 0, first: 4) {
Expand Down
16 changes: 0 additions & 16 deletions src/components/HomePage/scss/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,6 @@
}
}

&__featured {
padding: 2rem 0 4rem;

h3 {
text-transform: uppercase;
font-weight: $bold-font-weight;
margin-bottom: 2rem;
}

a {
display: inline-block;
text-decoration: none;
color: $base-font-color;
}
}

&__categories {
margin-bottom: 2rem;

Expand Down
4 changes: 0 additions & 4 deletions src/components/ProductFilters/scss/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,5 @@
@media (max-width: $small-screen) {
grid-template-columns: 1fr;
}

&__filter {
min-width: 15rem;
}
}
}
56 changes: 56 additions & 0 deletions src/components/ProductsFeatured/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import * as React from "react";
import { Query } from "react-apollo";
import { Link } from "react-router-dom";

import { Carousel, Loader, ProductListItem } from "..";
import { generateProductUrl, maybe } from "../../core/utils";
import { GET_FEATURED_PRODUCTS } from "./queries";

import "./scss/index.scss";

interface ProductsFeaturedProps {
title?: string;
}

const ProductsFeatured: React.SFC<ProductsFeaturedProps> = ({ title }) => {
return (
<Query query={GET_FEATURED_PRODUCTS}>
{({ error, data, loading }) => {
const products = maybe(
() => data.shop.homepageCollection.products.edges,
[]
);

if (products.length) {
return (
<div className="products-featured">
<div className="container">
<h3>{title}</h3>
<Carousel>
{products.map(({ node: product }) => (
<Link
to={generateProductUrl(product.id, product.name)}
key={product.id}
>
<ProductListItem product={product} />
</Link>
))}
</Carousel>
</div>
</div>
);
}
if (loading) {
return <Loader />;
}
return null;
}}
</Query>
);
};

ProductsFeatured.defaultProps = {
title: "Featured"
};

export default ProductsFeatured;
30 changes: 30 additions & 0 deletions src/components/ProductsFeatured/queries.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import gql from "graphql-tag";

export const GET_FEATURED_PRODUCTS = gql`
query ProductsList {
shop {
homepageCollection {
id
products(first: 20) {
edges {
node {
id
name
thumbnailUrl
thumbnailUrl2x: thumbnailUrl(size: 510)
category {
id
name
}
price {
currency
amount
localized
}
}
}
}
}
}
}
`;
17 changes: 17 additions & 0 deletions src/components/ProductsFeatured/scss/index.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
@import "../../App/scss/variables.scss";

.products-featured {
padding: 2rem 0 4rem;

h3 {
text-transform: uppercase;
font-weight: $bold-font-weight;
margin-bottom: $spacer * 2;
}

a {
display: inline-block;
text-decoration: none;
color: $base-font-color;
}
}
37 changes: 24 additions & 13 deletions src/components/ProductsList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface ProductsListProps {
onLoadMore: () => void;
products: CategoryProductInterface;
onOrder: (order: string) => void;
notFoundPhrase?: string;
}

export const ProductList: React.SFC<ProductsListProps> = ({
Expand All @@ -24,14 +25,19 @@ export const ProductList: React.SFC<ProductsListProps> = ({
hasNextPage,
onLoadMore,
products,
onOrder
onOrder,
notFoundPhrase
}) => {
const filterOptions = [
{ value: "price", label: "Price Low-High" },
{ value: "-price", label: "Price High-Low" },
{ value: "name", label: "Name Increasing" },
{ value: "-name", label: "Name Decreasing" }
];
const sortValues = filterOptions.find(
option => option.value === filters.sortBy
);
const hasProducts = !!products.totalCount;
return (
<div className="products-list">
<div className="products-list__products container">
Expand All @@ -45,19 +51,20 @@ export const ProductList: React.SFC<ProductsListProps> = ({
</div>
)}
<span className="products-list__products__subheader__sort">
<span>Sort by:</span>{" "}
<Dropdown
options={filterOptions}
value={
filterOptions.find(option => option.value === filters.sortBy) ||
""
}
isSearchable={false}
onChange={event => onOrder(event.value)}
/>
{hasProducts && (
<>
<span>Sort by:</span>{" "}
<Dropdown
options={filterOptions}
value={sortValues || ""}
isSearchable={false}
onChange={event => onOrder(event.value)}
/>
</>
)}
</span>
</div>
{products.edges.length > 0 ? (
{hasProducts ? (
<>
<div className="products-list__products__grid">
{products.edges.map(({ node: product }) => (
Expand All @@ -83,12 +90,16 @@ export const ProductList: React.SFC<ProductsListProps> = ({
</>
) : (
<div className="products-list__products-not-found">
We couldn't find any product matching these conditions
{notFoundPhrase}
</div>
)}
</div>
</div>
);
};

ProductList.defaultProps = {
notFoundPhrase: "We couldn't find any product matching these conditions"
};

export default ProductList;
1 change: 1 addition & 0 deletions src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export { default as CheckoutPayment } from "./CheckoutPayment";
export { default as ShippingAddressForm } from "./ShippingAddressForm";
export { default as CheckoutReview } from "./CheckoutReview";
export { default as Debounce } from "./Debounce";
export { default as ProductsFeatured } from "./ProductsFeatured";
9 changes: 9 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,12 @@ export const convertSortByFromString = (sortBy: string) => {
: undefined;
return { field, direction };
};

export function maybe<T>(exp: () => T, d?: T) {
try {
const result = exp();
return result === undefined ? d : result;
} catch {
return d;
}
}
Loading

0 comments on commit afb118a

Please sign in to comment.