diff --git a/frontend/components/global/product/Comment.vue b/frontend/components/global/product/Comment.vue
index 23f86e9..70b0816 100644
--- a/frontend/components/global/product/Comment.vue
+++ b/frontend/components/global/product/Comment.vue
@@ -7,6 +7,7 @@ type Props = {
date: string,
username: string,
content: string,
+ rate : number
}
// props
@@ -25,7 +26,7 @@ const formattedDate = useDateFormat(date.value, "YYYY/MM/DD");
- خیلی محصول خوبی بودددد
+ {{ title }}
{{ username }}
@@ -33,7 +34,7 @@ const formattedDate = useDateFormat(date.value, "YYYY/MM/DD");
{{ formattedDate }}
-
+
{{ content }}
diff --git a/frontend/components/product/ProductComments.vue b/frontend/components/product/ProductComments.vue
index cc321d5..d6ff612 100644
--- a/frontend/components/product/ProductComments.vue
+++ b/frontend/components/product/ProductComments.vue
@@ -3,8 +3,17 @@
import useGetComments from "~/composables/api/product/useGetComments";
import useCreateComment from "~/composables/api/product/useCreateComment";
+import useRateProduct from "~/composables/api/product/useRateProduct";
import { useAuth } from "~/composables/api/auth/useAuth";
+// props
+
+type Props = {
+ product: Product;
+};
+
+const props = defineProps
();
+
// state
const route = useRoute();
@@ -13,25 +22,66 @@ const id = route.params.id as string | undefined;
const page = ref(1);
const { token } = useAuth();
+const userTitle = ref("");
const userComment = ref("");
+const selectedRating = ref(5);
const showMoreComments = ref(false);
const { data: comments, refetch: refetchComments } = useGetComments(id, page);
const { mutateAsync: createComment, isPending: isCreateCommentPending } = useCreateComment(id);
+const { mutateAsync: rateProduct, isPending: isRateProductPending } = useRateProduct(props.product.slug);
+
+watch(
+ () => props.product.user_rating,
+ (newUserRating) => {
+ if (token.value && newUserRating !== null) {
+ selectedRating.value = newUserRating;
+ } else {
+ selectedRating.value = 5;
+ }
+ },
+ { immediate: true }
+);
+
+const canSubmitComment = computed(() => {
+ return userTitle.value.trim().length > 0 && userComment.value.trim().length > 0;
+});
+
+const hasUserRated = computed(() => {
+ return token.value && props.product.user_rating !== null;
+});
// methods
const submitComment = async () => {
- if (userComment.value.length > 3) {
- await createComment({
- content: userComment.value,
- });
-
- userComment.value = "";
-
- await refetchComments();
+ if (!canSubmitComment.value) {
+ return;
}
+
+ const promises: Promise[] = [
+ createComment({
+ title: userTitle.value,
+ content: userComment.value,
+ }),
+ ];
+
+ // Only submit rating if user hasn't rated before
+ if (!hasUserRated.value) {
+ promises.push(
+ rateProduct({
+ rating: selectedRating.value,
+ })
+ );
+ }
+
+ await Promise.all(promises);
+
+ userTitle.value = "";
+ userComment.value = "";
+ selectedRating.value = 5;
+
+ await refetchComments();
};
// computed
@@ -52,20 +102,69 @@ const limitedComments = computed(() => {
>
diff --git a/frontend/components/product/ProductDetails.vue b/frontend/components/product/ProductDetails.vue
index cf51ab0..3490b19 100644
--- a/frontend/components/product/ProductDetails.vue
+++ b/frontend/components/product/ProductDetails.vue
@@ -9,7 +9,7 @@ const { selectedVariant } = inject("productVariant") as ProductVariantProvideTyp
-
+
جزئیات محصول
diff --git a/frontend/components/product/ProductHero/index.vue b/frontend/components/product/ProductHero/index.vue
index 573fb86..341d775 100644
--- a/frontend/components/product/ProductHero/index.vue
+++ b/frontend/components/product/ProductHero/index.vue
@@ -61,7 +61,7 @@ watch(
},
{
immediate: true,
- }
+ },
);
watch(
@@ -72,7 +72,7 @@ watch(
},
{
immediate: true,
- }
+ },
);
@@ -136,7 +136,7 @@ watch(
تخفیف درصد
-
+