-
Notifications
You must be signed in to change notification settings - Fork 85
MM2 1479 Fix: PLP issues, remove PLP fetch limit to 20, fix pagination, add loading state #221
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
Open
sylwia-werner
wants to merge
6
commits into
test
Choose a base branch
from
fix/mm2-1479/plp-issues
base: test
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+121
−51
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
bdb1140
fix: remove limit to 20 products, fix pagination
sylwia-werner a3c727b
fix: add skeleton for algolia product listing to avoid layout shifts
sylwia-werner ff55654
fix: add existing product card skeleton component to algolia product …
sylwia-werner 8879a55
refactor: separate product listing components
sylwia-werner 9cdc6a4
fix: PR fixes
sylwia-werner 3d577c4
fix: PR fixes, add li to element to ensure semantics
sylwia-werner File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
src/components/molecules/ProductListingLoadingView/ProductListingLoadingView.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| import { SkeletonProductCard } from "@/components/organisms/ProductCard/SkeletonProductCard" | ||
| import { PRODUCT_LIMIT } from "@/const" | ||
|
|
||
| const ProductListingLoadingView = () => ( | ||
| <div className="flex flex-wrap gap-4"> | ||
| {Array.from({ length: PRODUCT_LIMIT }).map((_, idx) => ( | ||
| <SkeletonProductCard key={idx} /> | ||
| ))} | ||
| </div> | ||
| ) | ||
|
|
||
| export default ProductListingLoadingView |
10 changes: 10 additions & 0 deletions
10
src/components/molecules/ProductListingNoResultsView/ProductListingNoResultsView.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| const ProductListingNoResultsView = () => ( | ||
| <div className="text-center w-full my-10"> | ||
| <h2 className="uppercase text-primary heading-lg">No results</h2> | ||
| <p className="mt-4 text-lg"> | ||
| Sorry, we can't find any results for your criteria | ||
| </p> | ||
| </div> | ||
| ) | ||
|
|
||
| export default ProductListingNoResultsView |
29 changes: 29 additions & 0 deletions
29
src/components/molecules/ProductListingProductsView/ProductListingProductsView.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| import { HttpTypes } from "@medusajs/types" | ||
| import { BaseHit, Hit } from "instantsearch.js" | ||
| import { ProductCard } from "@/components/organisms" | ||
|
|
||
| interface Props { | ||
| products: Hit<BaseHit>[] | ||
| apiProducts: HttpTypes.StoreProduct[] | null | ||
| } | ||
|
|
||
| const ProductListingProductsView = ({ products, apiProducts }: Props) => ( | ||
| <div className="w-full"> | ||
| <ul className="flex flex-wrap gap-4"> | ||
| {products.map( | ||
| (hit) => | ||
| apiProducts?.find((p) => p.id === hit.objectID) && ( | ||
| <li key={hit.objectID} className="w-full lg:w-[calc(25%-1rem)] min-w-[250px]"> | ||
| <ProductCard | ||
| api_product={apiProducts?.find((p) => p.id === hit.objectID)} | ||
| product={hit} | ||
| className="w-full h-full lg:w-full min-w-0" | ||
| /> | ||
| </li> | ||
| ) | ||
| )} | ||
| </ul> | ||
| </div> | ||
| ) | ||
|
|
||
| export default ProductListingProductsView |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,18 +3,22 @@ | |
| import { HttpTypes } from "@medusajs/types" | ||
| import { | ||
| AlgoliaProductSidebar, | ||
| ProductCard, | ||
| ProductListingActiveFilters, | ||
| ProductsPagination, | ||
| } from "@/components/organisms" | ||
| import { | ||
| ProductListingLoadingView, | ||
| ProductListingNoResultsView, | ||
| ProductListingProductsView, | ||
| } from "@/components/molecules" | ||
| import { client } from "@/lib/client" | ||
| import { Configure, useHits } from "react-instantsearch" | ||
| import { InstantSearchNext } from "react-instantsearch-nextjs" | ||
| import { useSearchParams } from "next/navigation" | ||
| import { getFacedFilters } from "@/lib/helpers/get-faced-filters" | ||
| import { PRODUCT_LIMIT } from "@/const" | ||
| import { ProductListingSkeleton } from "@/components/organisms/ProductListingSkeleton/ProductListingSkeleton" | ||
| import { useEffect, useState } from "react" | ||
| import { useEffect, useMemo, useState } from "react" | ||
| import { listProducts } from "@/lib/data/products" | ||
| import { getProductPrice } from "@/lib/helpers/get-product-price" | ||
|
|
||
|
|
@@ -31,10 +35,11 @@ export const AlgoliaProductsListing = ({ | |
| seller_handle?: string | ||
| currency_code: string | ||
| }) => { | ||
| const searchParamas = useSearchParams() | ||
| const searchParams = useSearchParams() | ||
|
|
||
| const facetFilters: string = getFacedFilters(searchParamas) | ||
| const query: string = searchParamas.get("query") || "" | ||
| const facetFilters: string = getFacedFilters(searchParams) | ||
| const query: string = searchParams.get("query") || "" | ||
| const page: number = +(searchParams.get("page") || 1) | ||
|
|
||
| const filters = `${ | ||
| seller_handle | ||
|
|
@@ -52,7 +57,12 @@ export const AlgoliaProductsListing = ({ | |
|
|
||
| return ( | ||
| <InstantSearchNext searchClient={client} indexName="products"> | ||
| <Configure query={query} filters={filters} /> | ||
| <Configure | ||
| query={query} | ||
| filters={filters} | ||
| hitsPerPage={PRODUCT_LIMIT} | ||
| page={page - 1} | ||
| /> | ||
| <ProductsListing | ||
| locale={locale} | ||
| currency_code={currency_code} | ||
|
|
@@ -74,13 +84,19 @@ const ProductsListing = ({ | |
| const [apiProducts, setApiProducts] = useState< | ||
| HttpTypes.StoreProduct[] | null | ||
| >(null) | ||
| const [isLoadingProducts, setIsLoadingProducts] = useState(false) | ||
| const { items, results } = useHits() | ||
|
|
||
| const searchParamas = useSearchParams() | ||
| const searchParams = useSearchParams() | ||
|
|
||
| const itemsKey = useMemo( | ||
| () => items.map((item) => item.objectID).join(","), | ||
| [items] | ||
| ) | ||
|
|
||
| async function handleSetProducts() { | ||
| try { | ||
| setApiProducts(null) | ||
| setIsLoadingProducts(true) | ||
| const { response } = await listProducts({ | ||
| countryCode: locale, | ||
| queryParams: { | ||
|
|
@@ -99,34 +115,40 @@ const ProductsListing = ({ | |
| ) | ||
| } catch (error) { | ||
| setApiProducts(null) | ||
| } finally { | ||
| setIsLoadingProducts(false) | ||
| } | ||
| } | ||
|
|
||
| useEffect(() => { | ||
| handleSetProducts() | ||
| }, [items.length]) | ||
| if (items.length > 0) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is there any chance for items to be undefined?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. useHits returns empty array if there's no items |
||
| handleSetProducts() | ||
| } else { | ||
| setApiProducts([]) | ||
| setIsLoadingProducts(false) | ||
| } | ||
| }, [itemsKey]) | ||
|
|
||
| if (!results?.processingTimeMS) return <ProductListingSkeleton /> | ||
|
|
||
| const page: number = +(searchParamas.get("page") || 1) | ||
| const isLoading = isLoadingProducts && items.length > 0 | ||
|
|
||
| const filteredProducts = items.filter((pr) => | ||
| apiProducts?.some((p: any) => p.id === pr.objectID) | ||
| apiProducts?.some((p) => p.id === pr.objectID) | ||
| ) | ||
|
|
||
| const products = filteredProducts | ||
| .filter((pr) => | ||
| apiProducts?.some( | ||
| (p: any) => p.id === pr.objectID && filterProductsByCurrencyCode(p) | ||
| ) | ||
| const products = filteredProducts.filter((pr) => | ||
| apiProducts?.some( | ||
| (p) => p.id === pr.objectID && filterProductsByCurrencyCode(p) | ||
| ) | ||
| .slice((page - 1) * PRODUCT_LIMIT, page * PRODUCT_LIMIT) | ||
| ) | ||
|
|
||
| const count = filteredProducts?.length || 0 | ||
| const pages = Math.ceil(count / PRODUCT_LIMIT) || 1 | ||
| const count = results?.nbHits || 0 | ||
| const pages = results?.nbPages || 1 | ||
|
|
||
| function filterProductsByCurrencyCode(product: HttpTypes.StoreProduct) { | ||
| const minPrice = searchParamas.get("min_price") | ||
| const maxPrice = searchParamas.get("max_price") | ||
| const minPrice = searchParams.get("min_price") | ||
| const maxPrice = searchParams.get("max_price") | ||
|
|
||
| if ([minPrice, maxPrice].some((price) => typeof price === "string")) { | ||
| const variantsWithCurrencyCode = product?.variants?.filter( | ||
|
|
@@ -173,35 +195,23 @@ const ProductsListing = ({ | |
| <div className="w-[280px] flex-shrink-0 hidden md:block"> | ||
| <AlgoliaProductSidebar /> | ||
| </div> | ||
| <div className="w-full"> | ||
| {!items.length ? ( | ||
| <div className="text-center w-full my-10"> | ||
| <h2 className="uppercase text-primary heading-lg">no results</h2> | ||
| <p className="mt-4 text-lg"> | ||
| Sorry, we can't find any results for your criteria | ||
| </p> | ||
| </div> | ||
| ) : ( | ||
| <div className="w-full"> | ||
| <ul className="flex flex-wrap gap-4"> | ||
| {products.map( | ||
| (hit) => | ||
| apiProducts?.find((p: any) => p.id === hit.objectID) && ( | ||
| <ProductCard | ||
| api_product={apiProducts?.find( | ||
| (p: any) => p.id === hit.objectID | ||
| )} | ||
| key={hit.objectID} | ||
| product={hit} | ||
| /> | ||
| ) | ||
| )} | ||
| </ul> | ||
| </div> | ||
| <div className="w-full flex flex-col"> | ||
| {isLoading && <ProductListingLoadingView />} | ||
|
|
||
| {!isLoading && !items.length && <ProductListingNoResultsView />} | ||
|
|
||
| {!isLoading && items.length > 0 && ( | ||
| <ProductListingProductsView | ||
| products={products} | ||
| apiProducts={apiProducts} | ||
| /> | ||
| )} | ||
|
|
||
| <div className="mt-auto"> | ||
| <ProductsPagination pages={pages} /> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| <ProductsPagination pages={pages} /> | ||
| </div> | ||
| ) | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is there any chance for items to be undefined?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
useHits returns empty array if there's no items