-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from ildecimo/develop
merge dev
- Loading branch information
Showing
14 changed files
with
868 additions
and
17 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module.exports = { | ||
plugins: { | ||
tailwindcss: {}, | ||
autoprefixer: {}, | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,48 @@ | ||
import { authorize } from '~/lib/authorize'; | ||
import * as db from '~/lib/db'; | ||
|
||
import { fetchProductWithAttributes, fetchReviews } from '~/server/bigcommerce-api'; | ||
|
||
import ProductReviewList from '~/components/ProductReviewList'; | ||
|
||
interface PageProps { | ||
params: { productId: string }; | ||
searchParams: { product_name: string }; | ||
} | ||
|
||
export default function Page(props: PageProps) { | ||
console.log('props', props); | ||
export default async function Page(props: PageProps) { | ||
const { productId } = props.params; | ||
|
||
const authorized = authorize(); | ||
|
||
if (!authorized) { | ||
throw new Error('Token is not valid. Try to re-open the app.'); | ||
} | ||
|
||
const accessToken = await db.getStoreToken(authorized.storeHash); | ||
|
||
if (!accessToken) { | ||
throw new Error('Access token not found. Try to re-install the app.'); | ||
} | ||
|
||
const id = Number(productId); | ||
|
||
const product = await fetchProductWithAttributes( | ||
id, | ||
accessToken, | ||
authorized.storeHash | ||
); | ||
|
||
const reviews = await fetchReviews( | ||
id, | ||
accessToken, | ||
authorized.storeHash | ||
); | ||
|
||
console.log(reviews); | ||
|
||
return ( | ||
<div> | ||
<h1>Product Review Page from the APP</h1> | ||
<ProductReviewList productName={product.name} reviews={reviews} /> | ||
</div> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
'use client'; | ||
import { useMemo } from "react"; | ||
import { H1, Table, Badge, Link, Box } from '@bigcommerce/big-design'; | ||
import { | ||
StarBorderIcon, | ||
StarHalfIcon, | ||
StarIcon, | ||
} from '@bigcommerce/big-design-icons'; | ||
import { type Review } from 'types'; | ||
import { convertToDateString } from '~/utils/utils'; | ||
|
||
interface ProductReviewListProps { | ||
productName: string; | ||
reviews: Review[]; | ||
} | ||
|
||
interface StatusBadgeProps { | ||
status: 'approved' | 'pending' | 'disapproved'; | ||
} | ||
|
||
interface ReviewRatingProps { | ||
rating: number; | ||
} | ||
|
||
const StatusBadge = ({ status }: StatusBadgeProps) => { | ||
const variant = | ||
status === 'approved' | ||
? 'success' | ||
: status === 'pending' | ||
? 'warning' | ||
: 'danger'; | ||
const label = status.charAt(0).toUpperCase() + status.slice(1); | ||
|
||
return ( | ||
<Badge label={label} variant={variant}> | ||
{status} | ||
</Badge> | ||
); | ||
}; | ||
|
||
const ReviewRating = ({ rating }: ReviewRatingProps) => { | ||
const stars: React.JSX.Element[] = []; | ||
const fullStars = Math.floor(rating); | ||
const halfStars = Math.ceil(rating % 1); | ||
const emptyStars = 5 - fullStars - halfStars; | ||
|
||
for (let i = 0; i < fullStars; i++) { | ||
stars.push(<StarIcon color="warning50" size={'medium'} key={i} />); | ||
} | ||
|
||
if (halfStars) { | ||
stars.push( | ||
<StarHalfIcon color="warning50" size={'medium'} key={fullStars} /> | ||
); | ||
} | ||
|
||
for (let i = 0; i < emptyStars; i++) { | ||
stars.push( | ||
<StarBorderIcon size={'medium'} key={fullStars + halfStars + i} /> | ||
); | ||
} | ||
|
||
return <div className="flex">{stars}</div>; | ||
}; | ||
|
||
const ProductReviewList = ({ | ||
productName, | ||
reviews, | ||
}: ProductReviewListProps) => { | ||
|
||
const averageRating = useMemo(() => { | ||
return reviews.reduce((acc, review) => acc + review.rating, 0) / reviews.length; | ||
} | ||
, [reviews]); | ||
|
||
const approvedReviewsCount = useMemo(() => { | ||
return reviews.filter(review => review.status === 'approved').length; | ||
} | ||
, [reviews]); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
<div className="text-center"> | ||
<H1>Reviews for - <strong>{productName}</strong></H1> | ||
</div> | ||
<div className="my-12"> | ||
<Box border="box" padding="small" borderRadius="normal"> | ||
<div> | ||
<strong>Reviews count:</strong><span className="pl-2">{reviews.length}</span> | ||
</div> | ||
<div> | ||
<strong>Approved:</strong><span className="pl-2">{approvedReviewsCount}</span> | ||
</div> | ||
<div className="flex"> | ||
<strong>Average Rating:</strong><span className="pl-2"><ReviewRating rating={averageRating} /></span> | ||
</div> | ||
</Box> | ||
</div> | ||
<Table | ||
columns={[ | ||
{ | ||
header: 'Rating', | ||
hash: 'rating', | ||
render: ({ rating }) => <ReviewRating rating={rating} />, | ||
}, | ||
{ | ||
header: 'Status', | ||
hash: 'status', | ||
render: ({ status }) => <StatusBadge status={status} />, | ||
}, | ||
{ | ||
header: 'Posted by', | ||
hash: 'name', | ||
render: ({ name, email }) => ( | ||
<span> | ||
{name} | ||
<br /> | ||
<small>{email}</small> | ||
</span> | ||
), | ||
}, | ||
{ | ||
header: 'Date', | ||
hash: 'date', | ||
render: ({ date_created }) => ( | ||
<span>{convertToDateString(date_created)}</span> | ||
), | ||
}, | ||
{ | ||
header: 'Action', | ||
hash: 'action', | ||
render: ({id}) => <Link href={`/review/${id}`}>AI Explore</Link>, | ||
} | ||
]} | ||
items={reviews} | ||
stickyHeader | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default ProductReviewList; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
@tailwind base; | ||
@tailwind components; | ||
@tailwind utilities; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
/** @type {import('tailwindcss').Config} */ | ||
module.exports = { | ||
content: ['./src/**/*.{ts,tsx}'], | ||
theme: { | ||
extend: {}, | ||
}, | ||
plugins: [], | ||
}; |
Oops, something went wrong.