The Vue Storefront Essentials Course is now available! Learn More
GetProductReviews

GetProductReviews

Implements GetProductReviews Unified Method.

Source

import { defineApi } from "@vsf-enterprise/unified-api-sapcc";
import { Review } from "@vsf-enterprise/sapcc-types";
import { SfPagination, getNormalizers } from "@vue-storefront/unified-data-model";

export const getProductReviews = defineApi.getProductReviews(async (context, args) => {
  const { productId, currentPage, pageSize } = args;
  let reviewsData: Review[] = [];
  const { normalizeProductReview, normalizePagination } = getNormalizers(context);

  try {
    const response = await context.api.getProductReviews({
      productCode: productId,
    });
    reviewsData = response.reviews ?? [];
  } catch {
    console.error("Error while fetching product reviews");
  }

  const { data, pagination } = paginatedResponse(reviewsData, currentPage, pageSize);

  return {
    reviews: data.map((review) => normalizeProductReview(review)),
    pagination: pagination as ReturnType<typeof normalizePagination>,
  };
});

type PaginatedResponse<TData> = {
  data: TData[];
  pagination: SfPagination;
};

function paginatedResponse<TData>(
  data: TData[],
  currentPage = 0,
  pageSize = 0,
): PaginatedResponse<TData> {
  const totalPages = pageSize ? Math.ceil(data.length / pageSize) : 0;

  const start = currentPage * pageSize;
  const end = start + pageSize;

  return {
    data: data.slice(start, end),
    pagination: {
      currentPage,
      pageSize,
      totalResults: data.length,
      totalPages,
    },
  };
}