added some api

This commit is contained in:
Jelaletdin12
2025-11-13 21:56:30 +05:00
parent fdec9e4b0e
commit 21b9e88c5c
22 changed files with 2268 additions and 930 deletions

View File

@@ -1,59 +1,165 @@
import { useQuery, useMutation, useQueryClient } from "@tanstack/react-query"
import { apiClient } from "@/lib/api"
import type { Order, PaginatedResponse } from "@/lib/types/api"
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<PaginatedResponse<Order>>("/orders", {
const response = await apiClient.get("/orders", {
params: {
page: options?.page || 1,
per_page: options?.perPage || 20,
},
})
return response.data.data || response.data
});
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<Order>(`/orders/${id}`)
return response.data
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()
const queryClient = useQueryClient();
return useMutation({
mutationFn: async (orderId: number) => {
await apiClient.post(`/orders/${orderId}/cancel`, {})
const response = await apiClient.delete(`/orders/${orderId}`);
return transformOrderActionResponse(response.data, "Order cancelled");
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["orders"] })
onSuccess: (_, orderId) => {
queryClient.invalidateQueries({ queryKey: ["orders"] });
queryClient.invalidateQueries({ queryKey: ["order", orderId] });
},
})
}
export function useCreateOrder() {
const queryClient = useQueryClient()
return useMutation({
mutationFn: async (orderData: any) => {
const response = await apiClient.post<Order>("/orders", orderData)
return response.data
},
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ["orders"] })
queryClient.invalidateQueries({ queryKey: ["cart"] })
},
})
});
}