Skip to content

Commit

Permalink
dynamic ratings and reviews
Browse files Browse the repository at this point in the history
  • Loading branch information
Aman authored and Aman committed May 8, 2024
1 parent 1254b3f commit 0af86a9
Show file tree
Hide file tree
Showing 5 changed files with 190 additions and 157 deletions.
4 changes: 3 additions & 1 deletion app/search/page.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { appendReviewAndRating } from '@/lib/helper/helper';
import Grid from 'components/grid';
import ProductGridItems from 'components/layout/product-grid-items';
import { defaultSort, sorting } from 'lib/constants';
Expand All @@ -16,7 +17,8 @@ export default async function SearchPage({
const { sort, q: searchValue } = searchParams as { [key: string]: string };
const { sortKey, reverse } = sorting.find((item) => item.slug === sort) || defaultSort;

const products = await getProducts({ sortKey, reverse, query: searchValue });
const _products = await getProducts({ sortKey, reverse, query: searchValue });
const products = await appendReviewAndRating(_products);
const resultsText = products.length > 1 ? 'results' : 'result';

return (
Expand Down
35 changes: 19 additions & 16 deletions components/grid/tile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,27 @@ export function GridTileImage({
</Link>
</div>

<div className="absolute bottom-2 left-2 flex w-max flex-row justify-between gap-1 rounded-sm bg-white px-1 py-[1px] text-black">
<div data-rating="4.8">
<div
className="fera-stars-rating fera-productReviewsSummary-stars-rating"
style={{ width: '96.0%' }}
>
{product.ratings && product.ratings.average !== 0 && (
<div className="absolute bottom-2 left-2 flex w-max flex-row justify-between gap-1 rounded-sm bg-white px-1 py-[1px] text-black">
<div data-rating="4.8">
<div
className="fera-stars-rating fera-productReviewsSummary-stars-rating"
style={{ width: '96.0%' }}
>
</div>
<div />
</div>
<div />

<span
data-value={product?.ratings?.average}
style={{ transformOrigin: '0px 0px', opacity: 1, transform: 'scale(1, 1)' }}
>
{product.ratings.average}
</span>
<span style={{ display: 'none' }}>52</span>
</div>
<span
data-value="4.8"
style={{ transformOrigin: '0px 0px', opacity: 1, transform: 'scale(1, 1)' }}
>
4.8
</span>
<span style={{ display: 'none' }}>52</span>
</div>
)}
</div>
<div className="product-info p-2">
<div className="">
Expand Down
71 changes: 71 additions & 0 deletions lib/helper/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import axios from 'axios';

export function debounce(func, delay) {
let timeoutId;

Expand All @@ -11,3 +13,72 @@ export function debounce(func, delay) {
}, delay);
};
}

export async function appendReviewAndRating(products: any) {
try {
const reviews = await getReviews();
const ratings = await getRatings();

reviews.forEach((review: any) => {
const product = products.find((product: any) => {
const id = getProductId(product.id);
return id === review.external_product_id;
});
if (product) {
product.reviews = review;
}
});

ratings.forEach((rating: any) => {
const product = products.find((product: any) => {
const id = getProductId(product.id);
return id === rating.external_product_id;
});
if (product) {
product.ratings = rating;
}
});

return products;
} catch (error) {
console.log('error', error);
throw error;
}
}

const getProductId = (id: string): string => {
const parts = id.split('/');

// Extracting the part from the last occurrence of slash till the first occurrence
const extractedId = parts.slice(-1)[0];

return extractedId as string;
};

async function getReviews() {
const reviewApiOptions = {
method: 'GET',
url: 'https://api.fera.ai/v3/private/reviews',
headers: {
accept: 'application/json',
'SECRET-KEY': process.env.FERA_FOXTALE_SECRET_KEY
}
};
const response = await axios.request(reviewApiOptions);
const reviews = response.data.data;
return reviews;
}

async function getRatings() {
const ratingApiOptions = {
method: 'GET',
url: 'https://api.fera.ai/v3/private/ratings',
headers: {
accept: 'application/json',
'SECRET-KEY': process.env.FERA_FOXTALE_SECRET_KEY
}
};
const response = await axios.request(ratingApiOptions);
const ratings = response.data.data;
return ratings;
}
Loading

0 comments on commit 0af86a9

Please sign in to comment.