Files
approagency-admin-panel/lib/utils.ts
T
2026-06-17 19:10:21 +03:30

61 lines
1.7 KiB
TypeScript

import { User } from "@/types/user";
// Single source of truth for product type labels lives in types/product.ts
// (kept in sync with the backend Product::TYPES). Re-exported here so existing
// `@/lib/utils` imports keep working without a divergent (previously wrong) map.
export { getProductTypeLabel } from "@/types/product";
export const formatDate = (dateString: string | null): string => {
if (!dateString) return 'نامشخص';
try {
const date = new Date(dateString);
return new Intl.DateTimeFormat('fa-IR', {
year: 'numeric',
month: 'long',
day: 'numeric',
}).format(date);
} catch {
return 'نامشخص';
}
};
export const formatPrice = (price: number | null): string => {
if (price === null || price === undefined) return '0';
return new Intl.NumberFormat('fa-IR').format(price);
};
export const getFullName = (user: User): string => {
if (user.full_name) return user.full_name;
const firstName = user.first_name || '';
const lastName = user.last_name || '';
if (firstName || lastName) {
return `${firstName} ${lastName}`.trim();
}
return 'نامشخص';
};
export const getEmail = (email: string | null): string => {
return email || 'ایمیل ثبت نشده';
};
export const getMobile = (mobile: string | null): string => {
return mobile || 'شماره موبایل ثبت نشده';
};
export const formatTime = (dateString: string | null): string => {
if (!dateString) return '';
try {
const date = new Date(dateString);
return new Intl.DateTimeFormat('fa-IR', {
hour: '2-digit',
minute: '2-digit',
}).format(date);
} catch {
return '';
}
};