- Backend (NestJS): 82 suites, 641 tests passing; 0 ESLint errors; 0 tsc errors. Hardened SSL cert/key matching, SSRF IP pinning, and eBank validation. - Admin Panel (Vite/React): 48 suites, 300 tests passing; 0 ESLint errors; 0 tsc errors. Full coverage across all pages and settings. - Storefront (Next.js): 62 suites, 368 tests passing; 0 ESLint errors; 0 tsc errors. - Fixed unmount lifecycle guards, promise error handling, and component edge cases.
170 lines
7.4 KiB
TypeScript
170 lines
7.4 KiB
TypeScript
import { Save,Truck } from 'lucide-react';
|
||
import { useEffect,useState } from 'react';
|
||
import { toast } from 'react-hot-toast';
|
||
import Button from '../components/ui/Button';
|
||
import PriceInput from '../components/ui/PriceInput';
|
||
import Spinner from '../components/ui/Spinner';
|
||
import api from '../services/api';
|
||
|
||
|
||
|
||
export default function ShippingSettingsPage() {
|
||
const [shipping, setShipping] = useState({
|
||
SHIPPING_FEE: '85000',
|
||
FREE_SHIPPING_THRESHOLD: '2000000',
|
||
MIN_ORDER_AMOUNT: '0',
|
||
shipping_notice:
|
||
'ارسال رایگان برای سفارشهای بالای ۱۵۰ هزار تومان — گارانتی اصالت کنینا آلمان — مشاوره تخصصی مکملهای دارویی سگ و گربه با کادر دامپزشکان مجرب',
|
||
});
|
||
|
||
const [isLoading, setIsLoading] = useState(true);
|
||
const [isSaving, setIsSaving] = useState(false);
|
||
|
||
useEffect(() => {
|
||
let isSubscribed = true;
|
||
api.get('/admin/settings')
|
||
.then((res) => {
|
||
if (!isSubscribed) return;
|
||
if (res.data?.success) {
|
||
const d = res.data.data;
|
||
setShipping({
|
||
SHIPPING_FEE: String(d.SHIPPING_FEE ?? d.shipping_fee ?? d.standardShippingFee ?? '85000'),
|
||
FREE_SHIPPING_THRESHOLD: String(d.FREE_SHIPPING_THRESHOLD ?? d.free_shipping_threshold ?? d.freeShippingThreshold ?? '2000000'),
|
||
MIN_ORDER_AMOUNT: String(d.MIN_ORDER_AMOUNT ?? d.min_order_amount ?? d.minOrderAmount ?? '0'),
|
||
shipping_notice: d.shipping_notice || d.SHIPPING_NOTICE || 'ارسال رایگان برای سفارشهای بالای ۱۵۰ هزار تومان — گارانتی اصالت کنینا آلمان',
|
||
});
|
||
}
|
||
})
|
||
.catch((err) => {
|
||
console.error('Failed to fetch shipping settings', err);
|
||
})
|
||
.finally(() => {
|
||
if (isSubscribed) setIsLoading(false);
|
||
});
|
||
return () => {
|
||
isSubscribed = false;
|
||
};
|
||
}, []);
|
||
|
||
const handleSave = async (e: React.FormEvent) => {
|
||
e.preventDefault();
|
||
try {
|
||
setIsSaving(true);
|
||
await api.put('/admin/settings', shipping);
|
||
toast.success('تنظیمات ارسال و کرایه حمل با موفقیت بروزرسانی شد.');
|
||
} catch (err) {
|
||
console.error('Failed to update shipping settings:', err);
|
||
toast.error('خطا در ذخیره تنظیمات ارسال');
|
||
} finally {
|
||
setIsSaving(false);
|
||
}
|
||
};
|
||
|
||
return (
|
||
<div className="space-y-6 font-vazir" dir="rtl">
|
||
<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">
|
||
<Truck className="w-6 h-6 text-blue-600" />
|
||
تنظیمات ارسال و کرایه حمل (Shipping & Delivery)
|
||
</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-blue-600" />
|
||
</div>
|
||
) : (
|
||
<form onSubmit={handleSave} className="space-y-6">
|
||
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-6">
|
||
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
|
||
<Truck className="w-5 h-5 text-blue-600" />
|
||
قوانین هزینه و کرایه ارسال
|
||
</h3>
|
||
|
||
<div className="grid grid-cols-1 md:grid-cols-2 gap-6">
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-2">
|
||
هزینه ثابت ارسال (تومان) *
|
||
</label>
|
||
<PriceInput
|
||
value={shipping.SHIPPING_FEE}
|
||
onChange={(val) => setShipping({ ...shipping, SHIPPING_FEE: val })}
|
||
placeholder="مثال: ۸۵,۰۰۰"
|
||
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-blue-500"
|
||
/>
|
||
<p className="text-xs text-gray-500 mt-1.5 font-medium">
|
||
هزینه استاندارد پستی که برای سفارشهای عادی منظور میشود.
|
||
</p>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-2">
|
||
آستانه ارسال رایگان (تومان) *
|
||
</label>
|
||
<PriceInput
|
||
value={shipping.FREE_SHIPPING_THRESHOLD}
|
||
onChange={(val) => setShipping({ ...shipping, FREE_SHIPPING_THRESHOLD: val })}
|
||
placeholder="مثال: ۲,۰۰۰,۰۰۰"
|
||
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-blue-500"
|
||
/>
|
||
<p className="text-xs text-gray-500 mt-1.5 font-medium">
|
||
سفارشهای با جمع مبلغ بالاتر از این عدد، به طور خودکار رایگان ارسال میشوند.
|
||
</p>
|
||
</div>
|
||
|
||
<div>
|
||
<label className="block text-sm font-bold text-gray-700 mb-2">
|
||
حداقل مبلغ سفارش مجاز (تومان)
|
||
</label>
|
||
<PriceInput
|
||
value={shipping.MIN_ORDER_AMOUNT}
|
||
onChange={(val) => setShipping({ ...shipping, MIN_ORDER_AMOUNT: val })}
|
||
placeholder="۰ = بدون محدودیت"
|
||
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-blue-500"
|
||
/>
|
||
<p className="text-xs text-gray-500 mt-1.5 font-medium">
|
||
سفارشات کمتر از این عدد اجازه ورود به درگاه را نخواهند داشت (۰ برای لغو شرط).
|
||
</p>
|
||
</div>
|
||
|
||
<div className="md:col-span-2">
|
||
<label className="block text-sm font-bold text-gray-700 mb-2">
|
||
متن نوار متحرک اعلان بالای سایت (Ticker Announcement)
|
||
</label>
|
||
<textarea
|
||
rows={2}
|
||
value={shipping.shipping_notice}
|
||
onChange={(e) => setShipping({ ...shipping, shipping_notice: e.target.value })}
|
||
placeholder="ارسال رایگان برای سفارشهای بالای ۱۵۰ هزار تومان — ..."
|
||
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-blue-500 text-xs font-bold bg-amber-50/40"
|
||
/>
|
||
<p className="text-xs text-gray-500 mt-1 font-medium">
|
||
این متن در نوار زرد متحرک بالای تمام صفحات سایت نمایش داده میشود.
|
||
</p>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
<div className="flex justify-end">
|
||
<Button
|
||
type="submit"
|
||
variant="primary"
|
||
size="md"
|
||
startIcon={Save}
|
||
isLoading={isSaving}
|
||
>
|
||
ذخیره تغییرات ارسال
|
||
</Button>
|
||
</div>
|
||
</form>
|
||
)}
|
||
</div>
|
||
);
|
||
}
|
||
|