Skip to content

Commit

Permalink
Merge pull request #274 from iFixit/improve-product-list-item-pricing
Browse files Browse the repository at this point in the history
Improve product list item pricing
  • Loading branch information
danielbeardsley committed May 5, 2022
2 parents cdedad5 + 9015d2a commit 976e726
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 49 deletions.
Expand Up @@ -9,11 +9,10 @@ import {
ProductCardRating,
ProductCardTitle,
} from '@components/common';
import { computeDiscountPercentage } from '@helpers/commerce-helpers';
import { useAuthenticatedUser } from '@ifixit/auth-sdk';
import { useAppContext } from '@ifixit/ui';
import { ProductSearchHit } from '@models/product-list';
import * as React from 'react';
import { useProductSearchHitPricing } from './useProductSearchHitPricing';

export type ProductGridProps = React.PropsWithChildren<unknown>;

Expand All @@ -40,32 +39,10 @@ export interface ProductGridItemProps {
}

export function ProductGridItem({ product }: ProductGridItemProps) {
const user = useAuthenticatedUser();
const appContext = useAppContext();

const proTierPrice = React.useMemo(() => {
const proTier = user.data?.discountTier ?? null;
if (proTier) {
const priceString = product.price_tiers?.[proTier];
return priceString == null ? null : parseFloat(priceString);
}
return null;
}, [user.data?.discountTier, product.price_tiers]);

const isDiscounted =
product.compare_at_price != null &&
product.compare_at_price > product.price_float;

const percentage = isDiscounted
? computeDiscountPercentage(
product.price_float * 100,
product.compare_at_price! * 100
)
: 0;

const price = proTierPrice ?? product.price_float;
const compareAtPrice =
proTierPrice == null ? product.compare_at_price : undefined;
const { price, compareAtPrice, isDiscounted, percentage } =
useProductSearchHitPricing(product);

return (
<LinkBox as="article" display="block" w="full">
Expand Down
Expand Up @@ -14,12 +14,11 @@ import {
VStack,
} from '@chakra-ui/react';
import { Rating } from '@components/ui';
import { computeDiscountPercentage } from '@helpers/commerce-helpers';
import { useAuthenticatedUser } from '@ifixit/auth-sdk';
import { useAppContext } from '@ifixit/ui';
import { ProductSearchHit } from '@models/product-list';
import Image from 'next/image';
import * as React from 'react';
import { useProductSearchHitPricing } from './useProductSearchHitPricing';

export type ProductListProps = React.PropsWithChildren<unknown>;

Expand All @@ -42,29 +41,10 @@ export interface ProductListItemProps {
}

export function ProductListItem({ product }: ProductListItemProps) {
const user = useAuthenticatedUser();
const appContext = useAppContext();

const proTierPrice = React.useMemo(() => {
const proTier = user.data?.discountTier ?? null;
return (proTier ? product.price_tiers?.[proTier] : null) ?? null;
}, [user.data?.discountTier, product.price_tiers]);

const isDiscounted =
proTierPrice == null &&
product.compare_at_price != null &&
product.compare_at_price > product.price_float;

const price = proTierPrice ?? product.price_float;
const compareAtPrice =
proTierPrice == null ? product.compare_at_price : null;

const percentage = isDiscounted
? computeDiscountPercentage(
product.price_float * 100,
product.compare_at_price! * 100
)
: 0;
const { price, compareAtPrice, isDiscounted, percentage } =
useProductSearchHitPricing(product);

const productHeadingId = `product-heading-${product.handle}`;

Expand Down
@@ -0,0 +1,38 @@
import * as React from 'react';
import { useAuthenticatedUser } from '@ifixit/auth-sdk';
import { ProductSearchHit } from '@models/product-list';
import { computeDiscountPercentage } from '@helpers/commerce-helpers';

export type ProductSearchHitPricing = {
price: number;
compareAtPrice: number | undefined;
isDiscounted: boolean;
percentage: number;
};
export function useProductSearchHitPricing(
product: ProductSearchHit
): ProductSearchHitPricing {
const user = useAuthenticatedUser();

let proTierPrice: number | null = null;
const discountTier = user.data?.discountTier ?? null;
if (discountTier) {
const priceString = product.price_tiers?.[discountTier];
proTierPrice = priceString == null ? null : parseFloat(priceString);
}

const price = proTierPrice ?? product.price_float;
const compareAtPrice = product.compare_at_price ?? product.price_float;
const isDiscounted = compareAtPrice != null && compareAtPrice > price;

const percentage = isDiscounted
? computeDiscountPercentage(price * 100, compareAtPrice! * 100)
: 0;

return {
price,
compareAtPrice,
isDiscounted,
percentage,
};
}

1 comment on commit 976e726

@vercel
Copy link

@vercel vercel bot commented on 976e726 May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-commerce – ./

react-commerce.vercel.app
react-commerce-ifixit.vercel.app
react-commerce-git-main-ifixit.vercel.app

Please sign in to comment.