Skip to content

Commit

Permalink
#1150 [Recommendation] Adjust product detail page to show similar pro…
Browse files Browse the repository at this point in the history
…duct

- Fix build
  • Loading branch information
Chinh Duong committed Oct 10, 2024
1 parent b87853e commit 4b8df3e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
27 changes: 15 additions & 12 deletions storefront/modules/catalog/components/SimilarProduct.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Col, Row } from 'react-bootstrap';
import SimilarProductCard from '@/common/components/SimilarProductCard';
import { ProductThumbnail } from '../models/ProductThumbnail';
import { getSimilarProductsByProductId } from '../services/ProductService';
import { SimilarProduct } from '../models/SimilarProduct';

type SimilarProductProps = {
productId: number;
Expand All @@ -16,24 +17,27 @@ const SimilarProduct = ({ productId }: SimilarProductProps) => {
fetchSimilarProducts();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

const fetchSimilarProducts = () => {
// Convert API response to ProductThumbnail[
const convertToProductThumbnail = (response: SimilarProduct[]): ProductThumbnail[] => {
return response.map((product) => ({
id: product.id,
name: product.name,
slug: product.slug,
thumbnailUrl: product.thumbnail?.url || '',
price: product.price
}));
};
const fetchSimilarProducts = () => {
getSimilarProductsByProductId(productId)
.then((response) => {
// Transform response to match ProductThumbnail[]
const products: ProductThumbnail[] = response.map((product) => ({
id: product.id,
name: product.name,
slug: product.slug,
thumbnailUrl: product.thumbnail?.url || '',
price: product.price
}));
const products: ProductThumbnail[] = convertToProductThumbnail(response);
setProducts(products);
})
.catch((error) => console.log(error));
};
};

return (
return (
<div className="my-5">
<h4 className="mb-2 text-black">Similar Products</h4>
{products.length > 0 && (
Expand All @@ -48,7 +52,6 @@ const SimilarProduct = ({ productId }: SimilarProductProps) => {
))}
</Row>
)}

{products.length === 0 && <p className="mt-4">No product</p>}
</div>
);
Expand Down
10 changes: 10 additions & 0 deletions storefront/modules/catalog/models/SimilarProduct.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export type SimilarProduct = {
id: number;
name: string;
slug: string;
price: number;
thumbnail: {
id: number;
url: string;
}
};
3 changes: 2 additions & 1 deletion storefront/modules/catalog/services/ProductService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ProductAll, ProductFeature } from '../models/ProductFeature';
import { ProductOptionValueGet } from '../models/ProductOptionValueGet';
import { ProductVariation } from '../models/ProductVariation';
import { ProductsGet } from '../models/ProductsGet';
import { SimilarProduct } from '../models/SimilarProduct';

export async function getFeaturedProducts(pageNo: number): Promise<ProductFeature> {
const response = await fetch(`api/product/storefront/products/featured?pageNo=${pageNo}`);
Expand Down Expand Up @@ -55,7 +56,7 @@ export async function getRelatedProductsByProductId(productId: number): Promise<
return Promise.reject(res);
}

export async function getSimilarProductsByProductId(productId: number): Promise<ProductsGet> {
export async function getSimilarProductsByProductId(productId: number): Promise<SimilarProduct[]> {
const res = await fetch(`/api/recommendation/embedding/product/${productId}/similarity`, {
method: 'GET',
headers: {
Expand Down

0 comments on commit 4b8df3e

Please sign in to comment.