feat: complete url deeplinking across all admin modal/tab pages
Deploy Canina / deploy (push) Successful in 30s
Deploy Canina / deploy (push) Successful in 30s
This commit is contained in:
1 parent
5029edaaeb
commit
fc324aacaf
4 files changed
+198
-36
No files matched your search
@@ -11,6 +11,8 @@ import Modal from '../components/ui/Modal';
|
||||
|
||||
|
||||
|
||||
import { useSearchParams } from 'react-router-dom';
|
||||
|
||||
export interface BlogPost {
|
||||
author?: {
|
||||
firstName?: string;
|
||||
@@ -29,10 +31,11 @@ export interface BlogPost {
|
||||
}
|
||||
|
||||
export default function Blogs() {
|
||||
const [searchParams, setSearchParams] = useSearchParams();
|
||||
const [blogs, setBlogs] = useState<BlogPost[]>([]);
|
||||
const [isLoading, setIsLoading] = useState(true);
|
||||
const [search, setSearch] = useState('');
|
||||
const [page, setPage] = useState(1);
|
||||
const [search, setSearch] = useState(() => searchParams.get('search') || '');
|
||||
const [page, setPage] = useState(() => Number(searchParams.get('page')) || 1);
|
||||
const [totalPages, setTotalPages] = useState(1);
|
||||
const limit = 10;
|
||||
|
||||
@@ -41,6 +44,16 @@ export default function Blogs() {
|
||||
const [editingBlog, setEditingBlog] = useState<BlogPost | null>(null);
|
||||
const [deleteTargetId, setDeleteTargetId] = useState<string | null>(null);
|
||||
|
||||
const updateUrlParams = (paramsObj: Record<string, string | number | undefined | null>) => {
|
||||
const current = Object.fromEntries(searchParams.entries());
|
||||
const merged = { ...current, ...paramsObj };
|
||||
const cleaned: Record<string, string> = {};
|
||||
Object.entries(merged).forEach(([k, v]) => {
|
||||
if (v !== undefined && v !== null && String(v).trim() !== '') cleaned[k] = String(v);
|
||||
});
|
||||
setSearchParams(cleaned, { replace: true });
|
||||
};
|
||||
|
||||
const [formData, setFormData] = useState({
|
||||
title: '',
|
||||
slug: '',
|
||||
@@ -57,15 +70,25 @@ export default function Blogs() {
|
||||
setIsLoading(true);
|
||||
const res = await api.get('/admin/blogs', { params: { page, limit, search } });
|
||||
if (res.data?.data) {
|
||||
setBlogs(res.data.data);
|
||||
const blogList: BlogPost[] = res.data.data;
|
||||
setBlogs(blogList);
|
||||
setTotalPages(res.data.meta?.lastPage || 1);
|
||||
|
||||
const modalParam = searchParams.get('modal');
|
||||
const blogId = searchParams.get('blogId');
|
||||
if (modalParam === 'create') {
|
||||
openModal(null);
|
||||
} else if (modalParam === 'edit' && blogId) {
|
||||
const found = blogList.find((b) => b.id === blogId);
|
||||
if (found) openModal(found);
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
console.error(err);
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
}
|
||||
}, [page, search]);
|
||||
}, [page, search, searchParams]);
|
||||
|
||||
useEffect(() => {
|
||||
const timer = setTimeout(() => {
|
||||
@@ -84,7 +107,7 @@ export default function Blogs() {
|
||||
await api.post('/admin/blogs', formData);
|
||||
toast.success('مقاله جدید با موفقیت ذخیره شد');
|
||||
}
|
||||
setIsModalOpen(false);
|
||||
closeModal();
|
||||
fetchBlogs();
|
||||
} catch (error) {
|
||||
console.error('Save failed', error);
|
||||
@@ -109,6 +132,7 @@ export default function Blogs() {
|
||||
const openModal = (blog: BlogPost | null = null) => {
|
||||
if (blog) {
|
||||
setEditingBlog(blog);
|
||||
updateUrlParams({ modal: 'edit', blogId: blog.id });
|
||||
setFormData({
|
||||
title: blog.title,
|
||||
slug: blog.slug,
|
||||
@@ -121,6 +145,7 @@ export default function Blogs() {
|
||||
});
|
||||
} else {
|
||||
setEditingBlog(null);
|
||||
updateUrlParams({ modal: 'create', blogId: undefined });
|
||||
setFormData({
|
||||
title: '', slug: '', content: '', isPublished: true, metaTitle: '', metaDescription: '', keywords: '', imageUrl: ''
|
||||
});
|
||||
@@ -128,6 +153,12 @@ export default function Blogs() {
|
||||
setIsModalOpen(true);
|
||||
};
|
||||
|
||||
const closeModal = () => {
|
||||
setIsModalOpen(false);
|
||||
updateUrlParams({ modal: undefined, blogId: undefined });
|
||||
};
|
||||
|
||||
|
||||
return (
|
||||
<div className="space-y-6">
|
||||
<div className="flex flex-col sm:flex-row justify-between gap-4">
|
||||
@@ -234,7 +265,7 @@ export default function Blogs() {
|
||||
{isModalOpen && (
|
||||
<Modal
|
||||
isOpen={isModalOpen}
|
||||
onClose={() => setIsModalOpen(false)}
|
||||
onClose={closeModal}
|
||||
title={editingBlog ? 'ویرایش مقاله' : 'افزودن مقاله جدید'}
|
||||
icon={FileText}
|
||||
maxWidth="4xl"
|
||||
@@ -244,10 +275,11 @@ export default function Blogs() {
|
||||
variant="secondary"
|
||||
size="sm"
|
||||
type="button"
|
||||
onClick={() => setIsModalOpen(false)}
|
||||
onClick={closeModal}
|
||||
>
|
||||
انصراف
|
||||
</Button>
|
||||
|
||||
<Button
|
||||
variant="primary"
|
||||
size="sm"
|
||||
|
||||
Reference in new issue
Block a user