+
+
نظرات کاربران
- بر اساس ۴ نظر
-
+ بر اساس {{ comments?.count }} نظر
+
-
- نظر بنویسید
-
+
-
\ No newline at end of file
diff --git a/frontend/composables/api/product/useCreateComment.ts b/frontend/composables/api/product/useCreateComment.ts
new file mode 100644
index 0000000..9443850
--- /dev/null
+++ b/frontend/composables/api/product/useCreateComment.ts
@@ -0,0 +1,30 @@
+// imports
+
+import { useMutation } from "@tanstack/vue-query";
+import { API_ENDPOINTS } from "~/constants";
+
+// types
+
+export type CreateCommentRequest = {
+ content: string
+};
+
+const useCreateComment = (id: number | string | undefined) => {
+
+ // state
+
+ const { $axios: axios } = useNuxtApp();
+
+ // method
+
+ const handleCreateComment = async (variables: CreateCommentRequest) => {
+ const { data } = await axios.post(`${API_ENDPOINTS.product.create_comment}/${id}`, variables);
+ return data;
+ };
+
+ return useMutation({
+ mutationFn: (variables: CreateCommentRequest) => handleCreateComment(variables)
+ });
+};
+
+export default useCreateComment;
diff --git a/frontend/composables/api/product/useGetCategories.ts b/frontend/composables/api/product/useGetCategories.ts
index 10b34aa..47f3a2e 100644
--- a/frontend/composables/api/product/useGetCategories.ts
+++ b/frontend/composables/api/product/useGetCategories.ts
@@ -17,7 +17,6 @@ const useGetCategories = () => {
const handleGetCategories = async () => {
const { data } = await axios.get
(`${API_ENDPOINTS.products.categories}`);
- await new Promise(resolve => setTimeout(resolve, 5000));
return data;
};
diff --git a/frontend/composables/api/product/useGetComments.ts b/frontend/composables/api/product/useGetComments.ts
new file mode 100644
index 0000000..ad74c20
--- /dev/null
+++ b/frontend/composables/api/product/useGetComments.ts
@@ -0,0 +1,29 @@
+// imports
+
+import { useQuery } from "@tanstack/vue-query";
+import { API_ENDPOINTS, QUERY_KEYS } from "~/constants";
+
+// types
+
+export type GetCommentsResponse = ApiPaginated;
+
+const useGetComments = (id: string | number | undefined, page: Ref) => {
+
+ // state
+
+ const { $axios: axios } = useNuxtApp();
+
+ // methods
+
+ const handleGetComments = async () => {
+ const { data } = await axios.get(`${API_ENDPOINTS.product.comments}/${id}`);
+ return data;
+ };
+
+ return useQuery({
+ queryKey: [QUERY_KEYS.comments, id, page],
+ queryFn: () => handleGetComments()
+ });
+};
+
+export default useGetComments;
diff --git a/frontend/constants/index.ts b/frontend/constants/index.ts
index 7f1e2c4..bfad022 100644
--- a/frontend/constants/index.ts
+++ b/frontend/constants/index.ts
@@ -5,6 +5,8 @@ export const API_ENDPOINTS = {
send_otp: "/accounts/send_otp"
},
product: {
+ comments: "/products/comments",
+ create_comment: "/products/comments",
get: "/products"
},
auth: {
@@ -24,6 +26,7 @@ export const API_ENDPOINTS = {
};
export const QUERY_KEYS = {
+ comments: "comments",
home: "home",
chat: "chat",
product: "product",
diff --git a/frontend/pages/product/[id].vue b/frontend/pages/product/[id].vue
index 6ac127b..52e32e3 100644
--- a/frontend/pages/product/[id].vue
+++ b/frontend/pages/product/[id].vue
@@ -2,23 +2,27 @@
import ChatButton from "~/components/product/ChatBox/ChatButton.vue";
import useGetProduct from "~/composables/api/product/useGetProduct";
+import useGetComments from "~/composables/api/product/useGetComments";
const route = useRoute();
const id = route.params.id as string | undefined;
+const page = ref(1);
-const { suspense } = useGetProduct(id);
+const { suspense : suspenseProduct } = useGetProduct(id);
+const { suspense : suspenseComments} = useGetComments(id, page);
// ssr
await useAsyncData(async () => {
- const response = await suspense();
+ const productResponse = await suspenseProduct();
+ const commentsResponse = await suspenseComments();
- if (response.isError) {
+ if (productResponse.isError || commentsResponse.isError) {
throw createError({
statusCode: 404,
- statusMessage: `error : ${response.error.message}`,
- })
+ statusMessage: `error : product ${id} prefetch error`
+ });
}
});
@@ -30,7 +34,7 @@ await useAsyncData(async () => {
-
+
diff --git a/frontend/plugins/axios.ts b/frontend/plugins/axios.ts
index 04e524b..296a285 100644
--- a/frontend/plugins/axios.ts
+++ b/frontend/plugins/axios.ts
@@ -16,7 +16,7 @@ export default defineNuxtPlugin(() => {
!config.url?.includes(API_ENDPOINTS.auth.signin) &&
!config.url?.includes(API_ENDPOINTS.account.send_otp)
) {
- config.headers.Authorization = `Bearer ${token.value}`;
+ config.headers.Authorization = token.value ? `Bearer ${token.value}` : undefined;
}
return config;
diff --git a/frontend/types/global.d.ts b/frontend/types/global.d.ts
index ee75637..e28b8ce 100644
--- a/frontend/types/global.d.ts
+++ b/frontend/types/global.d.ts
@@ -43,6 +43,15 @@ declare global {
meta_rating: number | null;
};
+ type UserComment = {
+ "id": number,
+ "content": string,
+ "timestamp": string,
+ "show": boolean,
+ "product": number,
+ "user": number
+ }
+
type Category = {
id: number;
name: string;
@@ -57,7 +66,7 @@ declare global {
"name": string,
"slug": string,
"icon": string,
- "image" : string,
+ "image": string,
"product_count": string,
"parent": string,
"show": boolean