Skip to content

Commit 5c4934f

Browse files
committed
feat(reviews): conditionally display reviews based on display settings
1 parent 5b2edd0 commit 5c4934f

13 files changed

Lines changed: 99 additions & 20 deletions

File tree

.changeset/cute-trees-swim.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@bigcommerce/catalyst-core": minor
3+
---
4+
5+
Conditionally display product ratings in the storefront based on `site.settings.display.showProductRating`. The storefront logic when this setting is enabled/disabled matches exactly the logic of Stencil + Cornerstone.

core/app/[locale]/(default)/(faceted)/brand/[slug]/page-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ const BrandPageQuery = graphql(`
2626
productComparisonsEnabled
2727
}
2828
}
29+
display {
30+
showProductRating
31+
}
2932
reviews {
3033
enabled
3134
}

core/app/[locale]/(default)/(faceted)/brand/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export default async function Brand(props: Props) {
103103
return notFound();
104104
}
105105

106-
const reviewsEnabled = settings?.reviews.enabled ?? false;
106+
const showRating = Boolean(settings?.reviews.enabled && settings.display.showProductRating);
107107

108108
const productComparisonsEnabled =
109109
settings?.storefront.catalog?.productComparisonsEnabled ?? false;
@@ -221,8 +221,8 @@ export default async function Brand(props: Props) {
221221
rangeFilterApplyLabel={t('FacetedSearch.Range.apply')}
222222
removeLabel={t('Compare.remove')}
223223
resetFiltersLabel={t('FacetedSearch.resetFilters')}
224-
reviewsEnabled={reviewsEnabled}
225224
showCompare={productComparisonsEnabled}
225+
showRating={showRating}
226226
sortDefaultValue="featured"
227227
sortLabel={t('Search.title')}
228228
sortOptions={[

core/app/[locale]/(default)/(faceted)/category/[slug]/page-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ const CategoryPageQuery = graphql(
4545
productComparisonsEnabled
4646
}
4747
}
48+
display {
49+
showProductRating
50+
}
4851
reviews {
4952
enabled
5053
}

core/app/[locale]/(default)/(faceted)/category/[slug]/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ export default async function Category(props: Props) {
113113
href: path ?? '#',
114114
}));
115115

116-
const reviewsEnabled = settings?.reviews.enabled ?? false;
116+
const showRating = Boolean(settings?.reviews.enabled && settings.display.showProductRating);
117117

118118
const productComparisonsEnabled =
119119
settings?.storefront.catalog?.productComparisonsEnabled ?? false;
@@ -257,8 +257,8 @@ export default async function Category(props: Props) {
257257
rangeFilterApplyLabel={t('FacetedSearch.Range.apply')}
258258
removeLabel={t('Compare.remove')}
259259
resetFiltersLabel={t('FacetedSearch.resetFilters')}
260-
reviewsEnabled={reviewsEnabled}
261260
showCompare={productComparisonsEnabled}
261+
showRating={showRating}
262262
sortDefaultValue="featured"
263263
sortLabel={t('SortBy.sortBy')}
264264
sortOptions={[

core/app/[locale]/(default)/(faceted)/search/page-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ const SearchPageQuery = graphql(`
1818
productComparisonsEnabled
1919
}
2020
}
21+
display {
22+
showProductRating
23+
}
2124
reviews {
2225
enabled
2326
}

core/app/[locale]/(default)/(faceted)/search/page.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ export default async function Search(props: Props) {
7878

7979
const { settings } = await getSearchPageData();
8080

81-
const reviewsEnabled = settings?.reviews.enabled ?? false;
81+
const showRating = Boolean(settings?.reviews.enabled && settings.display.showProductRating);
8282

8383
const productComparisonsEnabled =
8484
settings?.storefront.catalog?.productComparisonsEnabled ?? false;
@@ -253,8 +253,8 @@ export default async function Search(props: Props) {
253253
rangeFilterApplyLabel={t('FacetedSearch.Range.apply')}
254254
removeLabel={t('Compare.remove')}
255255
resetFiltersLabel={t('FacetedSearch.resetFilters')}
256-
reviewsEnabled={reviewsEnabled}
257256
showCompare={productComparisonsEnabled}
257+
showRating={showRating}
258258
sortDefaultValue="featured"
259259
sortLabel={t('SortBy.sortBy')}
260260
sortOptions={[

core/app/[locale]/(default)/product/[slug]/page-data.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,9 @@ const ProductQuery = graphql(
175175
reviews {
176176
enabled
177177
}
178+
display {
179+
showProductRating
180+
}
178181
}
179182
product(entityId: $entityId) {
180183
entityId

core/app/[locale]/(default)/product/[slug]/page.tsx

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ import { SearchParams } from 'nuqs/server';
77
import { Stream, Streamable } from '@/vibes/soul/lib/streamable';
88
import { FeaturedProductCarousel } from '@/vibes/soul/sections/featured-product-carousel';
99
import { ProductDetail } from '@/vibes/soul/sections/product-detail';
10-
import { getSessionCustomerAccessToken } from '~/auth';
10+
import { auth, getSessionCustomerAccessToken } from '~/auth';
1111
import { pricesTransformer } from '~/data-transformers/prices-transformer';
1212
import { productCardTransformer } from '~/data-transformers/product-card-transformer';
1313
import { productOptionsTransformer } from '~/data-transformers/product-options-transformer';
1414
import { getPreferredCurrencyCode } from '~/lib/currency';
1515

1616
import { addToCart } from './_actions/add-to-cart';
17+
import { submitReview } from './_actions/submit-review';
1718
import { ProductAnalyticsProvider } from './_components/product-analytics-provider';
1819
import { ProductSchema } from './_components/product-schema';
1920
import { ProductViewed } from './_components/product-viewed';
@@ -80,7 +81,8 @@ export default async function Product({ params, searchParams }: Props) {
8081

8182
const { product: baseProduct, settings } = await getProduct(productId, customerAccessToken);
8283

83-
const reviewsEnabled = settings?.reviews.enabled ?? false;
84+
const reviewsEnabled = Boolean(settings?.reviews.enabled && !settings.display.showProductRating);
85+
const showRating = Boolean(settings?.reviews.enabled && settings.display.showProductRating);
8486

8587
if (!baseProduct) {
8688
return notFound();
@@ -510,6 +512,21 @@ export default async function Product({ params, searchParams }: Props) {
510512
};
511513
});
512514

515+
const streamableUser = Streamable.from(async () => {
516+
const session = await auth();
517+
const firstName = session?.user?.firstName ?? '';
518+
const lastName = session?.user?.lastName ?? '';
519+
520+
if (!firstName || !lastName) {
521+
return { email: session?.user?.email ?? '', name: '' };
522+
}
523+
524+
const lastInitial = lastName.charAt(0).toUpperCase();
525+
const obfuscatedName = `${firstName} ${lastInitial}.`;
526+
527+
return { email: session?.user?.email ?? '', name: obfuscatedName };
528+
});
529+
513530
return (
514531
<>
515532
<ProductAnalyticsProvider data={streamableAnalyticsData}>
@@ -538,6 +555,7 @@ export default async function Product({ params, searchParams }: Props) {
538555
images: streamableImages,
539556
price: streamablePrices,
540557
reviewsEnabled,
558+
showRating,
541559
subtitle: baseProduct.brand?.name,
542560
rating: baseProduct.reviewSummary.averageRating,
543561
accordions: streameableAccordions,
@@ -547,7 +565,9 @@ export default async function Product({ params, searchParams }: Props) {
547565
backorderDisplayData: streamableBackorderDisplayData,
548566
}}
549567
quantityLabel={t('ProductDetails.quantity')}
568+
reviewFormAction={submitReview}
550569
thumbnailLabel={t('ProductDetails.thumbnail')}
570+
user={streamableUser}
551571
/>
552572
</ProductAnalyticsProvider>
553573

@@ -562,7 +582,7 @@ export default async function Product({ params, searchParams }: Props) {
562582
title={t('RelatedProducts.title')}
563583
/>
564584

565-
{reviewsEnabled && (
585+
{showRating && (
566586
<Reviews
567587
productId={productId}
568588
searchParams={searchParams}

core/vibes/soul/primitives/product-card/index.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ export interface ProductCardProps {
3232
compareLabel?: string;
3333
compareParamName?: string;
3434
product: Product;
35-
reviewsEnabled?: boolean;
35+
showRating?: boolean;
3636
}
3737

3838
// eslint-disable-next-line valid-jsdoc
@@ -59,7 +59,7 @@ export interface ProductCardProps {
5959
*/
6060
export function ProductCard({
6161
product: { id, title, subtitle, badge, price, image, href, inventoryMessage, rating },
62-
reviewsEnabled = false,
62+
showRating = false,
6363
colorScheme = 'light',
6464
className,
6565
showCompare = false,
@@ -153,9 +153,7 @@ export function ProductCard({
153153
</span>
154154
)}
155155
{price != null && <PriceLabel colorScheme={colorScheme} price={price} />}
156-
{reviewsEnabled && typeof rating === 'number' && rating > 0 && (
157-
<Rating rating={rating} />
158-
)}
156+
{showRating && typeof rating === 'number' && rating > 0 && <Rating rating={rating} />}
159157
<span
160158
className={clsx(
161159
'block text-sm font-normal',

0 commit comments

Comments
 (0)