fix(lint&types): resolve all lint warnings, typecheck errors and effect state updates across frontend application and admin panel
This commit is contained in:
1 parent
920dd532e8
commit
c1b2f416ed
29 files changed
+442
-352
No files matched your search
@@ -1,5 +1,6 @@
|
||||
import axios from 'axios';
|
||||
import { toast } from 'sonner';
|
||||
import { useUserStore } from '../store/userStore';
|
||||
|
||||
const baseURL = process.env.NEXT_PUBLIC_API_URL || (process.env.NODE_ENV === 'development' ? 'http://localhost:4001/api' : '/api');
|
||||
|
||||
@@ -8,7 +9,7 @@ export interface ApiErrorPayload {
|
||||
statusCode: number;
|
||||
message: string;
|
||||
code: string;
|
||||
details?: Array<{ field: string; message: string }> | Record<string, any>;
|
||||
details?: Array<{ field: string; message: string }> | Record<string, unknown>;
|
||||
timestamp?: string;
|
||||
path?: string;
|
||||
}
|
||||
@@ -50,7 +51,6 @@ api.interceptors.response.use(
|
||||
localStorage.removeItem('accessToken');
|
||||
localStorage.removeItem('refreshToken');
|
||||
try {
|
||||
const { useUserStore } = require('../store/userStore');
|
||||
useUserStore.getState().logout();
|
||||
} catch {
|
||||
// Ignore if store not ready
|
||||
|
||||
@@ -11,8 +11,8 @@ export interface User {
|
||||
charityDonationTotal: number;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
pets?: any[];
|
||||
orders?: any[];
|
||||
pets?: unknown[];
|
||||
orders?: unknown[];
|
||||
}
|
||||
|
||||
export interface AuthResponse {
|
||||
@@ -23,6 +23,15 @@ export interface AuthResponse {
|
||||
};
|
||||
}
|
||||
|
||||
interface ApiErr {
|
||||
response?: {
|
||||
status?: number;
|
||||
data?: {
|
||||
message?: string | string[];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export class AuthService {
|
||||
private static instance: AuthService;
|
||||
|
||||
@@ -42,8 +51,9 @@ export class AuthService {
|
||||
try {
|
||||
const response = await api.post('/auth/send-otp', { phoneNumber });
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در ارسال کد تایید';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در ارسال کد تایید';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -59,8 +69,9 @@ export class AuthService {
|
||||
localStorage.setItem('accessToken', data.accessToken);
|
||||
}
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'کد تایید نامعتبر است';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'کد تایید نامعتبر است';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -68,7 +79,7 @@ export class AuthService {
|
||||
/**
|
||||
* Register with email/mobile and password
|
||||
*/
|
||||
public async register(data: any): Promise<AuthResponse> {
|
||||
public async register(data: Record<string, unknown>): Promise<AuthResponse> {
|
||||
try {
|
||||
const response = await api.post('/auth/register', data);
|
||||
const { data: resData } = response.data;
|
||||
@@ -76,8 +87,9 @@ export class AuthService {
|
||||
localStorage.setItem('accessToken', resData.accessToken);
|
||||
}
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در ثبتنام';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در ثبتنام';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -93,8 +105,9 @@ export class AuthService {
|
||||
localStorage.setItem('accessToken', data.accessToken);
|
||||
}
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'نام کاربری یا رمز عبور اشتباه است';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'نام کاربری یا رمز عبور اشتباه است';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -106,11 +119,12 @@ export class AuthService {
|
||||
try {
|
||||
const response = await api.get('/users/profile');
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
if (error.response?.status === 401 || error.response?.status === 403) {
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
if (err.response?.status === 401 || err.response?.status === 403) {
|
||||
return null;
|
||||
}
|
||||
const message = error.response?.data?.message || 'خطا در دریافت اطلاعات کاربری';
|
||||
const message = err.response?.data?.message || 'خطا در دریافت اطلاعات کاربری';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -122,8 +136,9 @@ export class AuthService {
|
||||
try {
|
||||
const response = await api.patch('/users/profile', profileData);
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در ویرایش اطلاعات کاربری';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در ویرایش اطلاعات کاربری';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,12 +1,11 @@
|
||||
import api from './api';
|
||||
import { CartItem } from '../store/cartStore';
|
||||
|
||||
export interface OrderItem {
|
||||
id: string;
|
||||
orderId: string;
|
||||
productId: string | null;
|
||||
quantity: number;
|
||||
product?: any;
|
||||
product?: Record<string, unknown>;
|
||||
}
|
||||
|
||||
export interface Order {
|
||||
@@ -21,6 +20,14 @@ export interface Order {
|
||||
orderItems: OrderItem[];
|
||||
}
|
||||
|
||||
interface ApiErr {
|
||||
response?: {
|
||||
data?: {
|
||||
message?: string | string[];
|
||||
};
|
||||
};
|
||||
}
|
||||
|
||||
export class OrderService {
|
||||
private static instance: OrderService;
|
||||
|
||||
@@ -50,8 +57,9 @@ export class OrderService {
|
||||
try {
|
||||
const response = await api.post('/orders', orderData);
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در ثبت سفارش';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در ثبت سفارش';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -63,8 +71,9 @@ export class OrderService {
|
||||
try {
|
||||
const response = await api.get('/orders');
|
||||
return response.data.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در دریافت لیست سفارشها';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در دریافت لیست سفارشها';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
@@ -76,8 +85,9 @@ export class OrderService {
|
||||
try {
|
||||
const response = await api.get(`/orders/${id}`);
|
||||
return response.data;
|
||||
} catch (error: any) {
|
||||
const message = error.response?.data?.message || 'خطا در دریافت جزئیات سفارش';
|
||||
} catch (error) {
|
||||
const err = error as ApiErr;
|
||||
const message = err.response?.data?.message || 'خطا در دریافت جزئیات سفارش';
|
||||
throw new Error(Array.isArray(message) ? message[0] : message);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -23,18 +23,19 @@ export class ProductService {
|
||||
return ProductService.instance;
|
||||
}
|
||||
|
||||
private mapBackendToFrontend(data: any): Product {
|
||||
private mapBackendToFrontend(inputData: Record<string, unknown>): Product {
|
||||
const data = inputData as any;
|
||||
// Find the local static product to inherit functions like calculateDosage
|
||||
const local = PRODUCTS.find(p =>
|
||||
p.artNo === data.artNo ||
|
||||
p.id === data.productGroup ||
|
||||
(data.slug && data.slug.includes(p.id))
|
||||
(typeof data.slug === 'string' && data.slug.includes(p.id))
|
||||
);
|
||||
|
||||
const safeParse = (val: any, fallback: any) => {
|
||||
const safeParse = (val: unknown, fallback: unknown) => {
|
||||
if (!val) return fallback;
|
||||
if (typeof val === 'string') {
|
||||
try { return JSON.parse(val); } catch (e) { return fallback; }
|
||||
try { return JSON.parse(val); } catch { return fallback; }
|
||||
}
|
||||
return val;
|
||||
};
|
||||
@@ -68,8 +69,8 @@ export class ProductService {
|
||||
? data.imageUrl
|
||||
: (local?.image || (data.imageUrl ? `/api${data.imageUrl}` : data.image || '')),
|
||||
|
||||
main_ingredients: data.ingredientList?.map((i: any) => i.ingredient) || data.ingredients?.split(/[،,-]/).map((s: string) => s.trim()).filter(Boolean) || [],
|
||||
symptoms: data.symptoms?.map((s: any) => s.symptom || s) || [],
|
||||
main_ingredients: data.ingredientList?.map((i: { ingredient: string }) => i.ingredient) || data.ingredients?.split(/[،,-]/).map((s: string) => s.trim()).filter(Boolean) || [],
|
||||
symptoms: data.symptoms?.map((s: { symptom?: string }) => s.symptom || s) || [],
|
||||
keyBenefits: safeParse(data.keyBenefits, []),
|
||||
expectedResults: safeParse(data.expectedResults, []),
|
||||
benefitsList: safeParse(data.benefitsList, []),
|
||||
@@ -102,7 +103,7 @@ export class ProductService {
|
||||
try {
|
||||
const response = await api.get(`/products?${params.toString()}`);
|
||||
return {
|
||||
data: response.data.data.map((item: any) => this.mapBackendToFrontend(item)),
|
||||
data: response.data.data.map((item: Record<string, unknown>) => this.mapBackendToFrontend(item)),
|
||||
meta: response.data.meta,
|
||||
};
|
||||
} catch (error) {
|
||||
@@ -134,7 +135,7 @@ export class ProductService {
|
||||
public async getFeaturedProducts(): Promise<Product[]> {
|
||||
try {
|
||||
const response = await api.get('/products?limit=30');
|
||||
const all = response.data.data.map((item: any) => this.mapBackendToFrontend(item));
|
||||
const all = response.data.data.map((item: Record<string, unknown>) => this.mapBackendToFrontend(item));
|
||||
const featured: Product[] = [];
|
||||
const categoriesSeen = new Set<string>();
|
||||
|
||||
|
||||
Reference in new issue
Block a user