From 1409290353489db42413ef5f1ce4b515145eb98b Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Wed, 19 Aug 2026 12:38:22 +0330 Subject: [PATCH] fix --- app/admin/auth-analytics/page.tsx | 53 +++++++++++++++++++------------ 1 file changed, 32 insertions(+), 21 deletions(-) diff --git a/app/admin/auth-analytics/page.tsx b/app/admin/auth-analytics/page.tsx index 9b0c072..7ad70e6 100644 --- a/app/admin/auth-analytics/page.tsx +++ b/app/admin/auth-analytics/page.tsx @@ -29,16 +29,25 @@ import { // Default price of one Kavenegar verify SMS, in Toman. Only used to price the // SMS total in the panel — the admin can change it and the choice is remembered. -const DEFAULT_SMS_UNIT_COST = 800; +const DEFAULT_SMS_UNIT_COST = 2000; const SMS_COST_STORAGE_KEY = 'approagency:sms_unit_cost'; -const emptyFilters: AuthLogFilters = { +// Today as the Gregorian YYYY-MM-DD the API and PersianDatePicker exchange. +const todayValue = (): string => { + const now = new Date(); + const month = String(now.getMonth() + 1).padStart(2, '0'); + const day = String(now.getDate()).padStart(2, '0'); + return `${now.getFullYear()}-${month}-${day}`; +}; + +// The section opens on today's activity; widen the range with the date pickers. +const createDefaultFilters = (): AuthLogFilters => ({ event: '', mobile: '', package_name: '', - date_from: '', - date_to: '', -}; + date_from: todayValue(), + date_to: todayValue(), +}); const eventBadgeClass = (event: string, success: boolean): string => { if (!success) return 'bg-red-100 text-red-800 dark:bg-red-900/30 dark:text-red-300'; @@ -51,23 +60,15 @@ const eventBadgeClass = (event: string, success: boolean): string => { export default function AuthAnalyticsPage() { const { token } = useAuth(); const [packages, setPackages] = useState([]); - const [filters, setFilters] = useState(emptyFilters); + const [filters, setFilters] = useState(createDefaultFilters); const [smsUnitCost, setSmsUnitCost] = useState(DEFAULT_SMS_UNIT_COST); + const [initialized, setInitialized] = useState(false); const [stats, setStats] = useState(null); const [logs, setLogs] = useState(null); const [page, setPage] = useState(1); const [isLoading, setIsLoading] = useState(false); const [error, setError] = useState(null); - // Restore the last SMS unit price the admin entered - useEffect(() => { - const saved = window.localStorage.getItem(SMS_COST_STORAGE_KEY); - if (saved) { - const parsed = parseInt(saved, 10); - if (!Number.isNaN(parsed) && parsed >= 0) setSmsUnitCost(parsed); - } - }, []); - const load = useCallback(async ( targetPage: number, activeFilters: AuthLogFilters, @@ -95,10 +96,19 @@ export default function AuthAnalyticsPage() { packagesApi.getAllPackages(token).then(setPackages).catch(() => setPackages([])); }, [token]); + // First load: restore the last SMS unit price the admin entered, then fetch + // today's activity with it (so the cost card is right on the very first render). useEffect(() => { - if (!token) return; - load(1, emptyFilters, DEFAULT_SMS_UNIT_COST); - }, [token, load]); + if (!token || initialized) return; + + const saved = window.localStorage.getItem(SMS_COST_STORAGE_KEY); + const parsed = saved ? parseInt(saved, 10) : NaN; + const unitCost = !Number.isNaN(parsed) && parsed >= 0 ? parsed : DEFAULT_SMS_UNIT_COST; + + setSmsUnitCost(unitCost); + setInitialized(true); + load(1, createDefaultFilters(), unitCost); + }, [token, initialized, load]); const handleInputChange = (e: React.ChangeEvent) => { const { name, value } = e.target; @@ -119,11 +129,12 @@ export default function AuthAnalyticsPage() { }; const handleReset = () => { - setFilters(emptyFilters); + const defaults = createDefaultFilters(); + setFilters(defaults); setSmsUnitCost(DEFAULT_SMS_UNIT_COST); window.localStorage.setItem(SMS_COST_STORAGE_KEY, String(DEFAULT_SMS_UNIT_COST)); setPage(1); - load(1, emptyFilters, DEFAULT_SMS_UNIT_COST); + load(1, defaults, DEFAULT_SMS_UNIT_COST); }; const goToPage = (targetPage: number) => { @@ -277,7 +288,7 @@ export default function AuthAnalyticsPage() { value={smsUnitCost ? formatPrice(smsUnitCost) : ''} onChange={handleUnitCostChange} className={inputClass} - placeholder="۸۰۰" + placeholder="۲۰۰۰" />