Rate And Review API¶
Feature name: product-rating or product-review
Api name: RateAndReviewApi
API used to handle actions regarding rating and reviewing products.
classDiagram
direction LR
class selectHasBeenReviewed["selectHasBeenReviewed$"]
RateAndReviewApi --> hasBeenReviewed
RateAndReviewApi --> saveThatHasBeenReviewed
RateAndReviewApi --> selectHasBeenReviewed
RateAndReviewApi --> rate
RateAndReviewApi --> review
RateAndReviewApi --> rateAndReview
class RateAndReviewApi {
hasBeenReviewed(productId: string) boolean
saveThatHasBeenReviewed(productId: string) void
selectHasBeenReviewed$(productId: string) Observable~boolean~
rate(props: TRateProps) Promise~TResponseStatus|undefined~
review(props: TReviewProps) Promise~TResponseStatus|undefined~
rateAndReview(props: TRateAndReviewProps) Promise~TResponseStatus|undefined~
}
link hasBeenReviewed "../methods/subscribe/"
link saveThatHasBeenReviewed "../methods/unsubscribe/"
link selectHasBeenReviewed "../methods/is-subscribed/"
link rate "../methods/rate/"
link review "../methods/review/"
link rateAndReview "../methods/rate-and-review/"
Get API¶
To get the Rate And Review API
use its name RateAndReviewApi
with the getApi
method.
This API is initialized asynchronously which means that if you use the getApiSync
method to get it, it might not exist yet and you will get an error as a result.
useStorefront(async (storefront) => {
const rateAndReviewApi = await storefront.getApi('RateAndReviewApi');
});
This API is initialized only after product-rating or product-review webcomponent has been added to the page. Unless you add at least one of those webcomponent, you won't be able to fetch the API. If you wish to use the API alone without the webcomponent you can use the registerDynamic method from the Feature System API. Here is an example on how to do it:
useStorefront(async (storefront) => {
const featureSystemApi = this.getApiSync('FeatureSystemApi');
await featureSystemApi.registerDynamic('product-rating');
const rateAndReviewApi = await storefront.getApi('RateAndReviewApi');
});
Methods¶
- hasBeenReviewed - returns an information about whether the product with a given ID has been reviewed before
- saveThatHasBeenReviewed - saves the information that the product has been reviewed to the local storage.
- selectHasBeenReviewed - returns an observable with the information about whether the product with a given ID has been reviewed before from the local storage.
- rate - rate a given product
- review - review a given product
- rateAndReview - rate and review a given product
Event Bus events¶
Methods of this API dispatch the following events with the Event Bus:
Example¶
In this example we make a RateAndReviewApi
call to check whether a given product has been reviewed before.
useStorefront((storefront) => {
const rateAndReviewApi = await storefront.getApi('rateAndReviewApi');
const hasBeenReviewed = rateAndReviewApi.hasBeenReviewed("145");
if (hasBeenReviewed) {
// do something if product has been reviewed
}
if (!hasBeenReviewed) {
// do something if product has not been reviewed
}
});
Example¶
In this example we make a RateAndReviewApi
call to save the information that the product has been reviewed to the local storage.
useStorefront((storefront) => {
const rateAndReviewApi = await storefront.getApi('rateAndReviewApi');
rateAndReviewApi.saveThatHasBeenReviewed("145");
});
Example¶
In this example we make a RateAndReviewApi
call to review the given product.
useStorefront(async (storefront) => {
const rateAndReviewApi = await storefront.getApi('rateAndReviewApi');
await rateAndReviewApi.review({
productId: "145",
review: "I recommend this product!",
reviewer: "Zenek",
challenge: "xYz"
});
});
Example¶
In this example we make a RateAndReviewApi
call to rate and review the given product.
useStorefront(async (storefront) => {
const rateAndReviewApi = await storefront.getApi('rateAndReviewApi');
await rateAndReviewApi.rateAndReview({
productId: "145",
review: "I recommend this product",
reviewer: "Zenek",
rating: 5,
challenge: "xYz"
});
});