feat(settings): unify settings and ui-texts with canonical aliases, add PriceInput thousand separators, and synchronize financial endpoints
Deploy Canina / deploy (push) Successful in 1m46s

This commit is contained in:
parsa aghaei committed 2026-08-17 09:57:23 +03:30
1 parent 45c9db133c
commit b3c60a1a7d
17 files changed
+79340 -78724

No files matched your search

@@ -0,0 +1,99 @@
import React, { useState, useEffect } from 'react';
interface PriceInputProps {
value: number | string;
onChange: (rawNumericValue: string) => void;
placeholder?: string;
className?: string;
suffix?: string;
disabled?: boolean;
required?: boolean;
min?: number;
max?: number;
name?: string;
id?: string;
}
// Convert Persian and Arabic digits to English digits
export function toEnglishDigits(str: string): string {
if (!str) return '';
return str
.replace(/[۰-۹]/g, (d) => String(d.charCodeAt(0) - 1776))
.replace(/[٠-٩]/g, (d) => String(d.charCodeAt(0) - 1632));
}
// Format numbers with thousand comma separator
export function formatPriceWithCommas(value: number | string): string {
if (value === undefined || value === null || value === '') return '';
const cleanStr = toEnglishDigits(String(value)).replace(/[^0-9]/g, '');
if (!cleanStr) return '';
return Number(cleanStr).toLocaleString('en-US');
}
export default function PriceInput({
value,
onChange,
placeholder = '۰',
className = '',
suffix = 'تومان',
disabled = false,
required = false,
min,
max,
name,
id,
}: PriceInputProps) {
const [displayValue, setDisplayValue] = useState<string>('');
useEffect(() => {
if (value === undefined || value === null || value === '') {
setDisplayValue('');
} else {
setDisplayValue(formatPriceWithCommas(value));
}
}, [value]);
const handleChange = (e: React.ChangeEvent<HTMLInputElement>) => {
const rawInput = e.target.value;
const cleanNumbers = toEnglishDigits(rawInput).replace(/[^0-9]/g, '');
if (cleanNumbers === '') {
setDisplayValue('');
onChange('');
return;
}
const numVal = Number(cleanNumbers);
if (max !== undefined && numVal > max) {
return;
}
setDisplayValue(Number(cleanNumbers).toLocaleString('en-US'));
onChange(cleanNumbers);
};
return (
<div className="relative w-full flex items-center">
<input
type="text"
inputMode="numeric"
id={id}
name={name}
disabled={disabled}
required={required}
value={displayValue}
onChange={handleChange}
placeholder={placeholder}
dir="ltr"
className={`w-full text-left font-mono font-bold tracking-wider outline-none transition-all ${
suffix ? 'pr-3 pl-16' : 'px-3'
} ${className}`}
/>
{suffix && (
<span className="absolute left-3 text-xs font-bold text-gray-400 pointer-events-none select-none font-vazir">
{suffix}
</span>
)}
</div>
);
}
+37 -3
View File
@@ -5,6 +5,7 @@ import api from '../services/api';
import Spinner from '../components/ui/Spinner';
import Pagination from '../components/ui/Pagination';
import ConfirmModal from '../components/ui/ConfirmModal';
import PriceInput from '../components/ui/PriceInput';
export interface CouponTarget {
targetType: string;
@@ -337,7 +338,30 @@ function CouponModal({ formData, setFormData, onSave, onClose, isEditing }: Coup
</div>
</div>
</label>
<input required type="number" min="0" value={formData.value} onChange={e => setFormData({...formData, value: e.target.value})} dir="ltr" className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500" />
{formData.type === 'fixed' ? (
<PriceInput
required
value={formData.value}
onChange={(val) => setFormData({ ...formData, value: val })}
placeholder="مثال: ۵۰,۰۰۰"
className="px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 text-sm"
/>
) : (
<div className="relative">
<input
required
type="number"
min="0"
max="100"
value={formData.value}
onChange={(e) => setFormData({ ...formData, value: e.target.value })}
dir="ltr"
placeholder="مثال: ۲۰"
className="w-full px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 font-bold"
/>
<span className="absolute left-4 top-1/2 -translate-y-1/2 text-gray-400 font-bold">%</span>
</div>
)}
</div>
</div>
<div className="grid grid-cols-1 md:grid-cols-4 gap-4">
@@ -351,7 +375,12 @@ function CouponModal({ formData, setFormData, onSave, onClose, isEditing }: Coup
</div>
</div>
</label>
<input type="number" min="0" value={formData.minCartValue} onChange={e => setFormData({...formData, minCartValue: e.target.value})} dir="ltr" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-purple-500 text-sm" />
<PriceInput
value={formData.minCartValue}
onChange={(val) => setFormData({ ...formData, minCartValue: val })}
placeholder="۰ = بدون محدودیت"
className="px-3 py-2 rounded-lg border border-gray-200 focus:border-purple-500 text-sm"
/>
</div>
<div className="space-y-2">
<label className="text-xs font-bold text-gray-700 flex items-center gap-1">
@@ -363,7 +392,12 @@ function CouponModal({ formData, setFormData, onSave, onClose, isEditing }: Coup
</div>
</div>
</label>
<input type="number" min="0" value={formData.maxCartValue} onChange={e => setFormData({...formData, maxCartValue: e.target.value})} dir="ltr" className="w-full px-3 py-2 rounded-lg border border-gray-200 focus:border-purple-500 text-sm" />
<PriceInput
value={formData.maxCartValue}
onChange={(val) => setFormData({ ...formData, maxCartValue: val })}
placeholder="بدون سقف"
className="px-3 py-2 rounded-lg border border-gray-200 focus:border-purple-500 text-sm"
/>
</div>
<div className="space-y-2">
<label className="text-xs font-bold text-gray-700 flex items-center gap-1">
@@ -3,6 +3,7 @@ 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 PriceInput from '../components/ui/PriceInput';
import type { FinancialSettings } from '../types/admin';
export default function FinancialSettingsPage() {
@@ -36,7 +37,7 @@ export default function FinancialSettingsPage() {
}
})
.catch(err => {
console.warn('Failed to fetch Financial settings, using default state', err);
console.warn('Failed to fetch Financial settings', err);
})
.finally(() => {
if (isSubscribed) setIsLoading(false);
@@ -113,28 +114,22 @@ export default function FinancialSettingsPage() {
</div>
<div>
<label className="block text-sm font-bold text-gray-700 mb-1">هزینه ارسال استاندارد (standardShippingFee - تومان) *</label>
<input
<label className="block text-sm font-bold text-gray-700 mb-1">هزینه ارسال استاندارد (standardShippingFee) *</label>
<PriceInput
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"
onChange={(val) => setFinancial({ ...financial, standardShippingFee: Number(val) })}
className="px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 text-sm"
/>
</div>
<div className="md:col-span-2">
<label className="block text-sm font-bold text-gray-700 mb-1">آستانه ارسال رایگان (freeShippingThreshold - تومان) *</label>
<input
<label className="block text-sm font-bold text-gray-700 mb-1">آستانه ارسال رایگان (freeShippingThreshold) *</label>
<PriceInput
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"
onChange={(val) => setFinancial({ ...financial, freeShippingThreshold: Number(val) })}
className="px-4 py-3 rounded-xl border border-gray-200 focus:border-purple-500 text-sm"
/>
<p className="text-xs text-gray-500 mt-1">سبدهای خرید با مبلغ بالاتر از این مقدار، ارسال رایگان خواهند داشت.</p>
</div>
@@ -142,18 +137,16 @@ export default function FinancialSettingsPage() {
<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)"
<PriceInput
placeholder="مبلغ جدید (مثال: ۱۰۰,۰۰۰)"
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"
onChange={(val) => setDonationInput(val)}
className="px-4 py-2 rounded-xl border border-gray-200 text-xs"
/>
<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"
className="px-4 py-2 bg-purple-600 hover:bg-purple-700 text-white rounded-xl text-xs font-bold flex items-center gap-1 shrink-0"
>
<Plus className="w-4 h-4" /> افزودن
</button>
+43 -47
View File
@@ -6,6 +6,7 @@ import Spinner from '../components/ui/Spinner';
import Pagination from '../components/ui/Pagination';
import MediaSelector from '../components/ui/MediaSelector';
import ConfirmModal from '../components/ui/ConfirmModal';
import PriceInput from '../components/ui/PriceInput';
export interface Category {
id: string;
@@ -706,13 +707,11 @@ export default function Products() {
</div>
</div>
</label>
<input
type="number"
min="0"
placeholder="مثال: 500000"
<PriceInput
placeholder="مثال: ۵۰۰,۰۰۰"
value={formData.buyPrice}
onChange={(e) => {
const bPrice = e.target.value ? Number(e.target.value) : '';
onChange={(rawVal) => {
const bPrice = rawVal ? Number(rawVal) : '';
let pPrice = formData.priceValue;
let wPrice = formData.wholesalePrice;
let pMargin = formData.priceValueMarginPercent;
@@ -737,11 +736,10 @@ export default function Products() {
wholesalePrice: wPrice,
priceValueMarginPercent: pMargin,
wholesaleMarginPercent: wMargin,
priceDisplay: `${pPrice.toLocaleString('fa-IR')} تومان`,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className="w-full px-4 py-3 rounded-xl border border-amber-300 focus:border-amber-500 outline-none bg-amber-50/20 font-bold"
dir="ltr"
className="px-4 py-3 rounded-xl border border-amber-300 focus:border-amber-500 bg-amber-50/20 text-sm"
/>
</div>
@@ -752,25 +750,24 @@ export default function Products() {
<span className="text-[10px] text-purple-600 font-bold">یا سود ٪ تک‌فروشی</span>
</div>
<div className="grid grid-cols-5 gap-2">
<input
required
type="number"
min="0"
value={formData.priceValue}
onChange={(e) => {
const val = Number(e.target.value);
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
const pMargin = bPrice && val ? Math.round(((val - bPrice) / bPrice) * 100) : '';
setFormData({
...formData,
priceValue: val,
priceValueMarginPercent: pMargin,
priceDisplay: `${val.toLocaleString('fa-IR')} تومان`,
});
}}
className="col-span-3 px-3 py-3 rounded-xl border border-gray-200 focus:border-purple-500 outline-none font-bold"
dir="ltr"
/>
<div className="col-span-3">
<PriceInput
required
value={formData.priceValue}
onChange={(rawVal) => {
const val = Number(rawVal || 0);
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
const pMargin = bPrice && val ? Math.round(((val - bPrice) / bPrice) * 100) : '';
setFormData({
...formData,
priceValue: val,
priceValueMarginPercent: pMargin,
priceDisplay: `${val.toLocaleString('fa-IR')} تومان`,
});
}}
className="px-3 py-3 rounded-xl border border-gray-200 focus:border-purple-500 text-sm"
/>
</div>
<div className="col-span-2 relative">
<input
type="number"
@@ -787,7 +784,7 @@ export default function Products() {
...formData,
priceValueMarginPercent: margin,
priceValue: pPrice,
priceDisplay: `${pPrice.toLocaleString('fa-IR')} تومان`,
priceDisplay: `${Number(pPrice || 0).toLocaleString('fa-IR')} تومان`,
});
}}
className="w-full px-2 py-3 rounded-xl border border-purple-200 focus:border-purple-500 text-center font-bold text-xs bg-purple-50/30 outline-none"
@@ -805,24 +802,23 @@ export default function Products() {
<span className="text-[10px] text-purple-600 font-bold">یا سود ٪ B2B</span>
</div>
<div className="grid grid-cols-5 gap-2">
<input
type="number"
min="0"
placeholder="قیمت B2B"
value={formData.wholesalePrice}
onChange={(e) => {
const val = e.target.value ? Number(e.target.value) : '';
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
const wMargin = bPrice && val ? Math.round(((Number(val) - bPrice) / bPrice) * 100) : '';
setFormData({
...formData,
wholesalePrice: val,
wholesaleMarginPercent: wMargin,
});
}}
className="col-span-3 px-3 py-3 rounded-xl border border-purple-200 focus:border-purple-600 outline-none font-bold bg-purple-50/20"
dir="ltr"
/>
<div className="col-span-3">
<PriceInput
placeholder="قیمت B2B"
value={formData.wholesalePrice}
onChange={(rawVal) => {
const val = rawVal ? Number(rawVal) : '';
const bPrice = formData.buyPrice ? Number(formData.buyPrice) : null;
const wMargin = bPrice && val ? Math.round(((Number(val) - bPrice) / bPrice) * 100) : '';
setFormData({
...formData,
wholesalePrice: val,
wholesaleMarginPercent: wMargin,
});
}}
className="px-3 py-3 rounded-xl border border-purple-200 focus:border-purple-600 bg-purple-50/20 text-sm"
/>
</div>
<div className="col-span-2 relative">
<input
type="number"
+49 -22
View File
@@ -4,11 +4,14 @@ import { Settings as SettingsIcon, Save, Truck, ShieldAlert, Percent, AlertCircl
import { toast } from 'react-hot-toast';
import api from '../services/api';
import Spinner from '../components/ui/Spinner';
import PriceInput from '../components/ui/PriceInput';
export default function Settings() {
const [settings, setSettings] = useState({
SHIPPING_FEE: '',
MIN_ORDER_AMOUNT: '',
FREE_SHIPPING_THRESHOLD: '2000000',
TAX_PERCENTAGE: '10',
B2B_DISCOUNT_PERCENT: '',
MAINTENANCE_MODE: 'false',
CATALOG_ONLY_MODE: 'false',
@@ -49,9 +52,11 @@ export default function Settings() {
if (!isSubscribed) return;
if (response.data?.success) {
setSettings({
SHIPPING_FEE: response.data.data.SHIPPING_FEE || '0',
MIN_ORDER_AMOUNT: response.data.data.MIN_ORDER_AMOUNT || '0',
B2B_DISCOUNT_PERCENT: response.data.data.B2B_DISCOUNT_PERCENT || '0',
SHIPPING_FEE: response.data.data.SHIPPING_FEE || response.data.data.shipping_fee || response.data.data.standardShippingFee || '0',
MIN_ORDER_AMOUNT: response.data.data.MIN_ORDER_AMOUNT || response.data.data.min_order_amount || response.data.data.minOrderAmount || '0',
FREE_SHIPPING_THRESHOLD: response.data.data.FREE_SHIPPING_THRESHOLD || response.data.data.free_shipping_threshold || response.data.data.freeShippingThreshold || '2000000',
TAX_PERCENTAGE: response.data.data.TAX_PERCENTAGE || response.data.data.tax_percentage || response.data.data.taxPercentage || '10',
B2B_DISCOUNT_PERCENT: response.data.data.B2B_DISCOUNT_PERCENT || response.data.data.b2b_discount_percent || response.data.data.b2bDiscountPercent || '0',
MAINTENANCE_MODE: response.data.data.MAINTENANCE_MODE || 'false',
CATALOG_ONLY_MODE: response.data.data.CATALOG_ONLY_MODE || 'false',
CATALOG_SHOW_PRICES: response.data.data.CATALOG_SHOW_PRICES || 'true',
@@ -149,41 +154,63 @@ export default function Settings() {
<div className="space-y-5">
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">هزینه ثابت ارسال (تومان)</label>
<input
type="number"
<label className="block text-sm font-bold text-gray-700 mb-2">هزینه ثابت ارسال</label>
<PriceInput
value={settings.SHIPPING_FEE}
onChange={(e) => setSettings({ ...settings, SHIPPING_FEE: e.target.value })}
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-purple-500 font-bold"
dir="ltr"
onChange={(val) => setSettings({ ...settings, SHIPPING_FEE: val })}
placeholder="مثال: ۸۵,۰۰۰"
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-purple-500"
/>
</div>
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">حداقل مبلغ سفارش (تومان)</label>
<input
type="number"
<label className="block text-sm font-bold text-gray-700 mb-2">آستانه ارسال رایگان</label>
<PriceInput
value={settings.FREE_SHIPPING_THRESHOLD}
onChange={(val) => setSettings({ ...settings, FREE_SHIPPING_THRESHOLD: val })}
placeholder="مثال: ۲,۰۰۰,۰۰۰"
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-purple-500"
/>
<p className="text-xs text-gray-500 mt-2 font-medium">سفارشات بالاتر از این مبلغ شامل ارسال رایگان خواهند بود.</p>
</div>
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">حداقل مبلغ سفارش</label>
<PriceInput
value={settings.MIN_ORDER_AMOUNT}
onChange={(e) => setSettings({ ...settings, MIN_ORDER_AMOUNT: e.target.value })}
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-purple-500 font-bold"
dir="ltr"
onChange={(val) => setSettings({ ...settings, MIN_ORDER_AMOUNT: val })}
placeholder="۰ = بدون محدودیت"
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-purple-500"
/>
<p className="text-xs text-gray-500 mt-2 font-medium">سفارشات زیر این مبلغ امکان ثبت نخواهند داشت (۰ = بدون محدودیت).</p>
</div>
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">پله رند کردن ردپای مهربانی (تومان)</label>
<input
type="number"
step="1000"
<label className="block text-sm font-bold text-gray-700 mb-2">پله رند کردن ردپای مهربانی</label>
<PriceInput
value={settings.CHARITY_ROUND_STEP}
onChange={(e) => setSettings({ ...settings, CHARITY_ROUND_STEP: e.target.value })}
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-purple-500 font-bold"
dir="ltr"
onChange={(val) => setSettings({ ...settings, CHARITY_ROUND_STEP: val })}
placeholder="10000"
className="border border-gray-200 rounded-xl p-3 focus:ring-2 focus:ring-purple-500"
/>
<p className="text-xs text-gray-500 mt-2 font-medium">مبلغ فاکتور به اولین ضریب این عدد (مثلاً ۱۰,۰۰۰ یا ۵۰,۰۰۰ تومان) رند می‌شود.</p>
</div>
<div>
<label className="block text-sm font-bold text-gray-700 mb-2">درصد مالیات بر ارزش افزوده</label>
<div className="relative">
<input
type="number"
value={settings.TAX_PERCENTAGE}
onChange={(e) => setSettings({ ...settings, TAX_PERCENTAGE: e.target.value })}
className="w-full border border-gray-200 rounded-xl p-3 outline-none focus:ring-2 focus:ring-purple-500 font-bold"
dir="ltr"
min="0"
max="100"
/>
<span className="absolute left-3 top-3.5 text-xs text-gray-400 font-bold">%</span>
</div>
</div>
</div>
</div>