Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dev -> main #29

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/app/product/[productId]/review/[reviewId]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default async function Page(props: PageProps) {
});

const customerReviews = reviews.filter((r) => r.email === review.email);
const customerReviewIdStrings = customerReviews.map((r) => `${r.id}`);

let sentimentAnalysis = await db.getReviewAnalysis({
productId,
Expand All @@ -70,6 +71,20 @@ export default async function Page(props: PageProps) {
}
}

const allAnalyses = await db.getAllReviewAnalyses({
storeHash: authorized.storeHash,
});

const allUserAnalyses =
allAnalyses?.filter((analysis) =>
customerReviewIdStrings.includes(analysis.reviewId)
) ?? [];

const userAverageScore = allUserAnalyses.length
? allUserAnalyses.reduce((acc, analysis) => acc + analysis.data.score, 0) /
allUserAnalyses.length
: undefined;

return (
<ReviewDetail
sentimentAnalysis={
Expand All @@ -81,6 +96,7 @@ export default async function Page(props: PageProps) {
customerReviews={customerReviews}
product={product}
review={review}
userAverageScore={userAverageScore}
/>
);
}
147 changes: 0 additions & 147 deletions src/app/productDescription/[productId]/form.tsx

This file was deleted.

33 changes: 0 additions & 33 deletions src/app/productDescription/[productId]/generator.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions src/app/productDescription/[productId]/loading.tsx

This file was deleted.

45 changes: 0 additions & 45 deletions src/app/productDescription/[productId]/page.tsx

This file was deleted.

7 changes: 5 additions & 2 deletions src/app/products/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ export default async function Page() {
throw new Error('Access token not found. Try to re-install the app.');
}

const products = await fetchAllProducts(accessToken, authorized.storeHash);
const [products, allReviews] = await Promise.all([
fetchAllProducts(accessToken, authorized.storeHash),
db.getAllReviewAnalyses({ storeHash: authorized.storeHash }),
]);

return <ProductList products={products} />;
return <ProductList allAnalyses={allReviews ?? []} products={products} />;
}
39 changes: 38 additions & 1 deletion src/components/ProductList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ import { NextLink } from '~/components/NextLink';
import { ProductFilters } from '~/components/ProductFilters';
import { StarRating } from '~/components/StarRating';

import { ScoreCircle } from '~/components/ScoreCircle';
import { type AllReviewAnalysesResponse } from '~/lib/db';
import { convertToUDS } from '~/utils/utils';

interface ProductListProps {
allAnalyses: AllReviewAnalysesResponse;
products: SimpleProduct[];
}

Expand All @@ -38,7 +41,7 @@ const ReviewsAverage = ({ product }: ReviewsAverageProps) => {
return <StarRating rating={average} />;
};

const ProductList = ({ products }: ProductListProps) => {
const ProductList = ({ allAnalyses, products }: ProductListProps) => {
const [filteredProducts, setFilteredProducts] = useState(products);
const sortedProductsByReviews = useMemo(
() =>
Expand All @@ -48,11 +51,35 @@ const ProductList = ({ products }: ProductListProps) => {
[filteredProducts]
);

const productScoresById = products?.reduce(
(acc, { id: productId }) => {
const reviewAnalyses =
allAnalyses?.filter((review) => review.productId === `${productId}`) ??
[];

const productScore = reviewAnalyses?.length
? Math.floor(
reviewAnalyses.reduce(
(acc, analysis) => acc + analysis.data.score,
0
) / reviewAnalyses.length
)
: undefined;

return {
...acc,
[productId]: productScore,
};
},
{} as Record<number, number>
);

return (
<div>
<Breadcrumbs>
<Breadcrumbs.Text>All Products</Breadcrumbs.Text>
</Breadcrumbs>

<div className="my-6">
<ProductFilters
products={products}
Expand Down Expand Up @@ -85,6 +112,16 @@ const ProductList = ({ products }: ProductListProps) => {
hash: 'name',
render: (product) => product.name,
},
{
header: 'Score',
hash: 'score',
render: (product) => (
<ScoreCircle
score={productScoresById[product.id]}
tooltip="Average product sentiment"
/>
),
},
{
header: 'Price',
hash: 'price',
Expand Down
4 changes: 3 additions & 1 deletion src/components/ReviewDetail.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ interface ReviewDetailProps {
product: Product;
review: Review;
sentimentAnalysis?: AnalyzeReviewOutputValues;
userAverageScore?: number;
}

export const ReviewDetail = ({
Expand All @@ -40,6 +41,7 @@ export const ReviewDetail = ({
product,
review: reviewProp,
sentimentAnalysis,
userAverageScore,
}: ReviewDetailProps) => {
const [review, setReview] = useState(reviewProp);

Expand Down Expand Up @@ -125,7 +127,7 @@ export const ReviewDetail = ({
}
topRightContent={
<ScoreCircle
score={92}
score={userAverageScore}
tooltip="Average customer sentiment score"
/>
}
Expand Down
Loading