feat: add BackButton, protect UserDashboard auth, fix product loading metadata title and add WebOTP auto-submit
This commit is contained in:
1 parent
2ce24b0473
commit
9870516256
9 files changed
+185
-51
No files matched your search
@@ -1,9 +1,11 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Search, Plus, Edit2, Trash2, FileText, Image as ImageIcon, CheckCircle2, XCircle } from 'lucide-react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import api, { BASE_DOMAIN } from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
import Pagination from '../components/ui/Pagination';
|
||||
import MediaSelector from '../components/ui/MediaSelector';
|
||||
import ConfirmModal from '../components/ui/ConfirmModal';
|
||||
|
||||
export default function Blogs() {
|
||||
const [blogs, setBlogs] = useState<any[]>([]);
|
||||
@@ -16,7 +18,8 @@ export default function Blogs() {
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isMediaSelectorOpen, setIsMediaSelectorOpen] = useState(false);
|
||||
const [editingBlog, setEditingBlog] = useState<any>(null);
|
||||
|
||||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
title: '',
|
||||
slug: '',
|
||||
@@ -55,24 +58,30 @@ export default function Blogs() {
|
||||
try {
|
||||
if (editingBlog) {
|
||||
await api.put(`/admin/blogs/${editingBlog.id}`, formData);
|
||||
toast.success('مقاله با موفقیت ویرایش شد');
|
||||
} else {
|
||||
await api.post('/admin/blogs', formData);
|
||||
toast.success('مقاله جدید با موفقیت ذخیره شد');
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
fetchBlogs();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Save failed', error);
|
||||
alert('خطا در ذخیره مقاله');
|
||||
toast.error('خطا در ذخیره مقاله: ' + (error.response?.data?.message || error.message || ''));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!window.confirm('آیا از حذف این مقاله مطمئن هستید؟')) return;
|
||||
const confirmDelete = async () => {
|
||||
if (!deleteTargetId) return;
|
||||
try {
|
||||
await api.delete(`/admin/blogs/${id}`);
|
||||
await api.delete(`/admin/blogs/${deleteTargetId}`);
|
||||
toast.success('مقاله با موفقیت حذف شد');
|
||||
fetchBlogs();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Delete failed', error);
|
||||
toast.error('خطا در حذف مقاله');
|
||||
} finally {
|
||||
setDeleteTargetId(null);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
import { Search, Plus, Edit2, Trash2, FolderTree, Image as ImageIcon, X } from 'lucide-react';
|
||||
import { toast } from 'react-hot-toast';
|
||||
import api, { BASE_DOMAIN } from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
import Pagination from '../components/ui/Pagination';
|
||||
import MediaSelector from '../components/ui/MediaSelector';
|
||||
import ConfirmModal from '../components/ui/ConfirmModal';
|
||||
|
||||
export default function Categories() {
|
||||
const [categories, setCategories] = useState<any[]>([]);
|
||||
@@ -16,7 +18,8 @@ export default function Categories() {
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isMediaSelectorOpen, setIsMediaSelectorOpen] = useState(false);
|
||||
const [editingCategory, setEditingCategory] = useState<any>(null);
|
||||
|
||||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
name: '',
|
||||
slug: '',
|
||||
@@ -54,24 +57,30 @@ export default function Categories() {
|
||||
try {
|
||||
if (editingCategory) {
|
||||
await api.put(`/admin/categories/${editingCategory.id}`, formData);
|
||||
toast.success('دستهبندی با موفقیت ویرایش شد');
|
||||
} else {
|
||||
await api.post('/admin/categories', formData);
|
||||
toast.success('دستهبندی جدید با موفقیت ایجاد شد');
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
fetchCategories();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Save failed', error);
|
||||
alert('خطا در ذخیره دستهبندی');
|
||||
toast.error('خطا در ذخیره دستهبندی: ' + (error.response?.data?.message || error.message || ''));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!window.confirm('آیا مطمئن هستید؟')) return;
|
||||
const confirmDelete = async () => {
|
||||
if (!deleteTargetId) return;
|
||||
try {
|
||||
await api.delete(`/admin/categories/${id}`);
|
||||
await api.delete(`/admin/categories/${deleteTargetId}`);
|
||||
toast.success('دستهبندی با موفقیت حذف شد');
|
||||
fetchCategories();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Delete failed', error);
|
||||
toast.error('خطا در حذف دستهبندی');
|
||||
} finally {
|
||||
setDeleteTargetId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -179,7 +188,7 @@ export default function Categories() {
|
||||
<button onClick={() => openModal(cat)} className="p-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors">
|
||||
<Edit2 className="w-4 h-4" />
|
||||
</button>
|
||||
<button onClick={() => handleDelete(cat.id)} className="p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors">
|
||||
<button onClick={() => setDeleteTargetId(cat.id)} className="p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors">
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
@@ -318,6 +327,14 @@ export default function Categories() {
|
||||
onClose={() => setIsMediaSelectorOpen(false)}
|
||||
onSelect={(url) => setFormData({...formData, imageUrl: url})}
|
||||
/>
|
||||
|
||||
<ConfirmModal
|
||||
isOpen={!!deleteTargetId}
|
||||
title="حذف دستهبندی"
|
||||
message="آیا از حذف این دستهبندی مطمئن هستید؟ این عملیات قابل بازگشت نیست."
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTargetId(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import api, { BASE_DOMAIN } from '../services/api';
|
||||
import Spinner from '../components/ui/Spinner';
|
||||
import Pagination from '../components/ui/Pagination';
|
||||
import MediaSelector from '../components/ui/MediaSelector';
|
||||
import ConfirmModal from '../components/ui/ConfirmModal';
|
||||
|
||||
export default function Products() {
|
||||
const [products, setProducts] = useState<any[]>([]);
|
||||
@@ -54,6 +55,7 @@ export default function Products() {
|
||||
const [isModalOpen, setIsModalOpen] = useState(false);
|
||||
const [isMediaSelectorOpen, setIsMediaSelectorOpen] = useState(false);
|
||||
const [editingProduct, setEditingProduct] = useState<any>(null);
|
||||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||||
const [activeTab, setActiveTab] = useState('general');
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
@@ -65,6 +67,7 @@ export default function Products() {
|
||||
shortDescription: '',
|
||||
categoryId: '',
|
||||
priceValue: 0,
|
||||
wholesalePrice: '' as number | string,
|
||||
priceDisplay: '',
|
||||
unit: '',
|
||||
packageSize: 0,
|
||||
@@ -169,26 +172,32 @@ export default function Products() {
|
||||
|
||||
if (editingProduct) {
|
||||
await api.put(`/admin/products/${editingProduct.id}`, finalPayload);
|
||||
toast.success('محصول با موفقیت ویرایش شد');
|
||||
} else {
|
||||
await api.post('/admin/products', finalPayload);
|
||||
toast.success('محصول با موفقیت ایجاد شد');
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
setImageErrors({});
|
||||
fetchData();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Save failed', error);
|
||||
alert('خطا در ذخیره محصول');
|
||||
toast.error('خطا در ذخیره محصول: ' + (error.response?.data?.message || error.message || ''));
|
||||
}
|
||||
};
|
||||
|
||||
const handleDelete = async (id: string) => {
|
||||
if (!window.confirm('آیا از حذف این محصول مطمئن هستید؟')) return;
|
||||
const confirmDelete = async () => {
|
||||
if (!deleteTargetId) return;
|
||||
try {
|
||||
await api.delete(`/admin/products/${id}`);
|
||||
await api.delete(`/admin/products/${deleteTargetId}`);
|
||||
toast.success('محصول با موفقیت حذف شد');
|
||||
setImageErrors({});
|
||||
fetchData();
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
console.error('Delete failed', error);
|
||||
toast.error('خطا در حذف محصول');
|
||||
} finally {
|
||||
setDeleteTargetId(null);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -297,7 +306,7 @@ export default function Products() {
|
||||
<button onClick={() => openModal(product)} className="p-2 text-blue-600 hover:bg-blue-50 rounded-lg transition-colors">
|
||||
<Edit2 className="w-4 h-4" />
|
||||
</button>
|
||||
<button onClick={() => handleDelete(product.id)} className="p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors">
|
||||
<button onClick={() => setDeleteTargetId(product.id)} className="p-2 text-red-600 hover:bg-red-50 rounded-lg transition-colors">
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
@@ -810,6 +819,14 @@ export default function Products() {
|
||||
}}
|
||||
selectedUrl={formData.imageUrl.startsWith('http') ? formData.imageUrl : formData.imageUrl ? `${BASE_DOMAIN}${formData.imageUrl}` : undefined}
|
||||
/>
|
||||
|
||||
<ConfirmModal
|
||||
isOpen={!!deleteTargetId}
|
||||
title="حذف محصول"
|
||||
message="آیا از حذف این محصول مطمئن هستید؟ این عملیات قابل بازگشت نیست."
|
||||
onConfirm={confirmDelete}
|
||||
onCancel={() => setDeleteTargetId(null)}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in new issue
Block a user