feat: response in mobile and accouting
This commit is contained in:
+248
-75
@@ -1,6 +1,7 @@
|
||||
'use client';
|
||||
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import type { ComponentType } from 'react';
|
||||
import { useAuth } from '@/contexts/AuthContext';
|
||||
import { purchasesApi } from '@/lib/api/purchases';
|
||||
import { packagesApi } from '@/lib/api/packages';
|
||||
@@ -25,6 +26,51 @@ import {
|
||||
// بنابراین هر دو وضعیت باید بهعنوان فروش محسوب شوند.
|
||||
const PAID_STATUSES = [2, 3];
|
||||
|
||||
// Local-time Gregorian helpers. The "YYYY-MM-DD" format matches what
|
||||
// PersianDatePicker exchanges and what the created_at comparison expects.
|
||||
const toGregorianValue = (date: Date): string => {
|
||||
const year = date.getFullYear();
|
||||
const month = String(date.getMonth() + 1).padStart(2, '0');
|
||||
const day = String(date.getDate()).padStart(2, '0');
|
||||
return `${year}-${month}-${day}`;
|
||||
};
|
||||
|
||||
const addDays = (date: Date, days: number): Date => {
|
||||
const copy = new Date(date);
|
||||
copy.setDate(copy.getDate() + days);
|
||||
return copy;
|
||||
};
|
||||
|
||||
const todayValue = (): string => toGregorianValue(new Date());
|
||||
|
||||
type DatePreset = 'today' | 'yesterday' | 'last7' | 'thisMonth';
|
||||
|
||||
const presetRange = (preset: DatePreset): { from: string; to: string } => {
|
||||
const now = new Date();
|
||||
switch (preset) {
|
||||
case 'today':
|
||||
return { from: toGregorianValue(now), to: toGregorianValue(now) };
|
||||
case 'yesterday': {
|
||||
const yesterday = addDays(now, -1);
|
||||
return { from: toGregorianValue(yesterday), to: toGregorianValue(yesterday) };
|
||||
}
|
||||
case 'last7':
|
||||
return { from: toGregorianValue(addDays(now, -6)), to: toGregorianValue(now) };
|
||||
case 'thisMonth':
|
||||
return {
|
||||
from: toGregorianValue(new Date(now.getFullYear(), now.getMonth(), 1)),
|
||||
to: toGregorianValue(now),
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
const PRESET_LABELS: { key: DatePreset; label: string }[] = [
|
||||
{ key: 'today', label: 'امروز' },
|
||||
{ key: 'yesterday', label: 'دیروز' },
|
||||
{ key: 'last7', label: '۷ روز اخیر' },
|
||||
{ key: 'thisMonth', label: 'این ماه' },
|
||||
];
|
||||
|
||||
interface StoreSummary {
|
||||
count: number;
|
||||
gross: number;
|
||||
@@ -166,9 +212,10 @@ export default function AccountingPage() {
|
||||
const [products, setProducts] = useState<Product[]>([]);
|
||||
const [selectedProductId, setSelectedProductId] = useState<string>('');
|
||||
|
||||
// Date range
|
||||
const [dateFrom, setDateFrom] = useState<string>('');
|
||||
const [dateTo, setDateTo] = useState<string>('');
|
||||
// Date range — defaults to today so the daily account is the first thing shown
|
||||
const [dateFrom, setDateFrom] = useState<string>(todayValue);
|
||||
const [dateTo, setDateTo] = useState<string>(todayValue);
|
||||
const [preset, setPreset] = useState<DatePreset | null>('today');
|
||||
|
||||
// Time range
|
||||
const [timeFrom, setTimeFrom] = useState<string>('');
|
||||
@@ -187,20 +234,20 @@ export default function AccountingPage() {
|
||||
const [result, setResult] = useState<AccountingResult | null>(null);
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
// Bumped by the «تحلیل» button / «بارگذاری مجدد» to force a refetch
|
||||
const [refreshTick, setRefreshTick] = useState(0);
|
||||
|
||||
const inputClass =
|
||||
'block w-full px-3 py-2.5 text-gray-900 dark:text-gray-100 border border-gray-200 dark:border-gray-700 rounded-xl shadow-sm focus:ring-2 focus:ring-indigo-500 focus:border-transparent sm:text-sm dark:bg-gray-800/80 transition-colors';
|
||||
const labelClass = 'block text-sm font-medium text-gray-700 dark:text-gray-300 mb-1.5';
|
||||
|
||||
// Load packages once on mount
|
||||
// Load packages once on mount — "همه پکیجها" stays selected by default so
|
||||
// the initial view covers the whole business.
|
||||
useEffect(() => {
|
||||
if (!token) return;
|
||||
packagesApi
|
||||
.getAllPackages(token)
|
||||
.then((data) => {
|
||||
setPackages(data);
|
||||
if (data.length > 0) setSelectedPackageName(data[0].name || '');
|
||||
})
|
||||
.then(setPackages)
|
||||
.catch(() => setError('خطا در دریافت لیست پکیجها'));
|
||||
}, [token]);
|
||||
|
||||
@@ -218,28 +265,61 @@ export default function AccountingPage() {
|
||||
.catch(() => setProducts([]));
|
||||
}, [selectedPackageName, token]);
|
||||
|
||||
const handleAnalyze = useCallback(async () => {
|
||||
// The server-side query window. Fetching only the selected window keeps the
|
||||
// payload small; the backend filters created_at by these dates. Runs
|
||||
// automatically on mount (today's account) and whenever the window,
|
||||
// package or product selection changes.
|
||||
const windowKey = [dateFrom, dateTo, timeFrom, timeTo, selectedPackageName, selectedProductId].join('|');
|
||||
|
||||
useEffect(() => {
|
||||
if (!token) return;
|
||||
let cancelled = false;
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
try {
|
||||
// Fetch all purchases once, then filter/aggregate in-memory.
|
||||
const purchases = await purchasesApi.getAllPurchases({ per_page: 500 }, token);
|
||||
setAllPurchases(purchases);
|
||||
setResult(
|
||||
computeAccounting(
|
||||
purchases,
|
||||
parseStoreRate(myketRate),
|
||||
parseStoreRate(cafeRate),
|
||||
parseFee(transactionFee)
|
||||
)
|
||||
);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'خطا در دریافت دادهها');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [token, myketRate, cafeRate, transactionFee]);
|
||||
|
||||
purchasesApi
|
||||
.getAllPurchases(
|
||||
{
|
||||
per_page: 500,
|
||||
...(dateFrom && { date_from: dateFrom }),
|
||||
...(dateTo && { date_to: dateTo }),
|
||||
...(timeFrom && { time_from: timeFrom }),
|
||||
...(timeTo && { time_to: timeTo }),
|
||||
...(selectedPackageName && { package_name: selectedPackageName }),
|
||||
...(selectedProductId && { product_id: Number(selectedProductId) }),
|
||||
},
|
||||
token
|
||||
)
|
||||
.then((purchases) => {
|
||||
if (cancelled) return;
|
||||
setAllPurchases(purchases);
|
||||
setResult(
|
||||
computeAccounting(
|
||||
purchases,
|
||||
parseStoreRate(myketRate),
|
||||
parseStoreRate(cafeRate),
|
||||
parseFee(transactionFee)
|
||||
)
|
||||
);
|
||||
})
|
||||
.catch((err) => {
|
||||
if (!cancelled) {
|
||||
setError(err instanceof Error ? err.message : 'خطا در دریافت دادهها');
|
||||
}
|
||||
})
|
||||
.finally(() => {
|
||||
if (!cancelled) setIsLoading(false);
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
// Commission rates are read at fetch time only; changing them recomputes
|
||||
// locally (see runAnalysis) without refetching.
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [token, windowKey, refreshTick]);
|
||||
|
||||
const handleAnalyze = () => setRefreshTick((t) => t + 1);
|
||||
|
||||
// Recompute whenever filters or commission rates change (uses cached data)
|
||||
const runAnalysis = useCallback(() => {
|
||||
@@ -291,13 +371,29 @@ export default function AccountingPage() {
|
||||
if (allPurchases) runAnalysis();
|
||||
}, [runAnalysis, allPurchases]);
|
||||
|
||||
const handleReset = () => {
|
||||
setSelectedPackageName(packages[0]?.name || '');
|
||||
setSelectedProductId('');
|
||||
setDateFrom('');
|
||||
setDateTo('');
|
||||
const applyPreset = (key: DatePreset) => {
|
||||
const range = presetRange(key);
|
||||
setPreset(key);
|
||||
setDateFrom(range.from);
|
||||
setDateTo(range.to);
|
||||
setTimeFrom('');
|
||||
setTimeTo('');
|
||||
};
|
||||
|
||||
const handleDateFromChange = (value: string) => {
|
||||
setPreset(null);
|
||||
setDateFrom(value);
|
||||
};
|
||||
|
||||
const handleDateToChange = (value: string) => {
|
||||
setPreset(null);
|
||||
setDateTo(value);
|
||||
};
|
||||
|
||||
const handleReset = () => {
|
||||
setSelectedPackageName('');
|
||||
setSelectedProductId('');
|
||||
applyPreset('today');
|
||||
setStatuses([...PAID_STATUSES]);
|
||||
setMyketRate('85');
|
||||
setCafeRate('85');
|
||||
@@ -310,11 +406,6 @@ export default function AccountingPage() {
|
||||
);
|
||||
};
|
||||
|
||||
const statCardClass =
|
||||
'rounded-2xl bg-white dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-800 p-5 shadow-sm';
|
||||
const statIconClass = 'h-5 w-5';
|
||||
const statValueClass = 'text-2xl font-bold text-gray-900 dark:text-gray-100 mt-1.5';
|
||||
|
||||
return (
|
||||
<div className="p-4 sm:p-8">
|
||||
<div className="max-w-7xl mx-auto">
|
||||
@@ -384,7 +475,7 @@ export default function AccountingPage() {
|
||||
label="از تاریخ"
|
||||
placeholder="انتخاب تاریخ شروع"
|
||||
value={dateFrom}
|
||||
onChange={setDateFrom}
|
||||
onChange={handleDateFromChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -406,7 +497,7 @@ export default function AccountingPage() {
|
||||
label="تا تاریخ"
|
||||
placeholder="انتخاب تاریخ پایان"
|
||||
value={dateTo}
|
||||
onChange={setDateTo}
|
||||
onChange={handleDateToChange}
|
||||
/>
|
||||
</div>
|
||||
|
||||
@@ -423,6 +514,30 @@ export default function AccountingPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Quick date-range presets */}
|
||||
<div className="mt-4">
|
||||
<label className={labelClass}>بازه زمانی</label>
|
||||
<div className="flex flex-wrap gap-2">
|
||||
{PRESET_LABELS.map(({ key, label }) => {
|
||||
const active = preset === key;
|
||||
return (
|
||||
<button
|
||||
key={key}
|
||||
type="button"
|
||||
onClick={() => applyPreset(key)}
|
||||
className={`px-3.5 py-1.5 rounded-xl text-sm font-medium transition-colors ${
|
||||
active
|
||||
? 'bg-indigo-600 text-white shadow-sm shadow-indigo-500/20'
|
||||
: 'bg-gray-50 dark:bg-gray-800 text-gray-600 dark:text-gray-300 ring-1 ring-gray-200 dark:ring-gray-700 hover:bg-gray-100 dark:hover:bg-gray-700'
|
||||
}`}
|
||||
>
|
||||
{label}
|
||||
</button>
|
||||
);
|
||||
})}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Status checkboxes */}
|
||||
<div className="mt-4">
|
||||
<label className={labelClass}>وضعیت</label>
|
||||
@@ -495,7 +610,7 @@ export default function AccountingPage() {
|
||||
کافهبازار ۸۵٪ (فروش زیر ۱۰ میلیارد ریال) و ۷۰٪ (بالای آن) است.
|
||||
</p>
|
||||
|
||||
<div className="flex items-center gap-3 mt-5">
|
||||
<div className="flex flex-wrap items-center gap-3 mt-5">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
@@ -537,38 +652,10 @@ export default function AccountingPage() {
|
||||
<>
|
||||
{/* Summary stat cards */}
|
||||
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4 mb-6">
|
||||
<div className={statCardClass}>
|
||||
<div className="flex items-center gap-2 text-sm font-medium text-gray-500 dark:text-gray-400">
|
||||
<WalletIcon className={statIconClass} />
|
||||
تعداد فروش
|
||||
</div>
|
||||
<div className={statValueClass}>{formatPrice(result.totals.count)}</div>
|
||||
</div>
|
||||
<div className={statCardClass}>
|
||||
<div className="flex items-center gap-2 text-sm font-medium text-gray-500 dark:text-gray-400">
|
||||
<BanknotesIcon className={statIconClass} />
|
||||
فروش ناخالص
|
||||
</div>
|
||||
<div className={statValueClass}>{formatPrice(result.totals.gross)} <span className="text-sm font-normal text-gray-400">تومان</span></div>
|
||||
</div>
|
||||
<div className={statCardClass}>
|
||||
<div className="flex items-center gap-2 text-sm font-medium text-gray-500 dark:text-gray-400">
|
||||
<ChartPieIcon className={statIconClass} />
|
||||
کارمزد فروشگاهها
|
||||
</div>
|
||||
<div className={`${statValueClass} text-amber-600 dark:text-amber-400`}>
|
||||
{formatPrice(result.totals.commission)} <span className="text-sm font-normal text-gray-400">تومان</span>
|
||||
</div>
|
||||
</div>
|
||||
<div className={statCardClass}>
|
||||
<div className="flex items-center gap-2 text-sm font-medium text-gray-500 dark:text-gray-400">
|
||||
<CalculatorIcon className={statIconClass} />
|
||||
سود خالص
|
||||
</div>
|
||||
<div className={`${statValueClass} text-emerald-600 dark:text-emerald-400`}>
|
||||
{formatPrice(result.totals.net)} <span className="text-sm font-normal text-gray-400">تومان</span>
|
||||
</div>
|
||||
</div>
|
||||
<StatCard label="تعداد فروش" value={formatPrice(result.totals.count)} Icon={WalletIcon} tone="indigo" />
|
||||
<StatCard label="فروش ناخالص" value={formatPrice(result.totals.gross)} unit="تومان" Icon={BanknotesIcon} tone="sky" />
|
||||
<StatCard label="کارمزد فروشگاهها" value={formatPrice(result.totals.commission)} unit="تومان" Icon={ChartPieIcon} tone="amber" />
|
||||
<StatCard label="سود خالص" value={formatPrice(result.totals.net)} unit="تومان" Icon={CalculatorIcon} tone="emerald" />
|
||||
</div>
|
||||
|
||||
{/* Per-store breakdown */}
|
||||
@@ -593,8 +680,51 @@ export default function AccountingPage() {
|
||||
</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-800">
|
||||
<>
|
||||
{/* Mobile: stacked cards */}
|
||||
<ul className="divide-y divide-gray-200 dark:divide-gray-800 md:hidden">
|
||||
{result.rows.map((row) => (
|
||||
<li key={row.productId} className="px-5 py-4">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate">
|
||||
{row.title}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5 truncate">
|
||||
{row.packageName}
|
||||
</p>
|
||||
</div>
|
||||
<span className="flex-shrink-0 px-2.5 py-1 text-xs font-semibold rounded-lg bg-indigo-50 text-indigo-700 dark:bg-indigo-500/10 dark:text-indigo-400">
|
||||
{formatPrice(row.count)} فروش
|
||||
</span>
|
||||
</div>
|
||||
<div className="grid grid-cols-3 gap-2 mt-3 pt-3 border-t border-gray-100 dark:border-gray-800/60">
|
||||
<div>
|
||||
<p className="text-[11px] text-gray-400 dark:text-gray-500">فروش ناخالص</p>
|
||||
<p className="text-xs font-bold text-gray-900 dark:text-gray-100 mt-0.5">
|
||||
{formatPrice(row.gross)}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-[11px] text-gray-400 dark:text-gray-500">کارمزد</p>
|
||||
<p className="text-xs font-bold text-amber-600 dark:text-amber-400 mt-0.5">
|
||||
{formatPrice(row.commission)}
|
||||
</p>
|
||||
</div>
|
||||
<div>
|
||||
<p className="text-[11px] text-gray-400 dark:text-gray-500">سود خالص</p>
|
||||
<p className="text-xs font-bold text-emerald-600 dark:text-emerald-400 mt-0.5">
|
||||
{formatPrice(row.net)}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
{/* Desktop: table */}
|
||||
<div className="hidden md:block overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-800">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800/60">
|
||||
<tr className="text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
<th className="px-4 py-3">محصول</th>
|
||||
@@ -630,7 +760,8 @@ export default function AccountingPage() {
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</div>
|
||||
</>
|
||||
@@ -651,6 +782,48 @@ export default function AccountingPage() {
|
||||
);
|
||||
}
|
||||
|
||||
const STAT_TONES = {
|
||||
indigo: 'bg-indigo-50 text-indigo-600 dark:bg-indigo-500/10 dark:text-indigo-400',
|
||||
sky: 'bg-sky-50 text-sky-600 dark:bg-sky-500/10 dark:text-sky-400',
|
||||
amber: 'bg-amber-50 text-amber-600 dark:bg-amber-500/10 dark:text-amber-400',
|
||||
emerald: 'bg-emerald-50 text-emerald-600 dark:bg-emerald-500/10 dark:text-emerald-400',
|
||||
} as const;
|
||||
|
||||
function StatCard({
|
||||
label,
|
||||
value,
|
||||
unit,
|
||||
Icon,
|
||||
tone,
|
||||
}: {
|
||||
label: string;
|
||||
value: string;
|
||||
unit?: string;
|
||||
Icon: ComponentType<{ className?: string }>;
|
||||
tone: keyof typeof STAT_TONES;
|
||||
}) {
|
||||
const valueColor =
|
||||
tone === 'amber'
|
||||
? 'text-amber-600 dark:text-amber-400'
|
||||
: tone === 'emerald'
|
||||
? 'text-emerald-600 dark:text-emerald-400'
|
||||
: 'text-gray-900 dark:text-gray-100';
|
||||
|
||||
return (
|
||||
<div className="rounded-2xl bg-white dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-800 p-5 shadow-sm">
|
||||
<div className="flex items-center gap-2.5">
|
||||
<span className={`h-9 w-9 rounded-xl flex items-center justify-center flex-shrink-0 ${STAT_TONES[tone]}`}>
|
||||
<Icon className="h-5 w-5" />
|
||||
</span>
|
||||
<span className="text-sm font-medium text-gray-500 dark:text-gray-400">{label}</span>
|
||||
</div>
|
||||
<div className={`text-2xl font-bold mt-2.5 ${valueColor}`}>
|
||||
{value} {unit && <span className="text-sm font-normal text-gray-400">{unit}</span>}
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
function StoreCard({ title, summary }: { title: string; summary: StoreSummary }) {
|
||||
return (
|
||||
<div className="bg-white dark:bg-gray-900 ring-1 ring-gray-200 dark:ring-gray-800 rounded-2xl p-5 shadow-sm">
|
||||
|
||||
@@ -113,7 +113,7 @@ export default function Dashboard() {
|
||||
<p className="text-sm text-gray-500 dark:text-gray-400 mt-1.5 leading-relaxed">
|
||||
{desc}
|
||||
</p>
|
||||
<div className="mt-4 flex items-center text-xs font-medium text-indigo-600 dark:text-indigo-400 opacity-0 group-hover:opacity-100 transition-opacity">
|
||||
<div className="mt-4 flex items-center text-xs font-medium text-indigo-600 dark:text-indigo-400 opacity-100 md:opacity-0 md:group-hover:opacity-100 transition-opacity">
|
||||
ورود به بخش
|
||||
<svg className="h-4 w-4 mr-1 rotate-180" fill="none" viewBox="0 0 24 24" strokeWidth={2} stroke="currentColor">
|
||||
<path strokeLinecap="round" strokeLinejoin="round" d="M13.5 4.5 21 12m0 0-7.5 7.5M21 12H3" />
|
||||
|
||||
@@ -23,7 +23,9 @@ export default function AdminLayout({
|
||||
) : (
|
||||
<AuthGuard>
|
||||
<Sidebar />
|
||||
<main className="lg:pr-64 min-h-screen">
|
||||
{/* Extra top padding on mobile so content clears the
|
||||
fixed hamburger button; the desktop sidebar needs none. */}
|
||||
<main className="lg:pr-64 min-h-screen pt-16 lg:pt-0">
|
||||
{children}
|
||||
</main>
|
||||
</AuthGuard>
|
||||
|
||||
@@ -6,6 +6,7 @@ import { packagesApi } from '@/lib/api/packages';
|
||||
import PackageList from '@/components/admin/packages/PackageList';
|
||||
import PackageForm from '@/components/admin/packages/PackageForm';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { PackageName, CreatePackageData, UpdatePackageData } from '@/types/package';
|
||||
import { PlusIcon } from '@heroicons/react/24/outline';
|
||||
@@ -17,6 +18,7 @@ export default function PackagesPage() {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isFormVisible, setIsFormVisible] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<PackageName | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
if (token) {
|
||||
@@ -47,10 +49,13 @@ export default function PackagesPage() {
|
||||
setIsFormVisible(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (pkg: PackageName) => {
|
||||
if (!confirm(`آیا از حذف پکیج "${pkg.title}" اطمینان دارید؟`)) {
|
||||
return;
|
||||
}
|
||||
const handleDelete = (pkg: PackageName) => {
|
||||
setDeleteTarget(pkg);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (!deleteTarget) return;
|
||||
const pkg = deleteTarget;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -63,6 +68,7 @@ export default function PackagesPage() {
|
||||
setError(err instanceof Error ? err.message : 'خطا در حذف پکیج');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setDeleteTarget(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -139,6 +145,16 @@ export default function PackagesPage() {
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleteTarget}
|
||||
title="حذف پکیج"
|
||||
message={`آیا از حذف پکیج «${deleteTarget?.title || deleteTarget?.name || ''}» اطمینان دارید؟`}
|
||||
confirmLabel="حذف"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTarget(null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -7,6 +7,7 @@ import { productsApi } from '@/lib/api/products';
|
||||
import ProductList from '@/components/admin/products/ProductList';
|
||||
import ProductForm from '@/components/admin/products/ProductForm';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { PackageName } from '@/types/package';
|
||||
import { Product, CreateProductData, UpdateProductData } from '@/types/product';
|
||||
@@ -22,6 +23,7 @@ export default function ProductsPage() {
|
||||
const [isLoadingPackages, setIsLoadingPackages] = useState(false);
|
||||
const [isFormVisible, setIsFormVisible] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<Product | null>(null);
|
||||
|
||||
const initialLoadRef = useRef(false);
|
||||
const prevPackageRef = useRef<string | null>(null);
|
||||
@@ -94,11 +96,13 @@ export default function ProductsPage() {
|
||||
setIsFormVisible(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (product: Product) => {
|
||||
if (!selectedPackage) return;
|
||||
if (!confirm(`آیا از حذف محصول "${product.title}" اطمینان دارید؟`)) {
|
||||
return;
|
||||
}
|
||||
const handleDelete = (product: Product) => {
|
||||
setDeleteTarget(product);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (!selectedPackage || !deleteTarget) return;
|
||||
const product = deleteTarget;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -114,6 +118,7 @@ export default function ProductsPage() {
|
||||
setError(err instanceof Error ? err.message : 'خطا در حذف محصول');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setDeleteTarget(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -226,6 +231,16 @@ export default function ProductsPage() {
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleteTarget}
|
||||
title="حذف محصول"
|
||||
message={`آیا از حذف محصول «${deleteTarget?.title || ''}» اطمینان دارید؟`}
|
||||
confirmLabel="حذف"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTarget(null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -6,6 +6,7 @@ import { promotionsApi } from '@/lib/api/promotions';
|
||||
import PromotionList from '@/components/admin/promotions/PromotionList';
|
||||
import PromotionForm from '@/components/admin/promotions/PromotionForm';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { Promotion, CreatePromotionData, UpdatePromotionData } from '@/types/promotion';
|
||||
import { PlusIcon } from '@heroicons/react/24/outline';
|
||||
@@ -17,6 +18,7 @@ export default function PromotionsPage() {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [isFormVisible, setIsFormVisible] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<Promotion | null>(null);
|
||||
|
||||
const initialLoadRef = useRef(false);
|
||||
|
||||
@@ -50,10 +52,13 @@ export default function PromotionsPage() {
|
||||
setIsFormVisible(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (promotion: Promotion) => {
|
||||
if (!confirm(`آیا از حذف تبلیغ "${promotion.title || 'بدون عنوان'}" اطمینان دارید؟`)) {
|
||||
return;
|
||||
}
|
||||
const handleDelete = (promotion: Promotion) => {
|
||||
setDeleteTarget(promotion);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (!deleteTarget) return;
|
||||
const promotion = deleteTarget;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -66,6 +71,7 @@ export default function PromotionsPage() {
|
||||
setError(err instanceof Error ? err.message : 'خطا در حذف تبلیغ');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setDeleteTarget(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -159,6 +165,16 @@ export default function PromotionsPage() {
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleteTarget}
|
||||
title="حذف تبلیغ"
|
||||
message={`آیا از حذف تبلیغ «${deleteTarget?.title || 'بدون عنوان'}» اطمینان دارید؟`}
|
||||
confirmLabel="حذف"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTarget(null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -148,7 +148,7 @@ export default function PurchasesPage() {
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center gap-3 mt-5">
|
||||
<div className="flex flex-wrap items-center gap-3 mt-5">
|
||||
<button
|
||||
type="submit"
|
||||
disabled={isLoading}
|
||||
@@ -202,7 +202,55 @@ export default function PurchasesPage() {
|
||||
<p className="text-gray-500 dark:text-gray-400 text-sm">هیچ خریدی با این فیلترها یافت نشد</p>
|
||||
</div>
|
||||
) : (
|
||||
<div className="overflow-x-auto">
|
||||
<>
|
||||
{/* Mobile: stacked cards */}
|
||||
<ul className="divide-y divide-gray-200 dark:divide-gray-800 md:hidden">
|
||||
{result.data.map((purchase) => (
|
||||
<li key={purchase.id} className="px-5 py-4 space-y-2.5">
|
||||
<div className="flex items-start justify-between gap-3">
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm font-semibold text-gray-900 dark:text-gray-100 truncate">
|
||||
{purchase.product?.title || '—'}
|
||||
</p>
|
||||
<p className="text-xs text-gray-500 dark:text-gray-400 mt-0.5 truncate">
|
||||
{purchase.product?.package_name?.title || purchase.product?.package_name?.name || '—'}
|
||||
{purchase.product ? ` • ${getProductTypeLabel(purchase.product.type)}` : ''}
|
||||
</p>
|
||||
</div>
|
||||
<span className={`flex-shrink-0 px-2.5 py-1 inline-flex text-xs leading-5 font-semibold rounded-lg ${statusBadgeClass(purchase.status)}`}>
|
||||
{getPurchaseStatusLabel(purchase.status)}
|
||||
</span>
|
||||
</div>
|
||||
<div className="text-xs text-gray-500 dark:text-gray-400 space-y-0.5">
|
||||
<p className="font-medium text-gray-700 dark:text-gray-300">
|
||||
{purchase.user?.full_name?.trim() || 'بدون نام'}
|
||||
</p>
|
||||
{purchase.user?.email && (
|
||||
<p dir="ltr" className="text-right truncate">{purchase.user.email}</p>
|
||||
)}
|
||||
{purchase.user?.mobile && (
|
||||
<p dir="ltr" className="text-right truncate">{purchase.user.mobile}</p>
|
||||
)}
|
||||
</div>
|
||||
<div className="flex items-center justify-between gap-3 pt-2.5 border-t border-gray-100 dark:border-gray-800/60">
|
||||
<span className="text-sm font-bold text-gray-900 dark:text-gray-100">
|
||||
{formatPrice(getPurchaseFinalPrice(purchase))} تومان
|
||||
</span>
|
||||
<div className="flex flex-col items-end gap-1">
|
||||
<span className="px-2 py-0.5 text-[11px] font-medium rounded-md bg-gray-100 text-gray-600 dark:bg-gray-800 dark:text-gray-300">
|
||||
{getGatewayLabel(purchase.gateway)}
|
||||
</span>
|
||||
<span className="text-[11px] text-gray-400 dark:text-gray-500">
|
||||
{formatDate(purchase.created_at)} {formatTime(purchase.created_at)}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
</li>
|
||||
))}
|
||||
</ul>
|
||||
|
||||
{/* Desktop: table */}
|
||||
<div className="hidden md:block overflow-x-auto">
|
||||
<table className="min-w-full divide-y divide-gray-200 dark:divide-gray-800">
|
||||
<thead className="bg-gray-50 dark:bg-gray-800/60">
|
||||
<tr className="text-right text-xs font-medium text-gray-500 dark:text-gray-400 uppercase tracking-wider">
|
||||
@@ -260,7 +308,8 @@ export default function PurchasesPage() {
|
||||
))}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
|
||||
{/* Pagination */}
|
||||
|
||||
@@ -5,6 +5,7 @@ import { useAuth } from '@/contexts/AuthContext';
|
||||
import { referralApi } from '@/lib/api/referral';
|
||||
import ReferralList from '@/components/admin/referral/ReferralList';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { ReferralRewardUser } from '@/types/referral';
|
||||
import { ArrowPathIcon } from '@heroicons/react/24/outline';
|
||||
@@ -16,6 +17,7 @@ export default function ReferralRewardsPage() {
|
||||
const [isLoading, setIsLoading] = useState(false);
|
||||
const [fulfillingId, setFulfillingId] = useState<number | null>(null);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [fulfillTarget, setFulfillTarget] = useState<ReferralRewardUser | null>(null);
|
||||
|
||||
const initialLoadRef = useRef(false);
|
||||
|
||||
@@ -40,10 +42,13 @@ export default function ReferralRewardsPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleFulfill = async (user: ReferralRewardUser) => {
|
||||
if (!confirm(`آیا از اعطای یک ماه اشتراک رایگان به «${user.name || user.mobile || user.email}» اطمینان دارید؟`)) {
|
||||
return;
|
||||
}
|
||||
const handleFulfill = (user: ReferralRewardUser) => {
|
||||
setFulfillTarget(user);
|
||||
};
|
||||
|
||||
const confirmFulfill = async () => {
|
||||
if (!fulfillTarget) return;
|
||||
const user = fulfillTarget;
|
||||
|
||||
setFulfillingId(user.id);
|
||||
setIsLoading(true);
|
||||
@@ -58,6 +63,7 @@ export default function ReferralRewardsPage() {
|
||||
} finally {
|
||||
setFulfillingId(null);
|
||||
setIsLoading(false);
|
||||
setFulfillTarget(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -104,6 +110,17 @@ export default function ReferralRewardsPage() {
|
||||
fulfillingId={fulfillingId}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!fulfillTarget}
|
||||
title="اعطای پاداش معرفی"
|
||||
message={`آیا از اعطای یک ماه اشتراک رایگان به «${fulfillTarget?.name || fulfillTarget?.mobile || fulfillTarget?.email || ''}» اطمینان دارید؟`}
|
||||
confirmLabel="اعطای اشتراک"
|
||||
tone="primary"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmFulfill}
|
||||
onCancel={() => setFulfillTarget(null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -7,6 +7,7 @@ import { remindersApi } from '@/lib/api/reminders';
|
||||
import ReminderList from '@/components/admin/reminders/ReminderList';
|
||||
import ReminderForm from '@/components/admin/reminders/ReminderForm';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { PackageName } from '@/types/package';
|
||||
import { Reminder, CreateReminderData, UpdateReminderData } from '@/types/reminder';
|
||||
@@ -22,6 +23,7 @@ export default function RemindersPage() {
|
||||
const [isLoadingPackages, setIsLoadingPackages] = useState(false);
|
||||
const [isFormVisible, setIsFormVisible] = useState(false);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
const [deleteTarget, setDeleteTarget] = useState<Reminder | null>(null);
|
||||
|
||||
const initialLoadRef = useRef(false);
|
||||
const prevPackageRef = useRef<string | null>(null);
|
||||
@@ -94,11 +96,13 @@ export default function RemindersPage() {
|
||||
setIsFormVisible(true);
|
||||
};
|
||||
|
||||
const handleDelete = async (reminder: Reminder) => {
|
||||
if (!selectedPackage) return;
|
||||
if (!confirm(`آیا از حذف یادآوری "${reminder.title}" اطمینان دارید؟`)) {
|
||||
return;
|
||||
}
|
||||
const handleDelete = (reminder: Reminder) => {
|
||||
setDeleteTarget(reminder);
|
||||
};
|
||||
|
||||
const confirmDelete = async () => {
|
||||
if (!selectedPackage || !deleteTarget) return;
|
||||
const reminder = deleteTarget;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -114,6 +118,7 @@ export default function RemindersPage() {
|
||||
setError(err instanceof Error ? err.message : 'خطا در حذف یادآوری');
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setDeleteTarget(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -226,6 +231,16 @@ export default function RemindersPage() {
|
||||
)}
|
||||
</>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={!!deleteTarget}
|
||||
title="حذف یادآوری"
|
||||
message={`آیا از حذف یادآوری «${deleteTarget?.title || ''}» اطمینان دارید؟`}
|
||||
confirmLabel="حذف"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTarget(null)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
@@ -11,6 +11,7 @@ import UserEditForm from '@/components/admin/users/UserEditForm';
|
||||
import PackageSelector from '@/components/admin/users/PackageSelector';
|
||||
import PageHeader from '@/components/admin/PageHeader';
|
||||
import StepIndicator from '@/components/admin/StepIndicator';
|
||||
import ConfirmDialog from '@/components/admin/ConfirmDialog';
|
||||
import { showToast } from '@/components/Toast';
|
||||
import { User, Product, UpdateUserProfileData } from '@/types/user';
|
||||
import { PackageName } from '@/types/package';
|
||||
@@ -159,7 +160,9 @@ export default function UsersPage() {
|
||||
}
|
||||
};
|
||||
|
||||
const handleDeleteUser = async () => {
|
||||
const [isDeleteConfirmOpen, setIsDeleteConfirmOpen] = useState(false);
|
||||
|
||||
const handleDeleteUser = () => {
|
||||
if (!user) return;
|
||||
const identifier = user.email || user.mobile;
|
||||
if (!identifier) {
|
||||
@@ -167,9 +170,13 @@ export default function UsersPage() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!confirm('آیا از حذف این کاربر اطمینان دارید؟ این عملیات قابل بازگشت نیست.')) {
|
||||
return;
|
||||
}
|
||||
setIsDeleteConfirmOpen(true);
|
||||
};
|
||||
|
||||
const confirmDeleteUser = async () => {
|
||||
if (!user) return;
|
||||
const identifier = user.email || user.mobile;
|
||||
if (!identifier) return;
|
||||
|
||||
setIsLoading(true);
|
||||
setError(null);
|
||||
@@ -178,6 +185,7 @@ export default function UsersPage() {
|
||||
await usersApi.deleteUser(identifier, token!);
|
||||
showToast('success', 'کاربر با موفقیت حذف شد');
|
||||
setUser(null);
|
||||
setIsDeleteConfirmOpen(false);
|
||||
} catch (err) {
|
||||
setError(err instanceof Error ? err.message : 'خطا در حذف کاربر');
|
||||
} finally {
|
||||
@@ -324,6 +332,16 @@ export default function UsersPage() {
|
||||
isLoading={isLoading}
|
||||
/>
|
||||
)}
|
||||
|
||||
<ConfirmDialog
|
||||
open={isDeleteConfirmOpen && !!user}
|
||||
title="حذف کاربر"
|
||||
message="آیا از حذف این کاربر اطمینان دارید؟ این عملیات قابل بازگشت نیست."
|
||||
confirmLabel="حذف کاربر"
|
||||
isLoading={isLoading}
|
||||
onConfirm={confirmDeleteUser}
|
||||
onCancel={() => setIsDeleteConfirmOpen(false)}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user