AbiirSkincare/Hydrogen/web/app/components/WishlistPageContent.tsx

151 lines
5.3 KiB
TypeScript

import {useCallback, useEffect, useState} from 'react';
import {Link} from 'react-router';
import {AbiirProductListItem} from '~/components/AbiirProductListItem';
import {CollectionPageView} from '~/components/CollectionPageView';
import {useAbiirWishlistCount} from '~/hooks/useAbiirWishlistCount';
import {
ABIIR_WISHLIST_CHANGE_EVENT,
readWishlistHandlesFromWindow,
} from '~/lib/abiirWishlistStorage';
import {
applyDisplayRatingsToListItemData,
mapStorefrontProductToAbiirProductListItemData,
type StorefrontProductNode,
} from '~/lib/featuredProducts';
import {ABIIR_PRODUCT_LIST_GRID_CLASS} from '~/lib/abiirProductListGrid';
import type {DisplayRating} from '~/lib/productDisplayRatings.server';
async function fetchWishlistProducts(
handles: string[],
): Promise<{
products: StorefrontProductNode[];
displayRatingsByHandle: Record<string, DisplayRating>;
}> {
if (handles.length === 0) {
return {products: [], displayRatingsByHandle: {}};
}
const fd = new FormData();
fd.set('handles', JSON.stringify(handles));
const res = await fetch('/api/wishlist-products', {method: 'POST', body: fd});
if (!res.ok) return {products: [], displayRatingsByHandle: {}};
const data = (await res.json()) as {
products?: StorefrontProductNode[];
displayRatingsByHandle?: Record<string, DisplayRating>;
};
return {
products: data.products ?? [],
displayRatingsByHandle: data.displayRatingsByHandle ?? {},
};
}
function WishlistProductCard({
product,
index,
displayRatingsByHandle,
}: {
product: StorefrontProductNode;
index: number;
displayRatingsByHandle: Record<string, DisplayRating>;
}) {
const handle = product.handle?.trim() ?? '';
const item = applyDisplayRatingsToListItemData(
mapStorefrontProductToAbiirProductListItemData(product),
handle ? displayRatingsByHandle[handle] : undefined,
);
return <AbiirProductListItem index={index} item={item} />;
}
export function WishlistPageContent() {
const [products, setProducts] = useState<StorefrontProductNode[] | null>(null);
const [displayRatingsByHandle, setDisplayRatingsByHandle] = useState<
Record<string, DisplayRating>
>({});
const [loading, setLoading] = useState(true);
const wishlistHandleCount = useAbiirWishlistCount();
const refresh = useCallback(async () => {
const handles = readWishlistHandlesFromWindow();
setLoading(true);
const {products: next, displayRatingsByHandle: stats} =
await fetchWishlistProducts(handles);
setProducts(next);
setDisplayRatingsByHandle(stats);
setLoading(false);
}, []);
useEffect(() => {
void refresh();
window.addEventListener(ABIIR_WISHLIST_CHANGE_EVENT, refresh);
window.addEventListener('storage', refresh);
return () => {
window.removeEventListener(ABIIR_WISHLIST_CHANGE_EVENT, refresh);
window.removeEventListener('storage', refresh);
};
}, [refresh]);
const count = products?.length ?? 0;
const loaded = !loading;
const hasStoreHandles = wishlistHandleCount > 0;
const showEmptyCta = loaded && !hasStoreHandles;
const showOrphan = loaded && hasStoreHandles && count === 0;
const showGrid = loaded && count > 0;
return (
<CollectionPageView
eyebrow="Saved for later"
productCountLine={
loading ? 'Loading…' : `${count} ${count === 1 ? 'item' : 'items'}`
}
showFilterTabs={false}
title="Wishlist"
>
{loading && products === null ?
<p className="m-0 font-['Raleway',sans-serif] text-[#5a5550]">
Loading your list…
</p>
: showEmptyCta ?
<div className="flex flex-col items-start gap-4 text-left">
<p className="m-0 max-w-md font-['Raleway',sans-serif] text-base leading-[1.5] text-[#5a5550]">
You haven&rsquo;t saved any products yet. Browse the shop and tap
the heart on a product to add it here.
</p>
<Link
className="font-['Raleway',sans-serif] text-sm font-medium uppercase tracking-[0.12em] text-[#5d0b24] no-underline transition-opacity hover:opacity-80"
to="/collections/all"
>
Shop all products
</Link>
</div>
: showOrphan ?
<div className="flex flex-col items-start gap-4 text-left">
<p className="m-0 max-w-md font-['Raleway',sans-serif] text-base leading-[1.5] text-[#5a5550]">
We couldn&rsquo;t load those products. They may be unavailable. Try
removing them from your list and add items again from the store.
</p>
<Link
className="font-['Raleway',sans-serif] text-sm font-medium uppercase tracking-[0.12em] text-[#5d0b24] no-underline transition-opacity hover:opacity-80"
to="/collections/all"
>
Shop all products
</Link>
</div>
: showGrid ?
<ul className={`${ABIIR_PRODUCT_LIST_GRID_CLASS} list-none p-0`}>
{(products ?? []).map((product, index) => (
<li key={product.id ?? product.handle ?? index}>
<WishlistProductCard
index={index}
product={product}
displayRatingsByHandle={displayRatingsByHandle}
/>
</li>
))}
</ul>
: <p className="m-0 font-['Raleway',sans-serif] text-[#5a5550]">
Loading your list…
</p>
}
</CollectionPageView>
);
}