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
@@ -32,11 +32,11 @@ const mockProduct = {
|
||||
dosage_logic: '1',
|
||||
benefits: '',
|
||||
symptoms: [],
|
||||
suitableFor: 'سگ' as any,
|
||||
suitableFor: 'سگ' as never,
|
||||
calculateDosage: () => ({ quantity: 1, unit: 'tablet', description: '' }),
|
||||
analysis: {},
|
||||
feedingAdvice: '',
|
||||
specialist: null as any,
|
||||
specialist: null as never,
|
||||
image: '',
|
||||
};
|
||||
|
||||
|
||||
@@ -44,7 +44,7 @@ interface CartStore {
|
||||
addOrder: (orderData: Omit<Order, 'id' | 'date' | 'status' | 'trackingNumber'>) => Promise<string>;
|
||||
clearCart: () => void;
|
||||
removeCoupon: () => void;
|
||||
setOrders: (orders: any[]) => void;
|
||||
setOrders: (orders: Record<string, unknown>[]) => void;
|
||||
getTotalItems: () => number;
|
||||
getSubtotal: () => number;
|
||||
getDiscount: () => number;
|
||||
@@ -120,6 +120,7 @@ export const useCartStore = create<CartStore>()(
|
||||
|
||||
try {
|
||||
const backendOrder = await orderService.createOrder(payload);
|
||||
const bo = backendOrder as unknown as Record<string, unknown>;
|
||||
|
||||
const newOrder: Order = {
|
||||
...orderData,
|
||||
@@ -127,15 +128,15 @@ export const useCartStore = create<CartStore>()(
|
||||
date: backendOrder.createdAt,
|
||||
total: Number(backendOrder.totalAmount),
|
||||
charityDonation: Number(backendOrder.charityDonation),
|
||||
status: backendOrder.status as any,
|
||||
isRefill: (backendOrder as any).isRefill !== undefined ? Boolean((backendOrder as any).isRefill) : Boolean(orderData.isRefill || get().isSubscribed),
|
||||
paymentMethod: (backendOrder as any).paymentMethod || orderData.paymentMethod,
|
||||
status: (['processing', 'shipped', 'delivered'].includes(backendOrder.status) ? backendOrder.status : 'processing') as Order['status'],
|
||||
isRefill: bo.isRefill !== undefined ? Boolean(bo.isRefill) : Boolean(orderData.isRefill || get().isSubscribed),
|
||||
paymentMethod: (bo.paymentMethod as string) || orderData.paymentMethod,
|
||||
trackingNumber: backendOrder.trackingNumber || `CN-${Math.floor(Math.random() * 90000) + 10000}`
|
||||
};
|
||||
|
||||
set({ orders: [newOrder, ...get().orders] });
|
||||
return backendOrder.id;
|
||||
} catch (error: any) {
|
||||
} catch (error) {
|
||||
console.error("Order creation failed on backend:", error);
|
||||
throw error;
|
||||
}
|
||||
@@ -144,20 +145,20 @@ export const useCartStore = create<CartStore>()(
|
||||
removeCoupon: () => set({ coupon: null }),
|
||||
setOrders: (backendOrders) => {
|
||||
const mappedOrders: Order[] = backendOrders.map(bo => ({
|
||||
id: bo.id,
|
||||
date: bo.createdAt,
|
||||
items: bo.orderItems?.map((oi: any) => ({
|
||||
id: String(bo.id),
|
||||
date: String(bo.createdAt),
|
||||
items: (bo.orderItems as Array<{ product: Product; quantity: number }>)?.map((oi) => ({
|
||||
product: oi.product,
|
||||
quantity: oi.quantity
|
||||
})) || [],
|
||||
total: Number(bo.totalAmount),
|
||||
charityDonation: Number(bo.charityDonation),
|
||||
status: bo.status,
|
||||
isRefill: Boolean((bo as any).isRefill),
|
||||
paymentMethod: (bo as any).paymentMethod,
|
||||
petId: bo.petId || bo.pet?.id,
|
||||
shippingAddress: bo.shippingAddress,
|
||||
trackingNumber: bo.trackingNumber || `CN-${Math.floor(Math.random() * 90000) + 10000}`
|
||||
status: (['processing', 'shipped', 'delivered'].includes(String(bo.status)) ? bo.status : 'processing') as Order['status'],
|
||||
isRefill: Boolean(bo.isRefill),
|
||||
paymentMethod: bo.paymentMethod as string | undefined,
|
||||
petId: (bo.petId as string) || ((bo.pet as { id?: string })?.id),
|
||||
shippingAddress: bo.shippingAddress as string | undefined,
|
||||
trackingNumber: (bo.trackingNumber as string) || `CN-${Math.floor(Math.random() * 90000) + 10000}`
|
||||
}));
|
||||
set({ orders: mappedOrders });
|
||||
},
|
||||
|
||||
@@ -4,9 +4,9 @@ interface UIState {
|
||||
isCartOpen: boolean;
|
||||
isLoginModalOpen: boolean;
|
||||
isB2BPortalOpen: boolean;
|
||||
advisorData: any;
|
||||
advisorData: Record<string, unknown> | null;
|
||||
setCartOpen: (open: boolean) => void;
|
||||
setLoginModalOpen: (open: boolean, advisorData?: any) => void;
|
||||
setLoginModalOpen: (open: boolean, advisorData?: Record<string, unknown> | null) => void;
|
||||
setB2BPortalOpen: (open: boolean) => void;
|
||||
clearAdvisorData: () => void;
|
||||
}
|
||||
|
||||
@@ -3,6 +3,7 @@ import { create } from "zustand";
|
||||
import { persist } from "zustand/middleware";
|
||||
import api from "../services/api";
|
||||
import { toast } from "sonner";
|
||||
import { useUserStore } from "./userStore";
|
||||
|
||||
export interface Reminder {
|
||||
id: string;
|
||||
@@ -54,7 +55,7 @@ interface PetStore {
|
||||
addReminder: (petId: string, reminder: Omit<Reminder, "id" | "completedDates">) => Promise<void>;
|
||||
toggleReminder: (petId: string, reminderId: string, date: string) => Promise<void>;
|
||||
addHealthLog: (petId: string, log: Omit<HealthLog, "id" | "date">) => Promise<void>;
|
||||
setPets: (pets: any[]) => void;
|
||||
setPets: (pets: Record<string, unknown>[]) => void;
|
||||
reset: () => void;
|
||||
}
|
||||
|
||||
@@ -125,7 +126,6 @@ export const usePetStore = create<PetStore>()(
|
||||
getActivePet: () => {
|
||||
const { pets, activePetId } = get();
|
||||
try {
|
||||
const { useUserStore } = require("./userStore");
|
||||
if (!useUserStore.getState().isLoggedIn) {
|
||||
return null;
|
||||
}
|
||||
@@ -195,29 +195,29 @@ export const usePetStore = create<PetStore>()(
|
||||
const mappedPets: PetProfile[] = backendPets.map(bp => {
|
||||
const existingLocal = currentLocalPets.find(lp => lp.id === bp.id);
|
||||
return {
|
||||
id: bp.id,
|
||||
name: bp.name,
|
||||
type: bp.type as any,
|
||||
breed: bp.breed,
|
||||
age: Number(bp.age),
|
||||
weight: Number(bp.weight),
|
||||
activityLevel: bp.activityLevel as any,
|
||||
medicalConditions: bp.medicalConditions?.map((mc: any) => mc.condition) || [],
|
||||
image: bp.imageUrl || undefined,
|
||||
reminders: bp.reminders?.map((r: any) => ({
|
||||
id: String(bp.id),
|
||||
name: String(bp.name),
|
||||
type: (bp.type as PetProfile['type']) || "سگ",
|
||||
breed: String(bp.breed || ''),
|
||||
age: Number(bp.age || 0),
|
||||
weight: Number(bp.weight || 0),
|
||||
activityLevel: (bp.activityLevel as PetProfile['activityLevel']) || "متوسط",
|
||||
medicalConditions: (bp.medicalConditions as Array<{ condition: string }>)?.map((mc) => mc.condition) || [],
|
||||
image: (bp.imageUrl as string) || undefined,
|
||||
reminders: (bp.reminders as Array<{ id: string; title: string; time: string; frequency: Reminder['frequency']; productId?: string; completions?: Array<{ completedDate: string }> }>)?.map((r) => ({
|
||||
id: r.id,
|
||||
title: r.title,
|
||||
time: r.time,
|
||||
frequency: r.frequency as any,
|
||||
frequency: r.frequency,
|
||||
productId: r.productId,
|
||||
completedDates: r.completions?.map((c: any) => c.completedDate) || []
|
||||
completedDates: r.completions?.map((c) => c.completedDate) || []
|
||||
})) || [],
|
||||
logs: bp.healthLogs?.map((hl: any) => ({
|
||||
logs: (bp.healthLogs as Array<{ id: string; loggedDate: string; appetite: HealthLog['appetite']; energy: HealthLog['energy']; digestion: HealthLog['digestion']; note?: string }>)?.map((hl) => ({
|
||||
id: hl.id,
|
||||
date: hl.loggedDate,
|
||||
appetite: hl.appetite as any,
|
||||
energy: hl.energy as any,
|
||||
digestion: hl.digestion as any,
|
||||
appetite: hl.appetite,
|
||||
energy: hl.energy,
|
||||
digestion: hl.digestion,
|
||||
note: hl.note || undefined
|
||||
})) || [],
|
||||
consumptions: existingLocal?.consumptions || []
|
||||
|
||||
@@ -94,7 +94,16 @@ export const useUserStore = create<UserStore>()(
|
||||
addAddress: async (address) => {
|
||||
try {
|
||||
// Remove ID so backend generates UUID
|
||||
const { id, ...addressData } = address;
|
||||
const addressData = {
|
||||
title: address.title,
|
||||
receptorName: address.receptorName,
|
||||
phone: address.phone,
|
||||
province: address.province,
|
||||
city: address.city,
|
||||
detail: address.detail,
|
||||
zipCode: address.zipCode,
|
||||
isDefault: address.isDefault,
|
||||
};
|
||||
await api.post('/users/addresses', addressData);
|
||||
await useUserStore.getState().fetchProfile();
|
||||
} catch (error) {
|
||||
@@ -104,7 +113,16 @@ export const useUserStore = create<UserStore>()(
|
||||
},
|
||||
updateAddress: async (id, updated) => {
|
||||
try {
|
||||
const { id: _, ...addressData } = updated;
|
||||
const addressData = {
|
||||
title: updated.title,
|
||||
receptorName: updated.receptorName,
|
||||
phone: updated.phone,
|
||||
province: updated.province,
|
||||
city: updated.city,
|
||||
detail: updated.detail,
|
||||
zipCode: updated.zipCode,
|
||||
isDefault: updated.isDefault,
|
||||
};
|
||||
await api.patch(`/users/addresses/${id}`, addressData);
|
||||
await useUserStore.getState().fetchProfile();
|
||||
} catch (error) {
|
||||
@@ -201,14 +219,15 @@ export const useUserStore = create<UserStore>()(
|
||||
return;
|
||||
}
|
||||
set((state) => {
|
||||
const pd = profileData as unknown as Record<string, unknown>;
|
||||
const backendWallet = Number(profileData.walletBalance || 0);
|
||||
const backendCharity = Number(profileData.charityDonationTotal || 0);
|
||||
const backendTransactions = (profileData as any).walletTransactions?.map((t: any) => ({
|
||||
const backendTransactions = (pd.walletTransactions as Array<{ id: string; type: string; amount: number; createdAt: string; status: string }>)?.map((t) => ({
|
||||
id: t.id,
|
||||
type: t.type === 'deposit' ? 'top_up' : 'purchase',
|
||||
type: (t.type === 'deposit' ? 'top_up' : 'purchase') as Transaction['type'],
|
||||
amount: Number(t.amount),
|
||||
date: t.createdAt,
|
||||
status: t.status === 'completed' ? 'success' : t.status === 'failed' ? 'failed' : 'pending'
|
||||
status: (t.status === 'completed' ? 'success' : t.status === 'failed' ? 'failed' : 'pending') as Transaction['status']
|
||||
})) || [];
|
||||
|
||||
return {
|
||||
@@ -221,7 +240,7 @@ export const useUserStore = create<UserStore>()(
|
||||
mobile: profileData.mobile || "",
|
||||
walletBalance: backendWallet,
|
||||
charityDonationTotal: backendCharity,
|
||||
addresses: (profileData as any).addresses?.length > 0 ? (profileData as any).addresses : state.profile.addresses,
|
||||
addresses: Array.isArray(pd.addresses) && pd.addresses.length > 0 ? (pd.addresses as Address[]) : state.profile.addresses,
|
||||
transactions: backendTransactions.length > 0 ? backendTransactions : state.profile.transactions
|
||||
}
|
||||
};
|
||||
@@ -230,11 +249,11 @@ export const useUserStore = create<UserStore>()(
|
||||
// Sync orders list to CartStore while preserving local petId & address mappings
|
||||
if (profileData.orders) {
|
||||
const currentLocalOrders = useCartStore.getState().orders;
|
||||
const mergedOrders = profileData.orders.map((bo: any) => {
|
||||
const mergedOrders = (profileData.orders as Record<string, unknown>[]).map((bo) => {
|
||||
const localMatch = currentLocalOrders.find(lo => lo.id === bo.id);
|
||||
return {
|
||||
...bo,
|
||||
petId: bo.petId || bo.pet?.id || localMatch?.petId,
|
||||
petId: bo.petId || (bo.pet as { id?: string })?.id || localMatch?.petId,
|
||||
shippingAddress: bo.shippingAddress || localMatch?.shippingAddress
|
||||
};
|
||||
});
|
||||
@@ -242,7 +261,7 @@ export const useUserStore = create<UserStore>()(
|
||||
}
|
||||
// Sync pets list to PetStore
|
||||
if (profileData.pets) {
|
||||
usePetStore.getState().setPets(profileData.pets);
|
||||
usePetStore.getState().setPets(profileData.pets as Record<string, unknown>[]);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch user profile:", error);
|
||||
|
||||
Reference in new issue
Block a user