Vue Storefront is now Alokai! Learn More
GetProductReviews

GetProductReviews

Implements GetProductReviews Unified Method.

Source

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

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,
  };
});

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,
    },
  };
}