188 lines
8.0 KiB
TypeScript
188 lines
8.0 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import { DollarSign, Save, Plus, X } from 'lucide-react';
|
|
import { toast } from 'react-hot-toast';
|
|
import api from '../services/api';
|
|
import Spinner from '../components/ui/Spinner';
|
|
import type { FinancialSettings } from '../types/admin';
|
|
|
|
export default function FinancialSettingsPage() {
|
|
const [financial, setFinancial] = useState<FinancialSettings>({
|
|
taxPercentage: 10,
|
|
freeShippingThreshold: 2000000,
|
|
standardShippingFee: 85000,
|
|
charityDonationOptions: [10000, 20000, 50000],
|
|
});
|
|
const [isLoading, setIsLoading] = useState(true);
|
|
const [isSaving, setIsSaving] = useState(false);
|
|
const [donationInput, setDonationInput] = useState('');
|
|
|
|
useEffect(() => {
|
|
let isSubscribed = true;
|
|
api.get('/settings/financial')
|
|
.then(res => {
|
|
if (!isSubscribed) return;
|
|
const data = res.data?.data || res.data;
|
|
if (data) {
|
|
setFinancial({
|
|
taxPercentage: Number(data.taxPercentage ?? 10),
|
|
freeShippingThreshold: Number(data.freeShippingThreshold ?? 2000000),
|
|
standardShippingFee: Number(data.standardShippingFee ?? 85000),
|
|
charityDonationOptions: Array.isArray(data.charityDonationOptions)
|
|
? data.charityDonationOptions.map(Number)
|
|
: [10000, 20000, 50000],
|
|
});
|
|
}
|
|
})
|
|
.catch(err => {
|
|
console.warn('Failed to fetch Financial settings, using default state', err);
|
|
})
|
|
.finally(() => {
|
|
if (isSubscribed) setIsLoading(false);
|
|
});
|
|
return () => {
|
|
isSubscribed = false;
|
|
};
|
|
}, []);
|
|
|
|
const handleSave = async (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
try {
|
|
setIsSaving(true);
|
|
await api.patch('/settings/financial', financial);
|
|
toast.success('تنظیمات مالی و ارسال با موفقیت بروزرسانی شد');
|
|
} catch (err) {
|
|
console.error('Failed to update financial settings:', err);
|
|
toast.error('خطا در بروزرسانی تنظیمات مالی');
|
|
} finally {
|
|
setIsSaving(false);
|
|
}
|
|
};
|
|
|
|
const addDonationOption = () => {
|
|
const val = Number(donationInput);
|
|
if (val > 0 && !financial.charityDonationOptions.includes(val)) {
|
|
setFinancial({
|
|
...financial,
|
|
charityDonationOptions: [...financial.charityDonationOptions, val].sort((a, b) => a - b),
|
|
});
|
|
setDonationInput('');
|
|
}
|
|
};
|
|
|
|
const removeDonationOption = (amount: number) => {
|
|
setFinancial({
|
|
...financial,
|
|
charityDonationOptions: financial.charityDonationOptions.filter(a => a !== amount),
|
|
});
|
|
};
|
|
|
|
return (
|
|
<div className="space-y-6">
|
|
<div className="flex flex-col sm:flex-row sm:items-center justify-between gap-4">
|
|
<div>
|
|
<h2 className="text-2xl font-black text-gray-900 flex items-center gap-2">
|
|
<DollarSign className="w-6 h-6 text-purple-600" />
|
|
تنظیمات مالی و ارسال (Financial & Shipping Settings)
|
|
</h2>
|
|
<p className="text-gray-500 font-medium mt-1">تنظیم درصد مالیات، آستانه ارسال رایگان و گزینههای کمک به خیریه</p>
|
|
</div>
|
|
</div>
|
|
|
|
{isLoading ? (
|
|
<div className="flex justify-center p-12"><Spinner size="lg" className="text-purple-600" /></div>
|
|
) : (
|
|
<form onSubmit={handleSave} className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-6">
|
|
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-1">درصد مالیات ارزش افزوده (taxPercentage) *</label>
|
|
<div className="relative">
|
|
<input
|
|
required
|
|
type="number"
|
|
min="0"
|
|
max="100"
|
|
value={financial.taxPercentage}
|
|
onChange={(e) => setFinancial({ ...financial, taxPercentage: Number(e.target.value) })}
|
|
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-bold text-sm"
|
|
dir="ltr"
|
|
/>
|
|
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold">%</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div>
|
|
<label className="block text-sm font-bold text-gray-700 mb-1">هزینه ارسال استاندارد (standardShippingFee - تومان) *</label>
|
|
<input
|
|
required
|
|
type="number"
|
|
min="0"
|
|
value={financial.standardShippingFee}
|
|
onChange={(e) => setFinancial({ ...financial, standardShippingFee: Number(e.target.value) })}
|
|
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-bold text-sm"
|
|
dir="ltr"
|
|
/>
|
|
</div>
|
|
|
|
<div className="md:col-span-2">
|
|
<label className="block text-sm font-bold text-gray-700 mb-1">آستانه ارسال رایگان (freeShippingThreshold - تومان) *</label>
|
|
<input
|
|
required
|
|
type="number"
|
|
min="0"
|
|
value={financial.freeShippingThreshold}
|
|
onChange={(e) => setFinancial({ ...financial, freeShippingThreshold: Number(e.target.value) })}
|
|
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-bold text-sm"
|
|
dir="ltr"
|
|
/>
|
|
<p className="text-xs text-gray-500 mt-1">سبدهای خرید با مبلغ بالاتر از این مقدار، ارسال رایگان خواهند داشت.</p>
|
|
</div>
|
|
|
|
<div className="md:col-span-2 space-y-2">
|
|
<label className="block text-sm font-bold text-gray-700 mb-1">گزینههای کمک به خیریه ردپای مهربانی (charityDonationOptions)</label>
|
|
<div className="flex gap-2 max-w-md">
|
|
<input
|
|
type="number"
|
|
placeholder="مبلغ جدید (مثال: 100000)"
|
|
value={donationInput}
|
|
onChange={(e) => setDonationInput(e.target.value)}
|
|
className="flex-1 px-4 py-2 rounded-xl border border-gray-200 text-xs outline-none font-bold"
|
|
dir="ltr"
|
|
/>
|
|
<button
|
|
type="button"
|
|
onClick={addDonationOption}
|
|
className="px-4 py-2 bg-purple-600 text-white rounded-xl text-xs font-bold flex items-center gap-1"
|
|
>
|
|
<Plus className="w-4 h-4" /> افزودن
|
|
</button>
|
|
</div>
|
|
|
|
<div className="flex flex-wrap gap-2 pt-2">
|
|
{financial.charityDonationOptions.map(amount => (
|
|
<span key={amount} className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-purple-50 text-purple-700 text-xs font-bold rounded-xl border border-purple-200">
|
|
{amount.toLocaleString('fa-IR')} تومان
|
|
<button type="button" onClick={() => removeDonationOption(amount)} className="hover:text-red-500">
|
|
<X className="w-3.5 h-3.5" />
|
|
</button>
|
|
</span>
|
|
))}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="flex justify-end pt-4 border-t border-gray-100">
|
|
<button
|
|
type="submit"
|
|
disabled={isSaving}
|
|
className="bg-purple-600 hover:bg-purple-700 text-white font-bold py-3 px-8 rounded-xl transition-colors flex items-center gap-2 shadow-md shadow-purple-200"
|
|
>
|
|
{isSaving ? <Spinner size="sm" /> : <Save className="w-5 h-5" />}
|
|
ذخیره تنظیمات مالی
|
|
</button>
|
|
</div>
|
|
</form>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|