Skip to content

Commit

Permalink
Merge pull request Weaverse#229 from Weaverse/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
hta218 authored Dec 10, 2024
2 parents b96b188 + cd617f2 commit 61fc0da
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 136 deletions.
4 changes: 2 additions & 2 deletions app/components/scroll-area.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export let ScrollArea = forwardRef<HTMLDivElement, ScrollAreaProps>(
</Viewport>
<Scrollbar
className={cn(
"flex touch-none select-none rounded-lg data-[orientation=horizontal]:flex-col",
"flex touch-none select-none data-[orientation=horizontal]:flex-col",
"bg-black/10 dark:bg-gray-700/50",
"transition-colors duration-150 ease-out",
variants({ size }),
Expand All @@ -65,7 +65,7 @@ export let ScrollArea = forwardRef<HTMLDivElement, ScrollAreaProps>(
>
<Thumb
className={cn(
"relative flex-1 rounded-lg shadow-intense",
"relative flex-1 shadow-intense",
"bg-gray-500 dark:bg-gray-500",
"before:absolute before:left-1/2 before:top-1/2 before:-translate-x-1/2 before:-translate-y-1/2 before:content-['']",
"before:h-full before:min-h-[20px] before:w-full before:min-w-[20px]",
Expand Down
5 changes: 3 additions & 2 deletions app/sections/collection-filters/filters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ import type { AppliedFilter } from "~/lib/filter";
import { getAppliedFilterLink, getFilterLink } from "~/lib/filter";
import type { CollectionFiltersData } from ".";
import { Input } from "../../modules/input";
import { cn } from "~/lib/cn";

export function Filters() {
export function Filters({ className }: { className?: string }) {
let parentInstance = useClosestWeaverseItem(".filters-list");
let parentData = parentInstance.data as unknown as CollectionFiltersData;
let { expandFilters, showFiltersCount } = parentData;
Expand Down Expand Up @@ -65,7 +66,7 @@ export function Filters() {
return (
<Accordion.Root
type="multiple"
className="filters-list divide-y divide-line-subtle"
className={cn("filters-list divide-y divide-line-subtle", className)}
key={expandFilters.toString() + showFiltersCount}
defaultValue={expandFilters ? filters.map((filter) => filter.id) : []}
>
Expand Down
69 changes: 22 additions & 47 deletions app/sections/collection-filters/index.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useLoaderData } from "@remix-run/react";
import { Pagination } from "@shopify/hydrogen";
import type { HydrogenComponentSchema } from "@weaverse/hydrogen";
import { forwardRef, useEffect, useState } from "react";
import { useInView } from "react-intersection-observer";
import type { CollectionDetailsQuery } from "storefrontapi.generated";
import Link from "~/components/link";
import { Section, type SectionProps, layoutInputs } from "~/components/section";
import { Filters } from "./filters";
import { ProductsLoadedOnScroll } from "./products-loaded-on-scroll";
import { ProductsPagination } from "./products-pagination";
import { ToolsBar } from "./tools-bar";

export interface CollectionFiltersData {
Expand Down Expand Up @@ -40,7 +38,6 @@ let CollectionFilters = forwardRef<HTMLElement, CollectionFiltersProps>(
loadMoreText,
...rest
} = props;
let { ref, inView } = useInView();
let { collection, collections } = useLoaderData<
CollectionDetailsQuery & {
collections: Array<{ handle: string; title: string }>;
Expand Down Expand Up @@ -86,54 +83,18 @@ let CollectionFilters = forwardRef<HTMLElement, CollectionFiltersProps>(
{...props}
/>
<div className="flex gap-8 pt-6 lg:pt-12 pb-8 lg:pb-20">
{filtersPosition === "sidebar" && (
{enableFilter && filtersPosition === "sidebar" && (
<div className="hidden lg:block shrink-0 w-72 space-y-4">
<div className="font-bold">Filters</div>
<Filters />
</div>
)}
<Pagination connection={collection.products}>
{({
nodes,
isLoading,
nextPageUrl,
previousPageUrl,
hasNextPage,
hasPreviousPage,
state,
}) => (
<div className="flex w-full flex-col gap-8 items-center">
{hasPreviousPage && (
<Link
to={previousPageUrl}
variant="outline"
className="mx-auto"
>
{isLoading ? "Loading..." : loadPrevText}
</Link>
)}
<ProductsLoadedOnScroll
gridSizeDesktop={gridSizeDesktop}
gridSizeMobile={gridSizeMobile}
nodes={nodes}
inView={inView}
nextPageUrl={nextPageUrl}
hasNextPage={hasNextPage}
state={state}
/>
{hasNextPage && (
<Link
ref={ref}
to={nextPageUrl}
variant="outline"
className="mx-auto"
>
{isLoading ? "Loading..." : loadMoreText}
</Link>
)}
</div>
)}
</Pagination>
<ProductsPagination
gridSizeDesktop={gridSizeDesktop}
gridSizeMobile={gridSizeMobile}
loadPrevText={loadPrevText}
loadMoreText={loadMoreText}
/>
</div>
</Section>
);
Expand Down Expand Up @@ -206,6 +167,20 @@ export let schema: HydrogenComponentSchema = {
defaultValue: true,
condition: "enableFilter.eq.true",
},
{
type: "switch",
name: "enableColorSwatch",
label: "Enable color swatches",
defaultValue: true,
condition: "enableFilter.eq.true",
},
{
type: "text",
name: "displayAsButtonFor",
label: "Display as button for:",
defaultValue: "Size, More filters",
condition: "enableFilter.eq.true",
},
],
},
{
Expand Down
57 changes: 0 additions & 57 deletions app/sections/collection-filters/products-loaded-on-scroll.tsx

This file was deleted.

115 changes: 115 additions & 0 deletions app/sections/collection-filters/products-pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { useLoaderData, useNavigate } from "@remix-run/react";
import { Pagination } from "@shopify/hydrogen";
import clsx from "clsx";
import { useEffect } from "react";
import { useInView } from "react-intersection-observer";
import type { CollectionDetailsQuery } from "storefrontapi.generated";
import Link from "~/components/link";
import { getImageLoadingPriority } from "~/lib/const";
import { ProductCard } from "~/modules/product-card";

export function ProductsPagination({
gridSizeDesktop,
gridSizeMobile,
loadPrevText,
loadMoreText,
}: {
gridSizeDesktop: number;
gridSizeMobile: number;
loadPrevText: string;
loadMoreText: string;
}) {
let { collection } = useLoaderData<
CollectionDetailsQuery & {
collections: Array<{ handle: string; title: string }>;
}
>();
let { ref, inView } = useInView();

return (
<Pagination connection={collection.products}>
{({
nodes,
isLoading,
nextPageUrl,
previousPageUrl,
hasNextPage,
hasPreviousPage,
state,
}) => (
<div
className="flex w-full flex-col gap-8 items-center"
style={
{
"--cols-mobile": `repeat(${gridSizeMobile || 1}, minmax(0, 1fr))`,
"--cols-desktop": `repeat(${gridSizeDesktop || 3}, minmax(0, 1fr))`,
} as React.CSSProperties
}
>
{hasPreviousPage && (
<Link to={previousPageUrl} variant="outline" className="mx-auto">
{isLoading ? "Loading..." : loadPrevText}
</Link>
)}
<ProductsLoadedOnScroll
nodes={nodes}
inView={inView}
nextPageUrl={nextPageUrl}
hasNextPage={hasNextPage}
state={state}
/>
{hasNextPage && (
<Link
ref={ref}
to={nextPageUrl}
variant="outline"
className="mx-auto"
>
{isLoading ? "Loading..." : loadMoreText}
</Link>
)}
</div>
)}
</Pagination>
);
}

interface ProductsLoadedOnScrollProps {
nodes: any;
inView: boolean;
nextPageUrl: string;
hasNextPage: boolean;
state: any;
}

function ProductsLoadedOnScroll(props: ProductsLoadedOnScrollProps) {
let { nodes, inView, nextPageUrl, hasNextPage, state } = props;
let navigate = useNavigate();

useEffect(() => {
if (inView && hasNextPage) {
navigate(nextPageUrl, {
replace: true,
preventScrollReset: true,
state,
});
}
}, [inView, navigate, state, nextPageUrl, hasNextPage]);

return (
<div
className={clsx([
"w-full gap-x-1.5 gap-y-8 lg:gap-y-10",
"grid grid-cols-[--cols-mobile] lg:grid-cols-[--cols-desktop]",
])}
>
{nodes.map((product: any, i: number) => (
<ProductCard
key={product.id}
product={product}
loading={getImageLoadingPriority(i)}
/>
))}
</div>
);
}
28 changes: 14 additions & 14 deletions app/sections/collection-filters/sort.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ const PRODUCT_SORT: { label: string; key: SortParam }[] = [
},
];

const SEARCH_SORT: { label: string; key: SortParam }[] = [
{
label: "Price: Low - High",
key: "price-low-high",
},
{
label: "Price: High - Low",
key: "price-high-low",
},
{
label: "Relevance",
key: "relevance",
},
];
// const SEARCH_SORT: { label: string; key: SortParam }[] = [
// {
// label: "Price: Low - High",
// key: "price-low-high",
// },
// {
// label: "Price: High - Low",
// key: "price-high-low",
// },
// {
// label: "Relevance",
// key: "relevance",
// },
// ];

export function Sort() {
let [params] = useSearchParams();
Expand Down
Loading

0 comments on commit 61fc0da

Please sign in to comment.