Files
canina/frontend/admin-panel/src/pages/SystemSettingsPage.tsx
T
devops ddbf1ca026
Deploy Canina / deploy (push) Successful in 4m1s
E2E Playwright Tests / Run Full E2E & Security Suites (push) Failing after 7s
test: comprehensive test suites and quality gates pass across backend, storefront and admin
- 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.
2026-10-13 16:01:00 +00:00

477 lines
24 KiB
TypeScript

import {
AlertTriangle,
BookOpen,
CheckCircle2,
MessageSquare,
Save,
ShieldAlert,
ShoppingBag,
Sliders
} from 'lucide-react';
import { useEffect,useState } from 'react';
import { toast } from 'react-hot-toast';
import { Link } from 'react-router-dom';
import Button from '../components/ui/Button';
import Spinner from '../components/ui/Spinner';
import api from '../services/api';
import type { SystemSettings } from '../types/admin';
export default function SystemSettingsPage() {
const [system, setSystem] = useState<SystemSettings>({
maintenanceMode: false,
allowGuestCheckout: true,
b2bRegistrationOpen: true,
supportPhone: '021-12345678',
});
const [catalogSettings, setCatalogSettings] = useState({
CATALOG_ONLY_MODE: 'false',
CATALOG_SHOW_PRICES: 'true',
CATALOG_ALLOW_CART: 'false',
CATALOG_ALLOW_CHECKOUT: 'false',
CATALOG_PREORDER_BTN: 'false',
CATALOG_SHOW_ORDERS: 'false',
CATALOG_SHOW_WALLET: 'false',
CATALOG_SHOW_CHARITY: 'false',
PRESCRIPTION_UPLOAD_ENABLED: 'true',
ORDER_DISABLED_TITLE: 'امکان ثبت سفارش آنلاین موقتاً غیرفعال است',
ORDER_DISABLED_MESSAGE:
'سامانه در حال حاضر در حالت کاتالوگ قرار دارد. جهت دریافت مشاوره تخصصی می‌توانید با کارشناسان ما تماس بگیرید.',
});
const [isLoading, setIsLoading] = useState(true);
const [isSaving, setIsSaving] = useState(false);
useEffect(() => {
let isSubscribed = true;
Promise.all([
api.get('/settings/system'),
api.get('/settings/ui-texts'),
])
.then(([resSys]) => {
if (!isSubscribed) return;
const data = resSys.data?.data || resSys.data;
if (data) {
setSystem((prev) => ({
maintenanceMode: Boolean(data.maintenanceMode),
allowGuestCheckout: Boolean(data.allowGuestCheckout ?? true),
b2bRegistrationOpen: Boolean(data.b2bRegistrationOpen ?? true),
supportPhone: data.supportPhone || prev.supportPhone,
}));
const toStrBool = (val: any, defaultVal = false) => {
if (val === undefined || val === null) return defaultVal ? 'true' : 'false';
return val === true || val === 'true' || val === 1 || val === '1' ? 'true' : 'false';
};
setCatalogSettings({
CATALOG_ONLY_MODE: toStrBool(data.catalogOnlyMode, false),
CATALOG_SHOW_PRICES: toStrBool(data.catalogShowPrices, true),
CATALOG_ALLOW_CART: toStrBool(data.catalogAllowCart, false),
CATALOG_ALLOW_CHECKOUT: toStrBool(data.catalogAllowCheckout, false),
CATALOG_PREORDER_BTN: toStrBool(data.catalogPreorderBtn, false),
CATALOG_SHOW_ORDERS: toStrBool(data.catalogShowOrders, false),
CATALOG_SHOW_WALLET: toStrBool(data.catalogShowWallet, false),
CATALOG_SHOW_CHARITY: toStrBool(data.catalogShowCharity, false),
PRESCRIPTION_UPLOAD_ENABLED: toStrBool(data.prescriptionUploadEnabled, true),
ORDER_DISABLED_TITLE: data.orderDisabledTitle || 'امکان ثبت سفارش آنلاین موقتاً غیرفعال است',
ORDER_DISABLED_MESSAGE:
data.orderDisabledMessage ||
'سامانه در حال حاضر در حالت کاتالوگ قرار دارد. جهت دریافت مشاوره تخصصی می‌توانید با کارشناسان ما تماس بگیرید.',
});
}
})
.catch((err) => {
console.warn('Failed to fetch system/catalog settings', err);
})
.finally(() => {
if (isSubscribed) setIsLoading(false);
});
return () => {
isSubscribed = false;
};
}, []);
const handleSave = async (e: React.FormEvent) => {
e.preventDefault();
try {
setIsSaving(true);
await Promise.all([
api.patch('/settings/system', {
...system,
...catalogSettings,
}),
api.put('/settings/ui-texts', {
MAINTENANCE_MODE: system.maintenanceMode ? 'true' : 'false',
...catalogSettings,
}),
]);
toast.success('تنظیمات سیستمی و حالت‌های فروشگاه با موفقیت ذخیره شد.');
} catch (err) {
console.error('Failed to update system settings:', err);
toast.error('خطا در بروزرسانی تنظیمات سیستمی');
} finally {
setIsSaving(false);
}
};
return (
<div className="space-y-6 font-vazir" dir="rtl">
{/* Top Header with Quick Security and SMS links */}
<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">
<Sliders className="w-6 h-6 text-purple-600" />
تنظیمات سیستمی پیشرفته و کنترل دسترسی
</h2>
<p className="text-gray-500 font-medium mt-1">
مدیریت وضعیت صیانت و تعمیرات، کنترل کاتالوگ‌مود، خرید مهمان و ابزارهای امنیتی
</p>
</div>
{/* Action buttons for SSL and SMS as requested */}
<div className="flex items-center gap-2.5 flex-wrap">
<Link
to="/settings/ssl"
className="bg-emerald-50 text-emerald-700 hover:bg-emerald-100 border border-emerald-200 text-xs font-black px-4 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-xs"
>
<ShieldAlert className="w-4 h-4 text-emerald-600" />
<span>مدیریت گواهی SSL و امنیت</span>
</Link>
<Link
to="/settings/sms"
className="bg-purple-50 text-purple-700 hover:bg-purple-100 border border-purple-200 text-xs font-black px-4 py-2.5 rounded-xl transition-all flex items-center gap-2 shadow-xs"
>
<MessageSquare className="w-4 h-4 text-purple-600" />
<span>تنظیمات درگاه پیامک (MeliPayamak)</span>
</Link>
</div>
</div>
{isLoading ? (
<div className="flex justify-center p-12">
<Spinner size="lg" className="text-purple-600" />
</div>
) : (
<form onSubmit={handleSave} className="space-y-6">
{/* Maintenance Mode Card */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<AlertTriangle className="w-5 h-5 text-amber-500" />
حالت تعمیرات و صیانت موقت سایت (Maintenance Mode)
</h3>
<div className="flex items-center justify-between p-4 bg-amber-50/70 rounded-2xl border border-amber-200">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-amber-500 text-white flex items-center justify-center font-black">
<AlertTriangle className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">فعال‌سازی حالت تعمیرات</h4>
<p className="text-xs text-gray-600 mt-0.5">
با فعال کردن این گزینه، فرانت‌اند سایت برای کاربران قفل شده و صفحه «در حال بروزرسانی» نمایش داده می‌شود.
</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={system.maintenanceMode}
onChange={(e) => setSystem({ ...system, maintenanceMode: e.target.checked })}
/>
<div className="w-12 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-amber-500"></div>
</label>
</div>
</div>
{/* Catalog Only Mode Card */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<BookOpen className="w-5 h-5 text-purple-600" />
حالت کاتالوگ آنلاین بدون فروش مستقیم (Catalog Only Mode)
</h3>
<div className="p-4 bg-purple-50/60 rounded-2xl border border-purple-100 space-y-4">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-purple-600 text-white flex items-center justify-center font-black">
<BookOpen className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">فعال‌سازی حالت کاتالوگ</h4>
<p className="text-xs text-gray-600 mt-0.5">
مناسب برای زمانی که قصد دارید محصولات را صرفاً به عنوان دایرکتوری و مشخصات فنی معرفی کنید.
</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={catalogSettings.CATALOG_ONLY_MODE === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_ONLY_MODE: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-12 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-purple-600"></div>
</label>
</div>
{catalogSettings.CATALOG_ONLY_MODE === 'true' && (
<div className="pt-4 border-t border-purple-200/60 grid grid-cols-1 md:grid-cols-2 gap-3 text-xs">
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_SHOW_PRICES === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_SHOW_PRICES: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>نمایش قیمت محصولات در حالت کاتالوگ</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_ALLOW_CART === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_ALLOW_CART: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>امکان افزودن به سبد خرید</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_ALLOW_CHECKOUT === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_ALLOW_CHECKOUT: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>امکان ورود به صفحه تسویه‌حساب (ثبت فاکتور)</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_PREORDER_BTN === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_PREORDER_BTN: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>نمایش دکمه «ثبت پیش‌خرید» به جای خرید مستقیم</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_SHOW_ORDERS === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_SHOW_ORDERS: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>نمایش بخش «سفارشات من» در پنل کاربری</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_SHOW_WALLET === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_SHOW_WALLET: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>نمایش بخش «کیف پول و شارژ» در پنل کاربری</span>
</label>
<label className="flex items-center gap-2.5 font-bold text-gray-800 p-2.5 bg-white rounded-xl border border-purple-100 cursor-pointer">
<input
type="checkbox"
checked={catalogSettings.CATALOG_SHOW_CHARITY === 'true'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
CATALOG_SHOW_CHARITY: e.target.checked ? 'true' : 'false',
})
}
className="w-4 h-4 rounded text-purple-600 focus:ring-purple-500"
/>
<span>نمایش بخش «ردپای مهربانی / خیریه» در پنل کاربری</span>
</label>
{/* Customizable Order Disabled Title and Message */}
<div className="md:col-span-2 pt-2 border-t border-purple-200/60 space-y-3">
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">
عنوان پیام غیرفعال بودن ثبت سفارش
</label>
<input
type="text"
value={catalogSettings.ORDER_DISABLED_TITLE}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
ORDER_DISABLED_TITLE: e.target.value,
})
}
placeholder="امکان ثبت سفارش آنلاین موقتاً غیرفعال است"
className="w-full px-3.5 py-2 bg-white rounded-xl border border-purple-200 focus:border-purple-600 outline-none text-xs font-bold text-gray-900"
/>
</div>
<div>
<label className="block text-xs font-bold text-gray-700 mb-1">
متن توضیحات پیام غیرفعال بودن ثبت سفارش (در صفحه تسویه‌حساب)
</label>
<textarea
rows={3}
value={catalogSettings.ORDER_DISABLED_MESSAGE}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
ORDER_DISABLED_MESSAGE: e.target.value,
})
}
placeholder="سامانه در حال حاضر در حالت کاتالوگ قرار دارد. جهت دریافت مشاوره تخصصی می‌توانید با کارشناسان ما تماس بگیرید."
className="w-full px-3.5 py-2 bg-white rounded-xl border border-purple-200 focus:border-purple-600 outline-none text-xs font-bold text-gray-900 resize-none"
/>
</div>
</div>
</div>
)}
</div>
</div>
{/* User & B2B System Rules */}
<div className="bg-white p-6 rounded-2xl shadow-sm border border-gray-200 space-y-4">
<h3 className="text-base font-black text-gray-900 border-b border-gray-100 pb-3 flex items-center gap-2">
<ShoppingBag className="w-5 h-5 text-slate-700" />
قوانین خرید مهمان و ثبت‌نام متقاضیان همکار
</h3>
<div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div className="flex items-center justify-between p-4 bg-gray-50 rounded-2xl border border-gray-200">
<div>
<h4 className="font-bold text-gray-900 text-sm">اجازه خرید کاربر مهمان</h4>
<p className="text-xs text-gray-500 mt-0.5">تسویه‌حساب بدون نیاز به ساخت حساب کاربری قبلی</p>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={system.allowGuestCheckout}
onChange={(e) => setSystem({ ...system, allowGuestCheckout: e.target.checked })}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-purple-600"></div>
</label>
</div>
<div className="flex items-center justify-between p-4 bg-gray-50 rounded-2xl border border-gray-200">
<div>
<h4 className="font-bold text-gray-900 text-sm">فرم ثبت‌نام همکاران B2B باز است</h4>
<p className="text-xs text-gray-500 mt-0.5">امکان ارسال درخواست نمایندگی جدید در سایت</p>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={system.b2bRegistrationOpen}
onChange={(e) => setSystem({ ...system, b2bRegistrationOpen: e.target.checked })}
/>
<div className="w-11 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-purple-600"></div>
</label>
</div>
{/* Prescription Upload Button Toggle */}
<div className="flex items-center justify-between p-4 bg-emerald-50/60 rounded-2xl border border-emerald-200/80 md:col-span-2">
<div className="flex items-center gap-3">
<div className="w-10 h-10 rounded-xl bg-emerald-600 text-white flex items-center justify-center font-black">
<CheckCircle2 className="w-5 h-5" />
</div>
<div>
<h4 className="font-bold text-gray-900 text-sm">دکمه و امکان «ثبت نسخه دامپزشک» در سایت فعال باشد</h4>
<p className="text-xs text-gray-600 mt-0.5">
با غیرفعال‌سازی این گزینه، دکمه و پنجره ثبت نسخه در هدر دسکتاپ و منوی موبایل پنهان می‌شود.
</p>
</div>
</div>
<label className="relative inline-flex items-center cursor-pointer">
<input
type="checkbox"
className="sr-only peer"
checked={catalogSettings.PRESCRIPTION_UPLOAD_ENABLED !== 'false'}
onChange={(e) =>
setCatalogSettings({
...catalogSettings,
PRESCRIPTION_UPLOAD_ENABLED: e.target.checked ? 'true' : 'false',
})
}
/>
<div className="w-12 h-6 bg-gray-200 peer-focus:outline-none rounded-full peer peer-checked:after:translate-x-full peer-checked:after:border-white after:content-[''] after:absolute after:top-[2px] after:left-[2px] after:bg-white after:border-gray-300 after:border after:rounded-full after:h-5 after:w-5 after:transition-all peer-checked:bg-emerald-600"></div>
</label>
</div>
<div className="md:col-span-2">
<label className="block text-sm font-bold text-gray-700 mb-1">
شماره تماس پشتیبانی اضطراری (در زمان تعمیرات)
</label>
<input
required
type="text"
value={system.supportPhone}
onChange={(e) => setSystem({ ...system, supportPhone: e.target.value })}
className="w-full px-4 py-2.5 rounded-xl border border-gray-200 focus:border-purple-500 outline-none text-xs font-bold"
dir="ltr"
/>
</div>
</div>
</div>
<div className="flex justify-end">
<Button
type="submit"
variant="primary"
size="md"
startIcon={Save}
isLoading={isSaving}
>
ذخیره تنظیمات سیستمی
</Button>
</div>
</form>
)}
</div>
);
}