diff --git a/src/views/Product/Rate/index.js b/src/views/Product/Rate/index.js index d193706..e558d0c 100644 --- a/src/views/Product/Rate/index.js +++ b/src/views/Product/Rate/index.js @@ -1,41 +1,107 @@ import React from "react"; -import separate from '../../../utils/separate'; +import separate from "../../../utils/separate"; //components import StarRating from "../../../components/StarRating"; import Progress from "../../../components/Progress"; +function Rate({ desktop, productId, rateDetail }) { + const fiveCount = React.useMemo(() => { + return rateDetail?.find((rate) => rate?.rate === 5); + }, [rateDetail]); + const fourCount = React.useMemo(() => { + return rateDetail?.find((rate) => rate?.rate === 4); + }, [rateDetail]); + + const threeCount = React.useMemo(() => { + return rateDetail?.find((rate) => rate?.rate === 3); + }, [rateDetail]); + + const twoCount = React.useMemo(() => { + return rateDetail?.find((rate) => rate?.rate === 2); + }, [rateDetail]); + + const oneCount = React.useMemo(() => { + return rateDetail?.find((rate) => rate?.rate === 1); + }, [rateDetail]); + + const allCount = React.useMemo(() => { + return rateDetail?.reduce((a, b) => a + b?.count, 0); + }, [rateDetail]); + + const rate = React.useMemo(() => { + return rateDetail?.length + ? ((oneCount?.count || 0) * 1 + + (twoCount?.count || 0) * 2 + + (threeCount?.count || 0) * 3 + + (fourCount?.count || 0) * 4 + + (fiveCount?.count || 0) * 5) / + rateDetail?.length + : 0; + }, [rateDetail]); -function Rate({ avarage, all, fields, desktop }) { return (
- { - desktop ? -

- {avarage} -

- : - - {avarage} - - } + {desktop ? ( +

+ {rate} +

+ ) : ( + + {rate} + + )}
- +
- {separate(all)} + + {" "} + {`از بین ${allCount} نفر`} +
@@ -44,30 +110,3 @@ function Rate({ avarage, all, fields, desktop }) { } export default Rate; - -Rate.defaultProps = { - avarage: 4.8, - all: 4780, - fields: [ - { - name: "5 ستاره", - percent: 49, - }, - { - name: "4 ستاره", - percent: 31, - }, - { - name: "3 ستاره", - percent: 9, - }, - { - name: "2 ستاره", - percent: 5, - }, - { - name: "1 ستاره", - percent: 6, - }, - ], -}; diff --git a/src/views/Product/index.js b/src/views/Product/index.js index 83c2132..2e1f298 100644 --- a/src/views/Product/index.js +++ b/src/views/Product/index.js @@ -149,7 +149,6 @@ export const Product = ({ {videoAvailableForSell ? "ویدئویی - " : ""} {digitalAvailableForSell ? "دیجیتال - " : ""} {physicalAvailableForSell ? "فیزیکی" : ""} -
در دسترس {book?.rate ? ( @@ -317,14 +316,18 @@ export const Product = ({
- +
- {[0, 1, 2, 2].map((comment, index) => ( - + {book?.comment?.map((comment, index) => ( + ))}