connected api with profile, order
This commit is contained in:
@@ -1,14 +1,14 @@
|
||||
export * from "./useProducts"
|
||||
export * from "./useCategories"
|
||||
export * from "./useCart"
|
||||
export * from "./useFavorites"
|
||||
export * from "./useOrders"
|
||||
export * from "../../features/products/hooks/useProducts"
|
||||
export * from "../../features/category/hooks/useCategories"
|
||||
export * from "../../features/cart/hooks/useCart"
|
||||
export * from "../../features/favorites/hooks/useFavorites"
|
||||
export * from "../../features/orders/hooks/useOrders"
|
||||
export * from "./useSearch"
|
||||
export * from "./useUserProfile"
|
||||
export * from "../../features/profile/hooks/useUserProfile"
|
||||
export * from "./useOpenStore"
|
||||
export * from "./useRegions"
|
||||
export * from "./useAddresses"
|
||||
export * from "./usePaymentTypes"
|
||||
|
||||
export * from "../../features/cart/hooks/useAddresses"
|
||||
export * from "../../features/cart/hooks/usePaymentTypes"
|
||||
|
||||
|
||||
|
||||
@@ -16,8 +16,8 @@ export * from "./usePaymentTypes"
|
||||
|
||||
|
||||
|
||||
export * from "./useMedia"
|
||||
export * from "./useCollections"
|
||||
export * from "../../features/home/hooks/useMedia"
|
||||
export * from "../../features/home/hooks/useCollections"
|
||||
|
||||
// Export types
|
||||
export type { Product, Category, Cart, CartItem, Order, Favorite, Banner } from "@/lib/types/api"
|
||||
|
||||
@@ -1,192 +1,160 @@
|
||||
import { useMutation, useQuery } from "@tanstack/react-query"
|
||||
import { apiClient, setAuthToken, clearAuthToken, setGuestToken } from "@/lib/api"
|
||||
import { queryClient } from "@/lib/queryClient"
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { useState, useEffect } from "react";
|
||||
import { apiClient, setAuthToken, clearAuthToken, setGuestToken } from "@/lib/api";
|
||||
|
||||
// ==================== TYPES ====================
|
||||
interface LoginCredentials {
|
||||
phone_number: string
|
||||
password?: string
|
||||
phone_number: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
interface RegisterData {
|
||||
phone_number: string
|
||||
name?: string
|
||||
email?: string
|
||||
phone_number: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
}
|
||||
|
||||
interface VerifyTokenData {
|
||||
phone_number: string
|
||||
code: string
|
||||
phone_number: string;
|
||||
code: string;
|
||||
}
|
||||
|
||||
interface AuthResponse {
|
||||
token?: string
|
||||
data?: string
|
||||
user?: any
|
||||
token?: string;
|
||||
data?: string;
|
||||
user?: {
|
||||
id: string;
|
||||
phone_number: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Guest Token alma (RTK mantığı)
|
||||
*/
|
||||
// ==================== AUTH STATUS ====================
|
||||
const getTokenFromCookie = (name: string): string | null => {
|
||||
if (typeof document === "undefined") return null;
|
||||
const value = `; ${document.cookie}`;
|
||||
const parts = value.split(`; ${name}=`);
|
||||
if (parts.length === 2) return parts.pop()?.split(";").shift() || null;
|
||||
return null;
|
||||
};
|
||||
|
||||
export function useAuthStatus() {
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [isAuthenticated, setIsAuthenticated] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
const authToken = getTokenFromCookie("authToken");
|
||||
setIsAuthenticated(!!authToken);
|
||||
setIsLoading(false);
|
||||
}, []);
|
||||
|
||||
return {
|
||||
isAuthenticated,
|
||||
isLoading,
|
||||
};
|
||||
}
|
||||
|
||||
// ==================== GUEST TOKEN ====================
|
||||
export function useGetGuestToken() {
|
||||
return useMutation({
|
||||
mutationFn: async (): Promise<AuthResponse> => {
|
||||
const response = await apiClient.post<AuthResponse>("/auth/guest-token", {}, {
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
})
|
||||
return response.data
|
||||
const response = await apiClient.post<AuthResponse>("/auth/guest-token", {});
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
const token = data?.token || data?.data
|
||||
const token = data?.token || data?.data;
|
||||
if (token) {
|
||||
setGuestToken(token)
|
||||
setGuestToken(token);
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Error fetching guest token:", error)
|
||||
console.error("Guest token hatası:", error);
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Login mutation (RTK mantığı)
|
||||
*/
|
||||
// ==================== LOGIN ====================
|
||||
export function useLogin() {
|
||||
return useMutation({
|
||||
mutationFn: async (credentials: LoginCredentials): Promise<AuthResponse> => {
|
||||
const response = await apiClient.post<AuthResponse>("/auth/login", credentials)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
const token = data?.token || data?.data
|
||||
if (token) {
|
||||
setAuthToken(token)
|
||||
apiClient.setAuthToken(token)
|
||||
|
||||
// Tüm cache'i temizle ve yeniden fetch et
|
||||
queryClient.invalidateQueries()
|
||||
}
|
||||
const response = await apiClient.post<AuthResponse>("/auth/login", credentials);
|
||||
return response.data;
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Login error:", error)
|
||||
console.error("Login hatası:", error);
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Register mutation (RTK mantığı)
|
||||
*/
|
||||
// ==================== REGISTER ====================
|
||||
export function useRegister() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (userData: RegisterData): Promise<AuthResponse> => {
|
||||
const response = await apiClient.post<AuthResponse>("/auth/register", userData)
|
||||
return response.data
|
||||
const response = await apiClient.post<AuthResponse>("/auth/register", userData);
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
const token = data?.token || data?.data
|
||||
const token = data?.token || data?.data;
|
||||
if (token) {
|
||||
setAuthToken(token)
|
||||
apiClient.setAuthToken(token)
|
||||
|
||||
// Tüm cache'i temizle
|
||||
queryClient.invalidateQueries()
|
||||
setAuthToken(token);
|
||||
queryClient.invalidateQueries({ queryKey: ["auth-status"] });
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Register error:", error)
|
||||
console.error("Register hatası:", error);
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Token doğrulama (RTK mantığı)
|
||||
*/
|
||||
// ==================== VERIFY TOKEN ====================
|
||||
export function useVerifyToken() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (verifyData: VerifyTokenData): Promise<AuthResponse> => {
|
||||
const response = await apiClient.post<AuthResponse>(
|
||||
"/auth/verify",
|
||||
verifyData,
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
Accept: "application/json",
|
||||
},
|
||||
}
|
||||
)
|
||||
return response.data
|
||||
const response = await apiClient.post<AuthResponse>("/auth/verify", verifyData);
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (data) => {
|
||||
const token = data?.data || data?.token
|
||||
const token = data?.data || data?.token;
|
||||
if (token) {
|
||||
setAuthToken(token)
|
||||
apiClient.setAuthToken(token)
|
||||
|
||||
// Tüm cache'i temizle
|
||||
queryClient.invalidateQueries()
|
||||
setAuthToken(token);
|
||||
queryClient.invalidateQueries({ queryKey: ["auth-status"] });
|
||||
}
|
||||
},
|
||||
onError: (error) => {
|
||||
console.error("Error verifying token:", error)
|
||||
console.error("Verify hatası:", error);
|
||||
},
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Logout işlemi
|
||||
*/
|
||||
// ==================== LOGOUT ====================
|
||||
export function useLogout() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
// Backend'e logout isteği gönder (eğer endpoint varsa)
|
||||
mutationFn: async (): Promise<void> => {
|
||||
try {
|
||||
await apiClient.post("/auth/logout")
|
||||
await apiClient.post("/auth/logout");
|
||||
} catch (error) {
|
||||
// Logout endpoint yoksa da devam et
|
||||
console.warn("Logout endpoint not available")
|
||||
console.warn("Logout endpoint çalışmadı:", error);
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
clearAuthToken()
|
||||
apiClient.clearAuthToken()
|
||||
|
||||
// Tüm cache'i temizle
|
||||
queryClient.clear()
|
||||
|
||||
// Login sayfasına yönlendir
|
||||
clearAuthToken();
|
||||
queryClient.clear();
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.location.href = "/login"
|
||||
window.location.href = "/login";
|
||||
}
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Kullanıcı bilgilerini getir
|
||||
*/
|
||||
export function useUser(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["user", "me"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/auth/me")
|
||||
return response.data
|
||||
onError: (error) => {
|
||||
console.error("Logout hatası:", error);
|
||||
clearAuthToken();
|
||||
queryClient.clear();
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 5, // 5 dakika
|
||||
retry: false,
|
||||
})
|
||||
}
|
||||
|
||||
/**
|
||||
* Authentication durumunu kontrol et
|
||||
*/
|
||||
export function useAuthStatus() {
|
||||
const { data: user, isLoading, error } = useUser({ enabled: true })
|
||||
|
||||
return {
|
||||
isAuthenticated: !!user && !error,
|
||||
isLoading,
|
||||
user,
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -1,270 +0,0 @@
|
||||
import { useQuery, useMutation, useQueryClient, UseQueryOptions } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { Cart, CartItem } from "@/lib/types/api"
|
||||
|
||||
interface CartResponse {
|
||||
message: string
|
||||
data: CartItem[]
|
||||
errorDetails?: string
|
||||
}
|
||||
|
||||
// Transform response to handle HTML/malformed responses
|
||||
function transformCartResponse(response: any): CartResponse {
|
||||
if (
|
||||
typeof response === "string" &&
|
||||
(response.trim().startsWith("<!DOCTYPE") || response.trim().startsWith("<html"))
|
||||
) {
|
||||
console.error("Received HTML response instead of JSON:", response.substring(0, 100))
|
||||
return {
|
||||
message: "error",
|
||||
data: [],
|
||||
errorDetails: "Server returned HTML instead of JSON. The server might be down or experiencing issues.",
|
||||
}
|
||||
}
|
||||
|
||||
if (typeof response === "object") {
|
||||
if (response.data) {
|
||||
return response
|
||||
}
|
||||
return { message: "success", data: [] }
|
||||
}
|
||||
|
||||
if (typeof response === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response)
|
||||
return parsed
|
||||
} catch (error) {
|
||||
console.error("Failed to parse response:", error)
|
||||
return { message: "error", data: [] }
|
||||
}
|
||||
}
|
||||
|
||||
return { message: "unknown", data: [] }
|
||||
}
|
||||
|
||||
export function useCart(options?: Partial<UseQueryOptions<CartResponse>>) {
|
||||
return useQuery({
|
||||
queryKey: ["cart"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/carts")
|
||||
return transformCartResponse(response.data)
|
||||
},
|
||||
refetchInterval: 5000, // Poll every 5 seconds like RTK
|
||||
refetchOnMount: true,
|
||||
refetchOnWindowFocus: false,
|
||||
refetchOnReconnect: true,
|
||||
staleTime: 0,
|
||||
retry: 1,
|
||||
...options,
|
||||
})
|
||||
}
|
||||
|
||||
export function useAddToCart() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ productId, quantity = 1 }: { productId: number; quantity?: number }) => {
|
||||
const params = new URLSearchParams({
|
||||
product_id: String(productId),
|
||||
product_quantity: String(quantity),
|
||||
})
|
||||
|
||||
const response = await apiClient.post("/carts", params.toString(), {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
})
|
||||
|
||||
if (typeof response.data === "object" && response.data.data) {
|
||||
return response.data
|
||||
}
|
||||
|
||||
if (typeof response.data === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response.data)
|
||||
return parsed
|
||||
} catch (error) {
|
||||
console.error("Failed to parse add to cart response:", error)
|
||||
return { message: "success", data: "Added to cart" }
|
||||
}
|
||||
}
|
||||
|
||||
return { message: "success", data: "Added to cart" }
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error("Add to cart error:", error.response?.data?.message || error.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useRemoveFromCart() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (productId: number) => {
|
||||
const params = new URLSearchParams({ product_id: String(productId) })
|
||||
|
||||
const response = await apiClient.patch("/carts", params.toString(), {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
})
|
||||
|
||||
if (typeof response.data === "object" && response.data.data) {
|
||||
return response.data.data
|
||||
}
|
||||
|
||||
if (typeof response.data === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response.data)
|
||||
return parsed.data || []
|
||||
} catch (error) {
|
||||
console.error("Failed to parse cart response:", error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
return []
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error("Remove from cart error:", error.response?.data?.message || error.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useCleanCart() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async () => {
|
||||
const response = await apiClient.delete("/carts", {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
})
|
||||
|
||||
if (typeof response.data === "object" && response.data.data) {
|
||||
return response.data.data
|
||||
}
|
||||
|
||||
if (typeof response.data === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response.data)
|
||||
return parsed.data || []
|
||||
} catch (error) {
|
||||
console.error("Failed to parse cart response:", error)
|
||||
return []
|
||||
}
|
||||
}
|
||||
|
||||
return []
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] })
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useUpdateCartItemQuantity() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({ productId, quantity }: { productId: number; quantity: number }) => {
|
||||
const params = new URLSearchParams({
|
||||
product_id: String(productId),
|
||||
product_quantity: String(quantity),
|
||||
})
|
||||
|
||||
const response = await apiClient.post("/carts", params.toString(), {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
})
|
||||
|
||||
if (typeof response.data === "object" && response.data.data) {
|
||||
return response.data
|
||||
}
|
||||
|
||||
if (typeof response.data === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response.data)
|
||||
return parsed
|
||||
} catch (error) {
|
||||
console.error("Failed to parse update cart response:", error)
|
||||
return { message: "success", data: "Updated cart" }
|
||||
}
|
||||
}
|
||||
|
||||
return { message: "success", data: "Updated cart" }
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error("API update failed:", error.response?.data?.message || error.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
export function useCreateOrder() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (payload: {
|
||||
customer_name?: string
|
||||
customer_phone?: string
|
||||
customer_address: string
|
||||
shipping_method: string
|
||||
payment_type_id: number
|
||||
delivery_time?: string
|
||||
delivery_at?: string
|
||||
region: string
|
||||
note?: string
|
||||
}) => {
|
||||
const response = await apiClient.post("/api/v1/orders", payload)
|
||||
return response.data
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] })
|
||||
queryClient.invalidateQueries({ queryKey: ["orders"] })
|
||||
},
|
||||
onError: (error: any) => {
|
||||
console.error("Create order error:", error.response?.data?.message || error.message)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
import type { Region } from "@/lib/types/api"
|
||||
|
||||
export function useRegions() {
|
||||
return useQuery({
|
||||
queryKey: ["regions"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Region[]>("/api/v1/provinces")
|
||||
return response.data
|
||||
},
|
||||
staleTime: 1000 * 60 * 60, // 1 hour
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
import type { Address } from "@/lib/types/api"
|
||||
|
||||
export function useAddresses() {
|
||||
return useQuery({
|
||||
queryKey: ["addresses"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Address[]>("/api/v1/addresses")
|
||||
return response.data
|
||||
},
|
||||
staleTime: 1000 * 60 * 30, // 30 minutes
|
||||
})
|
||||
}
|
||||
@@ -1,161 +0,0 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { Category, Product, PaginatedResponse } from "@/lib/types/api"
|
||||
|
||||
// Get all categories as tree
|
||||
export function useCategories(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["categories"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Category>>("/categories", {
|
||||
params: { type: "tree" },
|
||||
})
|
||||
return response.data.data || response.data
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 30, // 30 minutes
|
||||
})
|
||||
}
|
||||
|
||||
// Get single category by ID
|
||||
export function useCategory(id: number | string, options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["category", id],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Category>(`/categories/${id}`)
|
||||
return response.data
|
||||
},
|
||||
enabled: options?.enabled !== false && !!id,
|
||||
staleTime: 1000 * 60 * 15,
|
||||
})
|
||||
}
|
||||
|
||||
// Get products for a single category with pagination
|
||||
export function useCategoryProducts(
|
||||
categoryId: number | string,
|
||||
options?: {
|
||||
enabled?: boolean
|
||||
page?: number
|
||||
limit?: number
|
||||
}
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["category", categoryId, "products", options?.page, options?.limit],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/categories/${categoryId}/products`,
|
||||
{
|
||||
params: {
|
||||
page: options?.page || 1,
|
||||
limit: options?.limit
|
||||
},
|
||||
}
|
||||
)
|
||||
return {
|
||||
data: response.data.data || [],
|
||||
pagination: response.data.pagination || {}
|
||||
}
|
||||
},
|
||||
enabled: options?.enabled !== false && !!categoryId,
|
||||
})
|
||||
}
|
||||
|
||||
// Get ALL products from category and its children - NO pagination (for initial load)
|
||||
export function useAllCategoryProducts(
|
||||
category: Category | undefined,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["category", category?.id, "all-products"],
|
||||
queryFn: async () => {
|
||||
if (!category) return []
|
||||
|
||||
const fetchProducts = async (categoryId: number) => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/categories/${categoryId}/products`
|
||||
)
|
||||
return response.data.data || []
|
||||
}
|
||||
|
||||
let allProducts = await fetchProducts(category.id)
|
||||
|
||||
if (category.children && category.children.length > 0) {
|
||||
for (const child of category.children) {
|
||||
const childProducts = await fetchProducts(child.id)
|
||||
allProducts = [...allProducts, ...childProducts]
|
||||
}
|
||||
}
|
||||
|
||||
return allProducts
|
||||
},
|
||||
enabled: options?.enabled !== false && !!category,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
|
||||
// Get products from category and children WITH pagination (mimics RTK getAllCategoryProductsPaginated)
|
||||
export function useAllCategoryProductsPaginated(
|
||||
category: Category | undefined,
|
||||
options?: {
|
||||
enabled?: boolean
|
||||
page?: number
|
||||
limit?: number
|
||||
}
|
||||
) {
|
||||
const page = options?.page || 1
|
||||
const limit = options?.limit || 6
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["category", category?.id, "paginated-products", page, limit],
|
||||
queryFn: async () => {
|
||||
if (!category) {
|
||||
return {
|
||||
data: [],
|
||||
pagination: {
|
||||
currentPage: page,
|
||||
hasMorePages: false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const categoryIds = [category.id]
|
||||
if (category.children && category.children.length > 0) {
|
||||
category.children.forEach((child) => categoryIds.push(child.id))
|
||||
}
|
||||
|
||||
const perCategoryLimit = Math.ceil(limit / categoryIds.length)
|
||||
const hasMoreByCategory: Record<number, boolean> = {}
|
||||
let allPageProducts: Product[] = []
|
||||
|
||||
for (const categoryId of categoryIds) {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/categories/${categoryId}/products`,
|
||||
{
|
||||
params: {
|
||||
page,
|
||||
limit: perCategoryLimit
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
if (response.data.data) {
|
||||
allPageProducts = [...allPageProducts, ...response.data.data]
|
||||
hasMoreByCategory[categoryId] = !!response.data.pagination?.next_page_url
|
||||
}
|
||||
}
|
||||
|
||||
const hasMorePages = Object.values(hasMoreByCategory).some((hasMore) => hasMore)
|
||||
|
||||
return {
|
||||
data: allPageProducts,
|
||||
pagination: {
|
||||
currentPage: page,
|
||||
hasMorePages
|
||||
}
|
||||
}
|
||||
},
|
||||
enabled: options?.enabled !== false && !!category,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -1,108 +0,0 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { Collection, Product, PaginatedResponse } from "@/lib/types/api"
|
||||
|
||||
// Get all collections
|
||||
export function useCollections(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["collections"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Collection>>("/collections")
|
||||
return response.data.data || response.data
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 30, // 30 minutes
|
||||
})
|
||||
}
|
||||
|
||||
// Get single collection by ID
|
||||
export function useCollection(id: number | string, options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["collection", id],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Collection>(`/collections/${id}`)
|
||||
return response.data
|
||||
},
|
||||
enabled: options?.enabled !== false && !!id,
|
||||
staleTime: 1000 * 60 * 15,
|
||||
})
|
||||
}
|
||||
|
||||
// Get collection products (non-paginated)
|
||||
export function useCollectionProducts(
|
||||
collectionId: number | string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["collection", collectionId, "products"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/collections/${collectionId}/products`
|
||||
)
|
||||
const data = response.data.data || []
|
||||
return {
|
||||
data,
|
||||
isEmpty: data.length === 0,
|
||||
}
|
||||
},
|
||||
enabled: options?.enabled !== false && !!collectionId,
|
||||
})
|
||||
}
|
||||
|
||||
// Check if collection has products (limit=1 for efficiency)
|
||||
export function useCollectionHasProducts(
|
||||
collectionId: number | string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["collection", collectionId, "has-products"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/collections/${collectionId}/products`,
|
||||
{
|
||||
params: { limit: 1 },
|
||||
}
|
||||
)
|
||||
return {
|
||||
hasProducts: response.data.data && response.data.data.length > 0,
|
||||
}
|
||||
},
|
||||
enabled: options?.enabled !== false && !!collectionId,
|
||||
staleTime: 1000 * 60 * 5, // 5 minutes
|
||||
})
|
||||
}
|
||||
|
||||
// Get collection products with pagination
|
||||
export function useCollectionProductsPaginated(
|
||||
collectionId: number | string,
|
||||
options?: {
|
||||
enabled?: boolean
|
||||
page?: number
|
||||
limit?: number
|
||||
}
|
||||
) {
|
||||
const page = options?.page || 1
|
||||
const limit = options?.limit || 6
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["collection", collectionId, "products-paginated", page, limit],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
`/collections/${collectionId}/products`,
|
||||
{
|
||||
params: {
|
||||
page,
|
||||
limit,
|
||||
},
|
||||
}
|
||||
)
|
||||
const data = response.data.data || []
|
||||
return {
|
||||
data,
|
||||
pagination: response.data.pagination || {},
|
||||
isEmpty: data.length === 0,
|
||||
}
|
||||
},
|
||||
enabled: options?.enabled !== false && !!collectionId,
|
||||
})
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { apiClient } from "@/lib/api";
|
||||
import type { Favorite } from "@/lib/types/api";
|
||||
|
||||
// Response tiplerini tanımlayalım
|
||||
interface FavoritesResponse {
|
||||
data?: Favorite[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface FavoriteActionResponse {
|
||||
data?: string | Favorite[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// Response'u transform eden yardımcı fonksiyon
|
||||
function transformFavoritesResponse(response: any): Favorite[] {
|
||||
if (typeof response === "object" && response.data) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
if (typeof response === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response);
|
||||
return parsed.data || [];
|
||||
} catch (error) {
|
||||
console.error("Failed to parse favorites response:", error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
function transformActionResponse(response: any, defaultValue: string): string {
|
||||
if (typeof response === "object" && response.data) {
|
||||
return response.data;
|
||||
}
|
||||
|
||||
if (typeof response === "string") {
|
||||
try {
|
||||
const parsed = JSON.parse(response);
|
||||
return parsed.data || defaultValue;
|
||||
} catch (error) {
|
||||
if (response.includes("<!doctype html>")) {
|
||||
return defaultValue;
|
||||
}
|
||||
console.error(`Failed to parse favorite response:`, error);
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
export function useFavorites() {
|
||||
return useQuery({
|
||||
queryKey: ["favorites"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/favorites");
|
||||
return transformFavoritesResponse(response.data);
|
||||
},
|
||||
staleTime: 1000 * 60 * 5,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
export function useAddToFavorites() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (productId: number) => {
|
||||
const formData = new URLSearchParams({
|
||||
product_id: productId.toString(),
|
||||
});
|
||||
|
||||
const response = await apiClient.post("/favorites", formData, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
|
||||
return transformActionResponse(response.data, "Added");
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["favorites"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function useRemoveFromFavorites() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (productId: number) => {
|
||||
const formData = new URLSearchParams({
|
||||
product_id: productId.toString(),
|
||||
});
|
||||
|
||||
const response = await apiClient.post("/favorites", formData, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
});
|
||||
|
||||
return transformActionResponse(response.data, "Removed");
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["favorites"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { Carousel, Banner, PaginatedResponse } from "@/lib/types/api"
|
||||
|
||||
// Get all carousels
|
||||
export function useCarousels(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["carousels"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Carousel>>("/media/carousels")
|
||||
return response.data.data || response.data
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 30, // 30 minutes
|
||||
})
|
||||
}
|
||||
|
||||
// Get all banners
|
||||
export function useBanners(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["banners"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Banner>>("/media/banners")
|
||||
return response.data.data || response.data
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 30, // 30 minutes
|
||||
})
|
||||
}
|
||||
@@ -1,38 +1,38 @@
|
||||
"use client"
|
||||
// "use client"
|
||||
|
||||
import { useMutation } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import { API_ENDPOINTS } from "@/lib/config/api-endpoints"
|
||||
// import { useMutation } from "@tanstack/react-query"
|
||||
// import { apiClient } from "@/lib/api"
|
||||
// import { API_ENDPOINTS } from "@/lib/config/api-endpoints"
|
||||
|
||||
interface OpenStoreData {
|
||||
firstName: string
|
||||
lastName: string
|
||||
email: string
|
||||
phone: string
|
||||
patentFile: File
|
||||
}
|
||||
// interface OpenStoreData {
|
||||
// firstName: string
|
||||
// lastName: string
|
||||
// email: string
|
||||
// phone: string
|
||||
// patentFile: File
|
||||
// }
|
||||
|
||||
interface OpenStoreResponse {
|
||||
success: boolean
|
||||
message: string
|
||||
}
|
||||
// interface OpenStoreResponse {
|
||||
// success: boolean
|
||||
// message: string
|
||||
// }
|
||||
|
||||
export function useOpenStore() {
|
||||
return useMutation({
|
||||
mutationFn: async (data: OpenStoreData) => {
|
||||
const formData = new FormData()
|
||||
formData.append("first_name", data.firstName)
|
||||
formData.append("last_name", data.lastName)
|
||||
formData.append("email", data.email)
|
||||
formData.append("phone", data.phone)
|
||||
formData.append("patent_file", data.patentFile)
|
||||
// export function useOpenStore() {
|
||||
// return useMutation({
|
||||
// mutationFn: async (data: OpenStoreData) => {
|
||||
// const formData = new FormData()
|
||||
// formData.append("first_name", data.firstName)
|
||||
// formData.append("last_name", data.lastName)
|
||||
// formData.append("email", data.email)
|
||||
// formData.append("phone", data.phone)
|
||||
// formData.append("patent_file", data.patentFile)
|
||||
|
||||
const response = await apiClient.post<OpenStoreResponse>(API_ENDPOINTS.openStore, formData, {
|
||||
headers: {
|
||||
"Content-Type": "multipart/form-data",
|
||||
},
|
||||
})
|
||||
return response.data
|
||||
},
|
||||
})
|
||||
}
|
||||
// const response = await apiClient.post<OpenStoreResponse>(API_ENDPOINTS.openStore, formData, {
|
||||
// headers: {
|
||||
// "Content-Type": "multipart/form-data",
|
||||
// },
|
||||
// })
|
||||
// return response.data
|
||||
// },
|
||||
// })
|
||||
// }
|
||||
|
||||
@@ -1,165 +0,0 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { apiClient } from "@/lib/api";
|
||||
import type { Order, PaginatedResponse } from "@/lib/types/api";
|
||||
|
||||
// Response tiplerini tanımlayalım
|
||||
interface OrderResponse {
|
||||
data?: Order | Order[];
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
interface OrderActionResponse {
|
||||
data?: string | Order;
|
||||
[key: string]: any;
|
||||
}
|
||||
|
||||
// Response'u transform eden yardımcı fonksiyonlar
|
||||
function transformOrdersResponse(response: any): Order[] {
|
||||
if (typeof response === "object" && response.data) {
|
||||
return Array.isArray(response.data) ? response.data : [];
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function transformOrderResponse(response: any): Order | null {
|
||||
if (typeof response === "object" && response.data) {
|
||||
return response.data;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function transformOrderActionResponse(
|
||||
response: any,
|
||||
defaultValue: string
|
||||
): string | Order {
|
||||
if (response && response.data) {
|
||||
return response.data;
|
||||
}
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
function isHtmlResponse(response: any): boolean {
|
||||
return typeof response === "string" && response.includes("<!doctype html>");
|
||||
}
|
||||
|
||||
// Orders list query
|
||||
export function useOrders(options?: { page?: number; perPage?: number }) {
|
||||
return useQuery({
|
||||
queryKey: ["orders", options?.page],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/orders", {
|
||||
params: {
|
||||
page: options?.page || 1,
|
||||
per_page: options?.perPage || 20,
|
||||
},
|
||||
});
|
||||
return transformOrdersResponse(response.data);
|
||||
},
|
||||
staleTime: 1000 * 60 * 5,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Single order query
|
||||
export function useOrder(id: number | string) {
|
||||
return useQuery({
|
||||
queryKey: ["order", id],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get(`/orders/${id}`);
|
||||
return transformOrderResponse(response.data);
|
||||
},
|
||||
enabled: !!id,
|
||||
staleTime: 1000 * 60 * 5,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Order times query
|
||||
export function useOrderTimes() {
|
||||
return useQuery({
|
||||
queryKey: ["order-times"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/order-time");
|
||||
return transformOrdersResponse(response.data);
|
||||
},
|
||||
staleTime: 1000 * 60 * 10,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Order payments query
|
||||
export function useOrderPayments() {
|
||||
return useQuery({
|
||||
queryKey: ["order-payments"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get("/order-payments");
|
||||
return transformOrdersResponse(response.data);
|
||||
},
|
||||
staleTime: 1000 * 60 * 10,
|
||||
retry: 1,
|
||||
});
|
||||
}
|
||||
|
||||
// Create/Place order mutation
|
||||
export function useCreateOrder() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (orderData: Record<string, any>) => {
|
||||
try {
|
||||
const formData = new URLSearchParams();
|
||||
|
||||
// Convert orderData to URLSearchParams
|
||||
Object.entries(orderData).forEach(([key, value]) => {
|
||||
if (value !== null && value !== undefined) {
|
||||
formData.append(key, String(value));
|
||||
}
|
||||
});
|
||||
|
||||
const response = await apiClient.post("/orders", formData, {
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
},
|
||||
validateStatus: (status) => status >= 200 && status < 300,
|
||||
});
|
||||
|
||||
// Check for HTML response
|
||||
if (isHtmlResponse(response.data)) {
|
||||
throw new Error(
|
||||
"Server returned HTML instead of expected response format"
|
||||
);
|
||||
}
|
||||
|
||||
return transformOrderActionResponse(response.data, "Order placed");
|
||||
} catch (error: any) {
|
||||
// Handle HTML error response
|
||||
if (error.response && isHtmlResponse(error.response.data)) {
|
||||
throw new Error(
|
||||
"Server returned HTML instead of expected response format"
|
||||
);
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
queryClient.invalidateQueries({ queryKey: ["orders"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["cart"] });
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Cancel order mutation
|
||||
export function useCancelOrder() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (orderId: number) => {
|
||||
const response = await apiClient.delete(`/orders/${orderId}`);
|
||||
return transformOrderActionResponse(response.data, "Order cancelled");
|
||||
},
|
||||
onSuccess: (_, orderId) => {
|
||||
queryClient.invalidateQueries({ queryKey: ["orders"] });
|
||||
queryClient.invalidateQueries({ queryKey: ["order", orderId] });
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,14 +0,0 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { PaymentTypeOption } from "@/lib/types/api"
|
||||
|
||||
export function usePaymentTypes() {
|
||||
return useQuery({
|
||||
queryKey: ["paymentTypes"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaymentTypeOption[]>("/api/v1/order-payments")
|
||||
return response.data
|
||||
},
|
||||
staleTime: 1000 * 60 * 60, // 1 hour
|
||||
})
|
||||
}
|
||||
@@ -1,216 +0,0 @@
|
||||
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { apiClient } from "@/lib/api";
|
||||
import type { Review, Product, PaginatedResponse } from "@/lib/types/api";
|
||||
|
||||
// Get single review by ID
|
||||
export function useReview(
|
||||
reviewId: number | string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["review", reviewId],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Review>(`/reviews/${reviewId}`);
|
||||
return response.data;
|
||||
},
|
||||
enabled: options?.enabled !== false && !!reviewId,
|
||||
staleTime: 1000 * 60 * 10,
|
||||
});
|
||||
}
|
||||
|
||||
// Get all reviews with pagination
|
||||
export function useReviews(options?: {
|
||||
enabled?: boolean;
|
||||
page?: number;
|
||||
limit?: number;
|
||||
}) {
|
||||
return useQuery({
|
||||
queryKey: ["reviews", options?.page, options?.limit],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Review>>(
|
||||
`/reviews`,
|
||||
{
|
||||
params: {
|
||||
page: options?.page || 1,
|
||||
limit: options?.limit,
|
||||
},
|
||||
}
|
||||
);
|
||||
return {
|
||||
data: response.data.data || [],
|
||||
pagination: response.data.pagination || {},
|
||||
};
|
||||
},
|
||||
enabled: options?.enabled !== false,
|
||||
staleTime: 1000 * 60 * 5,
|
||||
});
|
||||
}
|
||||
|
||||
// Get related reviews for a review
|
||||
export function useRelatedReviews(
|
||||
reviewId: number | string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["review", reviewId, "related"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Review>>(
|
||||
`/reviews/${reviewId}/related`
|
||||
);
|
||||
return response.data.data || response.data;
|
||||
},
|
||||
enabled: options?.enabled !== false && !!reviewId,
|
||||
staleTime: 1000 * 60 * 15,
|
||||
});
|
||||
}
|
||||
|
||||
export function useProducts(options?: UseProductsOptions) {
|
||||
return useQuery({
|
||||
queryKey: ["products", options?.page, options?.perPage],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<PaginatedResponse<Product>>(
|
||||
"/products",
|
||||
{
|
||||
params: {
|
||||
page: options?.page || 1,
|
||||
per_page: options?.perPage || 20,
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data.data || response.data;
|
||||
},
|
||||
staleTime: options?.staleTime ?? 1000 * 60 * 5,
|
||||
enabled: options?.enabled !== false,
|
||||
});
|
||||
}
|
||||
|
||||
// Get single product by ID (for review context)
|
||||
export function useProduct(
|
||||
productId: number | string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["product", productId],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<Product>(`/products/${productId}`);
|
||||
return response.data;
|
||||
},
|
||||
enabled: options?.enabled !== false && !!productId,
|
||||
staleTime: 1000 * 60 * 10,
|
||||
});
|
||||
}
|
||||
|
||||
export function useProductsBySlug(
|
||||
slug: string,
|
||||
options?: { enabled?: boolean }
|
||||
) {
|
||||
return useQuery({
|
||||
queryKey: ["products", "slug", slug],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get(`/products/${slug}`);
|
||||
// API returns { message: "success", data: {...} }
|
||||
return response.data.data || response.data;
|
||||
},
|
||||
enabled: options?.enabled !== false && !!slug,
|
||||
staleTime: 1000 * 60 * 10,
|
||||
});
|
||||
}
|
||||
|
||||
// Submit review mutation
|
||||
export function useSubmitReview() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
productId,
|
||||
rating,
|
||||
title,
|
||||
source,
|
||||
}: {
|
||||
productId: number | string;
|
||||
rating: number;
|
||||
title: string;
|
||||
source: string;
|
||||
}) => {
|
||||
const response = await apiClient.post<Review>(
|
||||
`/products/${productId}/reviews`,
|
||||
{ rating, title, source },
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (_, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["reviews", "product", variables.productId],
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["product", variables.productId],
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["reviews"],
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Update review mutation
|
||||
export function useUpdateReview() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async ({
|
||||
reviewId,
|
||||
rating,
|
||||
title,
|
||||
source,
|
||||
}: {
|
||||
reviewId: number | string;
|
||||
rating?: number;
|
||||
title?: string;
|
||||
source?: string;
|
||||
}) => {
|
||||
const response = await apiClient.put<Review>(
|
||||
`/reviews/${reviewId}`,
|
||||
{ rating, title, source },
|
||||
{
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
}
|
||||
);
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (data, variables) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["review", variables.reviewId],
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["reviews"],
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// Delete review mutation
|
||||
export function useDeleteReview() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation({
|
||||
mutationFn: async (reviewId: number | string) => {
|
||||
const response = await apiClient.delete(`/reviews/${reviewId}`);
|
||||
return response.data;
|
||||
},
|
||||
onSuccess: (_, reviewId) => {
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["review", reviewId],
|
||||
});
|
||||
queryClient.invalidateQueries({
|
||||
queryKey: ["reviews"],
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
@@ -1,28 +1,28 @@
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import type { SearchFilters, SearchResponse } from "@/lib/types/api"
|
||||
// import { useQuery } from "@tanstack/react-query"
|
||||
// import { apiClient } from "@/lib/api"
|
||||
// import type { SearchFilters, SearchResponse } from "@/lib/types/api"
|
||||
|
||||
export function useSearch(options: SearchFilters) {
|
||||
const { q, category_id, brand_id, price_from, price_to, page = 1, per_page = 20 } = options
|
||||
// export function useSearch(options: SearchFilters) {
|
||||
// const { q, category_id, brand_id, price_from, price_to, page = 1, per_page = 20 } = options
|
||||
|
||||
return useQuery({
|
||||
queryKey: ["search", { q, category_id, brand_id, price_from, price_to, page, per_page }],
|
||||
queryFn: async () => {
|
||||
const params = new URLSearchParams({
|
||||
page: String(page),
|
||||
per_page: String(per_page),
|
||||
})
|
||||
// return useQuery({
|
||||
// queryKey: ["search", { q, category_id, brand_id, price_from, price_to, page, per_page }],
|
||||
// queryFn: async () => {
|
||||
// const params = new URLSearchParams({
|
||||
// page: String(page),
|
||||
// per_page: String(per_page),
|
||||
// })
|
||||
|
||||
if (q) params.append("q", q)
|
||||
if (category_id) params.append("category_id", String(category_id))
|
||||
if (brand_id) params.append("brand_id", String(brand_id))
|
||||
if (price_from) params.append("price_from", String(price_from))
|
||||
if (price_to) params.append("price_to", String(price_to))
|
||||
// // if (q) params.append("q", q)
|
||||
// if (category_id) params.append("category_id", String(category_id))
|
||||
// if (brand_id) params.append("brand_id", String(brand_id))
|
||||
// if (price_from) params.append("price_from", String(price_from))
|
||||
// if (price_to) params.append("price_to", String(price_to))
|
||||
|
||||
const response = await apiClient.get<SearchResponse>(`/search?${params}`)
|
||||
return response.data
|
||||
},
|
||||
enabled: !!q && q.length > 0,
|
||||
staleTime: 1000 * 60 * 5,
|
||||
})
|
||||
}
|
||||
// const response = await apiClient.get<SearchResponse>(`/search?${params}`)
|
||||
// return response.data
|
||||
// },
|
||||
// enabled: !!q && q.length > 0,
|
||||
// staleTime: 1000 * 60 * 5,
|
||||
// })
|
||||
// }
|
||||
|
||||
@@ -1,18 +0,0 @@
|
||||
"use client"
|
||||
|
||||
import { useQuery } from "@tanstack/react-query"
|
||||
import { apiClient } from "@/lib/api"
|
||||
import { API_ENDPOINTS } from "@/lib/config/api-endpoints"
|
||||
import type { UserProfile } from "@/lib/types/api"
|
||||
|
||||
export function useUserProfile(options?: { enabled?: boolean }) {
|
||||
return useQuery({
|
||||
queryKey: ["user", "profile"],
|
||||
queryFn: async () => {
|
||||
const response = await apiClient.get<UserProfile>(API_ENDPOINTS.profile)
|
||||
return response.data
|
||||
},
|
||||
staleTime: 1000 * 60 * 10, // 10 minutes
|
||||
enabled: options?.enabled !== false,
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user