fixed order, image carousel
This commit is contained in:
84
lib/api.ts
84
lib/api.ts
@@ -1,6 +1,10 @@
|
||||
// lib/api.ts
|
||||
|
||||
import axios, { type AxiosInstance, type AxiosRequestConfig, type AxiosResponse } from "axios";
|
||||
import axios, {
|
||||
type AxiosInstance,
|
||||
type AxiosRequestConfig,
|
||||
type AxiosResponse,
|
||||
} from "axios";
|
||||
import TokenStorage from "./tokenStorage";
|
||||
|
||||
const localeToApiLang = (locale: string): string => {
|
||||
@@ -42,27 +46,32 @@ class APIClient {
|
||||
config.headers.Authorization = `Bearer ${token}`;
|
||||
}
|
||||
|
||||
// Add language parameter
|
||||
let lang = "tm";
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
if ((window as any).i18n?.language) {
|
||||
lang = localeToApiLang((window as any).i18n.language);
|
||||
} else {
|
||||
const pathLocale = window.location.pathname.split("/")[1];
|
||||
if (pathLocale === "tm" || pathLocale === "ru") {
|
||||
lang = localeToApiLang(pathLocale);
|
||||
// Add language parameter (except for POST requests to /orders)
|
||||
const url = config.url || "";
|
||||
const isOrderPost =
|
||||
config.method?.toLowerCase() === "post" && url.includes("/orders");
|
||||
|
||||
if (!isOrderPost) {
|
||||
let lang = "tm";
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
if ((window as any).i18n?.language) {
|
||||
lang = localeToApiLang((window as any).i18n.language);
|
||||
} else {
|
||||
const pathLocale = window.location.pathname.split("/")[1];
|
||||
if (pathLocale === "tm" || pathLocale === "ru") {
|
||||
lang = localeToApiLang(pathLocale);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const url = config.url || "";
|
||||
const separator = url.includes("?") ? "&" : "?";
|
||||
config.url = `${url}${separator}lang=${lang}`;
|
||||
const separator = url.includes("?") ? "&" : "?";
|
||||
config.url = `${url}${separator}lang=${lang}`;
|
||||
}
|
||||
|
||||
return config;
|
||||
},
|
||||
(error) => Promise.reject(error)
|
||||
(error) => Promise.reject(error),
|
||||
);
|
||||
|
||||
// Response interceptor
|
||||
@@ -92,11 +101,12 @@ class APIClient {
|
||||
"Content-Type": "application/json",
|
||||
"Api-Token": process.env.NEXT_PUBLIC_API_TOKEN || "123",
|
||||
},
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
const newToken = guestTokenResponse.data?.token || guestTokenResponse.data?.data;
|
||||
|
||||
const newToken =
|
||||
guestTokenResponse.data?.token || guestTokenResponse.data?.data;
|
||||
|
||||
if (newToken) {
|
||||
TokenStorage.setGuestToken(newToken);
|
||||
this.processQueue(null);
|
||||
@@ -105,11 +115,11 @@ class APIClient {
|
||||
} catch (refreshError) {
|
||||
this.processQueue(refreshError);
|
||||
TokenStorage.clearTokens();
|
||||
|
||||
|
||||
if (typeof window !== "undefined") {
|
||||
window.location.href = "/login";
|
||||
}
|
||||
|
||||
|
||||
return Promise.reject(refreshError);
|
||||
} finally {
|
||||
this.isRefreshing = false;
|
||||
@@ -131,7 +141,7 @@ class APIClient {
|
||||
}
|
||||
|
||||
return Promise.reject(error);
|
||||
}
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
@@ -146,25 +156,43 @@ class APIClient {
|
||||
this.failedQueue = [];
|
||||
}
|
||||
|
||||
get<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
get<T = any>(
|
||||
url: string,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<T>> {
|
||||
return this.client.get<T>(url, config);
|
||||
}
|
||||
|
||||
post<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
post<T = any>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<T>> {
|
||||
return this.client.post<T>(url, data, config);
|
||||
}
|
||||
|
||||
put<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
put<T = any>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<T>> {
|
||||
return this.client.put<T>(url, data, config);
|
||||
}
|
||||
|
||||
patch<T = any>(url: string, data?: any, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
patch<T = any>(
|
||||
url: string,
|
||||
data?: any,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<T>> {
|
||||
return this.client.patch<T>(url, data, config);
|
||||
}
|
||||
|
||||
delete<T = any>(url: string, config?: AxiosRequestConfig): Promise<AxiosResponse<T>> {
|
||||
delete<T = any>(
|
||||
url: string,
|
||||
config?: AxiosRequestConfig,
|
||||
): Promise<AxiosResponse<T>> {
|
||||
return this.client.delete<T>(url, config);
|
||||
}
|
||||
}
|
||||
|
||||
export const apiClient = new APIClient();
|
||||
export const apiClient = new APIClient();
|
||||
|
||||
@@ -14,8 +14,8 @@ interface LoginCredentials {
|
||||
|
||||
interface RegisterData {
|
||||
phone_number: string;
|
||||
name?: string;
|
||||
email?: string;
|
||||
name: string;
|
||||
address: string;
|
||||
}
|
||||
|
||||
interface VerifyTokenData {
|
||||
@@ -52,31 +52,31 @@ function extractToken(data: AuthResponse): string {
|
||||
|
||||
function handleAuthError(error: unknown): AuthError {
|
||||
if (error instanceof AxiosError) {
|
||||
if (error.code === 'ECONNABORTED') {
|
||||
if (error.code === "ECONNABORTED") {
|
||||
return {
|
||||
message: "Request timeout - server not responding",
|
||||
code: "TIMEOUT",
|
||||
statusCode: 408
|
||||
statusCode: 408,
|
||||
};
|
||||
}
|
||||
if (error.response) {
|
||||
return {
|
||||
message: error.response.data?.message || "Authentication failed",
|
||||
code: error.response.data?.code || "AUTH_ERROR",
|
||||
statusCode: error.response.status
|
||||
statusCode: error.response.status,
|
||||
};
|
||||
}
|
||||
if (error.request) {
|
||||
return {
|
||||
message: "Network error - cannot reach server",
|
||||
code: "NETWORK_ERROR",
|
||||
statusCode: 0
|
||||
statusCode: 0,
|
||||
};
|
||||
}
|
||||
}
|
||||
return {
|
||||
message: error instanceof Error ? error.message : "Unknown error occurred",
|
||||
code: "UNKNOWN_ERROR"
|
||||
code: "UNKNOWN_ERROR",
|
||||
};
|
||||
}
|
||||
|
||||
@@ -106,8 +106,8 @@ export function useGetGuestToken() {
|
||||
{},
|
||||
{
|
||||
signal: controller.signal,
|
||||
timeout: 10000
|
||||
}
|
||||
timeout: 10000,
|
||||
},
|
||||
);
|
||||
clearTimeout(timeoutId);
|
||||
return extractToken(response.data);
|
||||
@@ -123,7 +123,7 @@ export function useGetGuestToken() {
|
||||
console.error("[Guest Token] Failed:", {
|
||||
message: error.message,
|
||||
code: error.code,
|
||||
statusCode: error.statusCode
|
||||
statusCode: error.statusCode,
|
||||
});
|
||||
},
|
||||
retry: (failureCount, error) => {
|
||||
@@ -147,7 +147,7 @@ export function useLogin() {
|
||||
const response = await apiClient.post<AuthResponse>(
|
||||
"/auth/login",
|
||||
credentials,
|
||||
{ timeout: 15000 }
|
||||
{ timeout: 15000 },
|
||||
);
|
||||
return extractToken(response.data);
|
||||
},
|
||||
@@ -172,7 +172,7 @@ export function useRegister() {
|
||||
const response = await apiClient.post<AuthResponse>(
|
||||
"/auth/register",
|
||||
userData,
|
||||
{ timeout: 15000 }
|
||||
{ timeout: 15000 },
|
||||
);
|
||||
return extractToken(response.data);
|
||||
},
|
||||
@@ -197,7 +197,7 @@ export function useVerifyToken() {
|
||||
const response = await apiClient.post<AuthResponse>(
|
||||
"/auth/verify",
|
||||
verifyData,
|
||||
{ timeout: 15000 }
|
||||
{ timeout: 15000 },
|
||||
);
|
||||
return extractToken(response.data);
|
||||
},
|
||||
@@ -222,7 +222,9 @@ export function useLogout() {
|
||||
try {
|
||||
await apiClient.post("/auth/logout", {}, { timeout: 5000 });
|
||||
} catch (error) {
|
||||
console.warn("[Logout] Server call failed, clearing local state anyway");
|
||||
console.warn(
|
||||
"[Logout] Server call failed, clearing local state anyway",
|
||||
);
|
||||
}
|
||||
},
|
||||
onSuccess: () => {
|
||||
@@ -234,4 +236,4 @@ export function useLogout() {
|
||||
queryClient.clear();
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -235,7 +235,7 @@ export interface CreateOrderRequest {
|
||||
delivery_time?: string;
|
||||
delivery_at?: string;
|
||||
region: string;
|
||||
note?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
export interface CreateOrderPayload {
|
||||
@@ -247,7 +247,7 @@ export interface CreateOrderPayload {
|
||||
delivery_time?: string;
|
||||
delivery_at?: string;
|
||||
region: string;
|
||||
note?: string;
|
||||
notes?: string;
|
||||
}
|
||||
|
||||
// Pagination Types
|
||||
|
||||
Reference in New Issue
Block a user