Compare commits
1 Commits
eb2cb72ea4
...
features/p
| Author | SHA1 | Date | |
|---|---|---|---|
| cba6c9ba67 |
2
doc/prompts/13. Pagination
Normal file
2
doc/prompts/13. Pagination
Normal file
@ -0,0 +1,2 @@
|
|||||||
|
Please implement pagination to all list pages in admin
|
||||||
|
create new branch and please work in the branch
|
||||||
@ -4,6 +4,7 @@ import { useState, useEffect, FormEvent } from 'react';
|
|||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { useRouter, useSearchParams } from 'next/navigation';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import { CONTACT_TYPES } from '@/lib/constants';
|
import { CONTACT_TYPES } from '@/lib/constants';
|
||||||
|
import Pagination from '@/lib/components/Pagination';
|
||||||
|
|
||||||
interface Customer {
|
interface Customer {
|
||||||
id: string;
|
id: string;
|
||||||
@ -19,6 +20,18 @@ interface ContactRecord {
|
|||||||
customer: Customer;
|
customer: Customer;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PaginationInfo {
|
||||||
|
page: number;
|
||||||
|
pageSize: number;
|
||||||
|
totalCount: number;
|
||||||
|
totalPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface ContactRecordsResponse {
|
||||||
|
data: ContactRecord[];
|
||||||
|
pagination: PaginationInfo;
|
||||||
|
}
|
||||||
|
|
||||||
export default function ContactRecordsList() {
|
export default function ContactRecordsList() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
@ -28,16 +41,26 @@ export default function ContactRecordsList() {
|
|||||||
const initialContactType = searchParams.get('contactType') || '';
|
const initialContactType = searchParams.get('contactType') || '';
|
||||||
const initialDateFrom = searchParams.get('dateFrom') || '';
|
const initialDateFrom = searchParams.get('dateFrom') || '';
|
||||||
const initialDateTo = searchParams.get('dateTo') || '';
|
const initialDateTo = searchParams.get('dateTo') || '';
|
||||||
|
const initialPage = parseInt(searchParams.get('page') || '1');
|
||||||
|
const initialPageSize = parseInt(searchParams.get('pageSize') || '10');
|
||||||
|
|
||||||
// State for filters
|
// State for filters
|
||||||
const [customerId, setCustomerId] = useState(initialCustomerId);
|
const [customerId, setCustomerId] = useState(initialCustomerId);
|
||||||
const [contactType, setContactType] = useState(initialContactType);
|
const [contactType, setContactType] = useState(initialContactType);
|
||||||
const [dateFrom, setDateFrom] = useState(initialDateFrom);
|
const [dateFrom, setDateFrom] = useState(initialDateFrom);
|
||||||
const [dateTo, setDateTo] = useState(initialDateTo);
|
const [dateTo, setDateTo] = useState(initialDateTo);
|
||||||
|
const [page, setPage] = useState(initialPage);
|
||||||
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
||||||
|
|
||||||
// State for data
|
// State for data
|
||||||
const [contactRecords, setContactRecords] = useState<ContactRecord[]>([]);
|
const [contactRecords, setContactRecords] = useState<ContactRecord[]>([]);
|
||||||
const [customers, setCustomers] = useState<Customer[]>([]);
|
const [customers, setCustomers] = useState<Customer[]>([]);
|
||||||
|
const [pagination, setPagination] = useState<PaginationInfo>({
|
||||||
|
page: initialPage,
|
||||||
|
pageSize: initialPageSize,
|
||||||
|
totalCount: 0,
|
||||||
|
totalPages: 0
|
||||||
|
});
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
const [error, setError] = useState<string | null>(null);
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
@ -60,19 +83,21 @@ export default function ContactRecordsList() {
|
|||||||
fetchCustomers();
|
fetchCustomers();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
// Fetch contact records with filters
|
// Fetch contact records with filters and pagination
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchContactRecords = async () => {
|
const fetchContactRecords = async () => {
|
||||||
setIsLoading(true);
|
setIsLoading(true);
|
||||||
setError(null);
|
setError(null);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
// Build query string with filters
|
// Build query string with filters and pagination
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (customerId) params.append('customerId', customerId);
|
if (customerId) params.append('customerId', customerId);
|
||||||
if (contactType) params.append('contactType', contactType);
|
if (contactType) params.append('contactType', contactType);
|
||||||
if (dateFrom) params.append('dateFrom', dateFrom);
|
if (dateFrom) params.append('dateFrom', dateFrom);
|
||||||
if (dateTo) params.append('dateTo', dateTo);
|
if (dateTo) params.append('dateTo', dateTo);
|
||||||
|
params.append('page', initialPage.toString());
|
||||||
|
params.append('pageSize', initialPageSize.toString());
|
||||||
|
|
||||||
const response = await fetch(`/api/contact-records?${params.toString()}`);
|
const response = await fetch(`/api/contact-records?${params.toString()}`);
|
||||||
|
|
||||||
@ -80,8 +105,9 @@ export default function ContactRecordsList() {
|
|||||||
throw new Error('Failed to fetch contact records');
|
throw new Error('Failed to fetch contact records');
|
||||||
}
|
}
|
||||||
|
|
||||||
const data = await response.json();
|
const responseData: ContactRecordsResponse = await response.json();
|
||||||
setContactRecords(data);
|
setContactRecords(responseData.data);
|
||||||
|
setPagination(responseData.pagination);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching contact records:', err);
|
console.error('Error fetching contact records:', err);
|
||||||
setError(err instanceof Error ? err.message : 'An error occurred');
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||||
@ -90,22 +116,21 @@ export default function ContactRecordsList() {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Only fetch if we have URL parameters or if this is the initial load
|
|
||||||
if (initialCustomerId || initialContactType || initialDateFrom || initialDateTo || isLoading) {
|
|
||||||
fetchContactRecords();
|
fetchContactRecords();
|
||||||
}
|
}, [initialCustomerId, initialContactType, initialDateFrom, initialDateTo, initialPage, initialPageSize]);
|
||||||
}, [initialCustomerId, initialContactType, initialDateFrom, initialDateTo]);
|
|
||||||
|
|
||||||
// Handle filter form submission
|
// Handle filter form submission
|
||||||
const handleFilterSubmit = (e: FormEvent) => {
|
const handleFilterSubmit = (e: FormEvent) => {
|
||||||
e.preventDefault();
|
e.preventDefault();
|
||||||
|
|
||||||
// Build query string with filters
|
// Reset to page 1 when applying new filters
|
||||||
const params = new URLSearchParams();
|
const params = new URLSearchParams();
|
||||||
if (customerId) params.append('customerId', customerId);
|
if (customerId) params.append('customerId', customerId);
|
||||||
if (contactType) params.append('contactType', contactType);
|
if (contactType) params.append('contactType', contactType);
|
||||||
if (dateFrom) params.append('dateFrom', dateFrom);
|
if (dateFrom) params.append('dateFrom', dateFrom);
|
||||||
if (dateTo) params.append('dateTo', dateTo);
|
if (dateTo) params.append('dateTo', dateTo);
|
||||||
|
params.append('page', '1'); // Reset to page 1
|
||||||
|
params.append('pageSize', pageSize.toString());
|
||||||
|
|
||||||
// Update URL with filters
|
// Update URL with filters
|
||||||
router.push(`/admin/contact-records?${params.toString()}`);
|
router.push(`/admin/contact-records?${params.toString()}`);
|
||||||
@ -117,7 +142,19 @@ export default function ContactRecordsList() {
|
|||||||
setContactType('');
|
setContactType('');
|
||||||
setDateFrom('');
|
setDateFrom('');
|
||||||
setDateTo('');
|
setDateTo('');
|
||||||
router.push('/admin/contact-records');
|
router.push(`/admin/contact-records?page=1&pageSize=${pageSize}`);
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
|
||||||
|
// Build query string with current filters and new page
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', newPage.toString());
|
||||||
|
|
||||||
|
// Update URL with new page
|
||||||
|
router.push(`/admin/contact-records?${params.toString()}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
@ -308,6 +345,14 @@ export default function ContactRecordsList() {
|
|||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
</div>
|
</div>
|
||||||
|
{/* Pagination */}
|
||||||
|
<Pagination
|
||||||
|
currentPage={pagination.page}
|
||||||
|
totalPages={pagination.totalPages}
|
||||||
|
totalItems={pagination.totalCount}
|
||||||
|
pageSize={pagination.pageSize}
|
||||||
|
onPageChange={handlePageChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</>
|
</>
|
||||||
|
|||||||
@ -1,20 +1,103 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { getDataSource, Customer } from '@/lib/database';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import DeleteButton from './DeleteButton';
|
import DeleteButton from './DeleteButton';
|
||||||
|
import Pagination from '@/lib/components/Pagination';
|
||||||
|
|
||||||
export default async function AdminCustomers() {
|
interface Customer {
|
||||||
// Fetch customers from the database
|
id: string;
|
||||||
const dataSource = await getDataSource();
|
name: string;
|
||||||
const customerRepository = dataSource.getRepository(Customer);
|
url: string;
|
||||||
|
email: string;
|
||||||
|
createdAt: string;
|
||||||
|
modifiedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
const customers = await customerRepository.find({
|
interface PaginationInfo {
|
||||||
order: { createdAt: 'DESC' }
|
page: number;
|
||||||
|
pageSize: number;
|
||||||
|
totalCount: number;
|
||||||
|
totalPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface CustomersResponse {
|
||||||
|
data: Customer[];
|
||||||
|
pagination: PaginationInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function AdminCustomers() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// Get pagination values from URL params
|
||||||
|
const initialPage = parseInt(searchParams.get('page') || '1');
|
||||||
|
const initialPageSize = parseInt(searchParams.get('pageSize') || '10');
|
||||||
|
|
||||||
|
// State for pagination
|
||||||
|
const [page, setPage] = useState(initialPage);
|
||||||
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
||||||
|
|
||||||
|
// State for data
|
||||||
|
const [customers, setCustomers] = useState<Customer[]>([]);
|
||||||
|
const [pagination, setPagination] = useState<PaginationInfo>({
|
||||||
|
page: initialPage,
|
||||||
|
pageSize: initialPageSize,
|
||||||
|
totalCount: 0,
|
||||||
|
totalPages: 0
|
||||||
});
|
});
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Fetch customers with pagination
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchCustomers = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Build query string with pagination
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('page', initialPage.toString());
|
||||||
|
params.append('pageSize', initialPageSize.toString());
|
||||||
|
|
||||||
|
const response = await fetch(`/api/customers?${params.toString()}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch customers');
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData: CustomersResponse = await response.json();
|
||||||
|
setCustomers(responseData.data);
|
||||||
|
setPagination(responseData.pagination);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching customers:', err);
|
||||||
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchCustomers();
|
||||||
|
}, [initialPage, initialPageSize]);
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
|
||||||
|
// Build query string with new page
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', newPage.toString());
|
||||||
|
|
||||||
|
// Update URL with new page
|
||||||
|
router.push(`/admin/customers?${params.toString()}`);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<h1 className="text-2xl font-semibold text-gray-900">Customers</h1>
|
<h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Customers</h1>
|
||||||
<Link
|
<Link
|
||||||
href="/admin/customers/new"
|
href="/admin/customers/new"
|
||||||
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||||
@ -23,46 +106,68 @@ export default async function AdminCustomers() {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Error Message */}
|
||||||
|
{error && (
|
||||||
|
<div className="bg-red-50 border-l-4 border-red-400 p-4 my-4 dark:bg-red-900/20 dark:border-red-500">
|
||||||
|
<div className="flex">
|
||||||
|
<div className="flex-shrink-0">
|
||||||
|
<svg className="h-5 w-5 text-red-400 dark:text-red-300" viewBox="0 0 20 20" fill="currentColor">
|
||||||
|
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3">
|
||||||
|
<p className="text-sm text-red-700 dark:text-red-300">{error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Loading State */}
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="text-center py-8">
|
||||||
|
<p className="text-gray-500 dark:text-gray-400">Loading customers...</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
<div className="mt-8 flex flex-col">
|
<div className="mt-8 flex flex-col">
|
||||||
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
||||||
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
||||||
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
||||||
<table className="min-w-full divide-y divide-gray-300">
|
<table className="min-w-full divide-y divide-gray-300 dark:divide-gray-700">
|
||||||
<thead className="bg-gray-50">
|
<thead className="bg-gray-50 dark:bg-gray-700">
|
||||||
<tr>
|
<tr>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-200 sm:pl-6"
|
||||||
>
|
>
|
||||||
ID
|
ID
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Name
|
Name
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
URL
|
URL
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Email
|
Email
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Created
|
Created
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Modified
|
Modified
|
||||||
</th>
|
</th>
|
||||||
@ -71,58 +176,58 @@ export default async function AdminCustomers() {
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-200 bg-white">
|
<tbody className="divide-y divide-gray-200 bg-white dark:bg-gray-800 dark:divide-gray-700">
|
||||||
{customers.length > 0 ? (
|
{customers.length > 0 ? (
|
||||||
customers.map((customer) => (
|
customers.map((customer) => (
|
||||||
<tr key={customer.id}>
|
<tr key={customer.id}>
|
||||||
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
|
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 dark:text-gray-200 sm:pl-6">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/customers/detail/${customer.id}`}
|
href={`/admin/customers/detail/${customer.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
|
||||||
>
|
>
|
||||||
{customer.id.substring(0, 8)}...
|
{customer.id.substring(0, 8)}...
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/customers/detail/${customer.id}`}
|
href={`/admin/customers/detail/${customer.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
|
||||||
>
|
>
|
||||||
{customer.name}
|
{customer.name}
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{customer.url ? (
|
{customer.url ? (
|
||||||
<a
|
<a
|
||||||
href={customer.url.startsWith('http') ? customer.url : `https://${customer.url}`}
|
href={customer.url.startsWith('http') ? customer.url : `https://${customer.url}`}
|
||||||
target="_blank"
|
target="_blank"
|
||||||
rel="noopener noreferrer"
|
rel="noopener noreferrer"
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
|
||||||
>
|
>
|
||||||
{customer.url}
|
{customer.url}
|
||||||
</a>
|
</a>
|
||||||
) : (
|
) : (
|
||||||
<span className="text-gray-400">-</span>
|
<span className="text-gray-400 dark:text-gray-500">-</span>
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
<a
|
<a
|
||||||
href={`mailto:${customer.email}`}
|
href={`mailto:${customer.email}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
|
||||||
>
|
>
|
||||||
{customer.email}
|
{customer.email}
|
||||||
</a>
|
</a>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(customer.createdAt).toLocaleDateString()}
|
{new Date(customer.createdAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(customer.modifiedAt).toLocaleDateString()}
|
{new Date(customer.modifiedAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/customers/edit/${customer.id}`}
|
href={`/admin/customers/edit/${customer.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900 mr-4"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
|
||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</Link>
|
</Link>
|
||||||
@ -132,17 +237,27 @@ export default async function AdminCustomers() {
|
|||||||
))
|
))
|
||||||
) : (
|
) : (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={7} className="py-4 pl-4 pr-3 text-sm text-gray-500 text-center">
|
<td colSpan={7} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
|
||||||
No customers found. Create your first customer!
|
No customers found. Create your first customer!
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
{/* Pagination */}
|
||||||
|
<Pagination
|
||||||
|
currentPage={pagination.page}
|
||||||
|
totalPages={pagination.totalPages}
|
||||||
|
totalItems={pagination.totalCount}
|
||||||
|
pageSize={pagination.pageSize}
|
||||||
|
onPageChange={handlePageChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +1,102 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import { getDataSource, EmailTemplate } from '@/lib/database';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import DeleteButton from './DeleteButton';
|
import DeleteButton from './DeleteButton';
|
||||||
|
import Pagination from '@/lib/components/Pagination';
|
||||||
|
|
||||||
export default async function AdminEmailTemplates() {
|
interface EmailTemplate {
|
||||||
// Fetch email templates from the database
|
id: string;
|
||||||
const dataSource = await getDataSource();
|
title: string;
|
||||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
content: string;
|
||||||
|
createdAt: string;
|
||||||
|
modifiedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
const emailTemplates = await emailTemplateRepository.find({
|
interface PaginationInfo {
|
||||||
order: { createdAt: 'DESC' }
|
page: number;
|
||||||
|
pageSize: number;
|
||||||
|
totalCount: number;
|
||||||
|
totalPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface EmailTemplatesResponse {
|
||||||
|
data: EmailTemplate[];
|
||||||
|
pagination: PaginationInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function AdminEmailTemplates() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// Get pagination values from URL params
|
||||||
|
const initialPage = parseInt(searchParams.get('page') || '1');
|
||||||
|
const initialPageSize = parseInt(searchParams.get('pageSize') || '10');
|
||||||
|
|
||||||
|
// State for pagination
|
||||||
|
const [page, setPage] = useState(initialPage);
|
||||||
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
||||||
|
|
||||||
|
// State for data
|
||||||
|
const [emailTemplates, setEmailTemplates] = useState<EmailTemplate[]>([]);
|
||||||
|
const [pagination, setPagination] = useState<PaginationInfo>({
|
||||||
|
page: initialPage,
|
||||||
|
pageSize: initialPageSize,
|
||||||
|
totalCount: 0,
|
||||||
|
totalPages: 0
|
||||||
});
|
});
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Fetch email templates with pagination
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchEmailTemplates = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Build query string with pagination
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('page', initialPage.toString());
|
||||||
|
params.append('pageSize', initialPageSize.toString());
|
||||||
|
|
||||||
|
const response = await fetch(`/api/email-templates?${params.toString()}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch email templates');
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData: EmailTemplatesResponse = await response.json();
|
||||||
|
setEmailTemplates(responseData.data);
|
||||||
|
setPagination(responseData.pagination);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching email templates:', err);
|
||||||
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchEmailTemplates();
|
||||||
|
}, [initialPage, initialPageSize]);
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
|
||||||
|
// Build query string with new page
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', newPage.toString());
|
||||||
|
|
||||||
|
// Update URL with new page
|
||||||
|
router.push(`/admin/email-templates?${params.toString()}`);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<h1 className="text-2xl font-semibold text-gray-900">Email Templates</h1>
|
<h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Email Templates</h1>
|
||||||
<Link
|
<Link
|
||||||
href="/admin/email-templates/new"
|
href="/admin/email-templates/new"
|
||||||
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||||
@ -23,30 +105,52 @@ export default async function AdminEmailTemplates() {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Error Message */}
|
||||||
|
{error && (
|
||||||
|
<div className="bg-red-50 border-l-4 border-red-400 p-4 my-4 dark:bg-red-900/20 dark:border-red-500">
|
||||||
|
<div className="flex">
|
||||||
|
<div className="flex-shrink-0">
|
||||||
|
<svg className="h-5 w-5 text-red-400 dark:text-red-300" viewBox="0 0 20 20" fill="currentColor">
|
||||||
|
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3">
|
||||||
|
<p className="text-sm text-red-700 dark:text-red-300">{error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Loading State */}
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="text-center py-8">
|
||||||
|
<p className="text-gray-500 dark:text-gray-400">Loading email templates...</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
<div className="mt-8 flex flex-col">
|
<div className="mt-8 flex flex-col">
|
||||||
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
||||||
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
||||||
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
||||||
<table className="min-w-full divide-y divide-gray-300">
|
<table className="min-w-full divide-y divide-gray-300 dark:divide-gray-700">
|
||||||
<thead className="bg-gray-50">
|
<thead className="bg-gray-50 dark:bg-gray-700">
|
||||||
<tr>
|
<tr>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
style={{ width: "60%" }}
|
style={{ width: "60%" }}
|
||||||
>
|
>
|
||||||
Title
|
Title
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
style={{ width: "10%" }}
|
style={{ width: "10%" }}
|
||||||
>
|
>
|
||||||
Created
|
Created
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
style={{ width: "10%" }}
|
style={{ width: "10%" }}
|
||||||
>
|
>
|
||||||
Modified
|
Modified
|
||||||
@ -56,31 +160,32 @@ export default async function AdminEmailTemplates() {
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-200 bg-white">
|
<tbody className="divide-y divide-gray-200 bg-white dark:bg-gray-800 dark:divide-gray-700">
|
||||||
{emailTemplates.length > 0 ? (
|
{emailTemplates.length > 0 ? (
|
||||||
emailTemplates.map((template) => (
|
emailTemplates.map((template) => (
|
||||||
<tr key={template.id}>
|
<tr key={template.id}>
|
||||||
|
<td className="py-4 px-3 text-sm text-gray-500 dark:text-gray-400">
|
||||||
<td className="py-4 px-3 text-sm text-gray-500">
|
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/email-templates/detail/${template.id}`}
|
href={`/admin/email-templates/detail/${template.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300"
|
||||||
>
|
>
|
||||||
{template.title}
|
{template.title}
|
||||||
</Link>
|
</Link>
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(template.createdAt).toLocaleDateString()}
|
{new Date(template.createdAt).toLocaleDateString()}
|
||||||
|
{' '}
|
||||||
{new Date(template.createdAt).toLocaleTimeString()}
|
{new Date(template.createdAt).toLocaleTimeString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(template.modifiedAt).toLocaleDateString()}
|
{new Date(template.modifiedAt).toLocaleDateString()}
|
||||||
|
{' '}
|
||||||
{new Date(template.modifiedAt).toLocaleTimeString()}
|
{new Date(template.modifiedAt).toLocaleTimeString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/email-templates/edit/${template.id}`}
|
href={`/admin/email-templates/edit/${template.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900 mr-4"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
|
||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</Link>
|
</Link>
|
||||||
@ -90,17 +195,27 @@ export default async function AdminEmailTemplates() {
|
|||||||
))
|
))
|
||||||
) : (
|
) : (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 text-center">
|
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
|
||||||
No email templates found. Create your first template!
|
No email templates found. Create your first template!
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
{/* Pagination */}
|
||||||
|
<Pagination
|
||||||
|
currentPage={pagination.page}
|
||||||
|
totalPages={pagination.totalPages}
|
||||||
|
totalItems={pagination.totalCount}
|
||||||
|
pageSize={pagination.pageSize}
|
||||||
|
onPageChange={handlePageChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,20 +1,108 @@
|
|||||||
import Link from 'next/link';
|
'use client';
|
||||||
import { Post, getDataSource } from '@/lib/database';
|
|
||||||
import DeleteButton from './DeleteButton';
|
|
||||||
|
|
||||||
export default async function AdminPosts() {
|
import { useState, useEffect } from 'react';
|
||||||
// Fetch posts from the database
|
import Link from 'next/link';
|
||||||
const dataSource = await getDataSource();
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
const postRepository = dataSource.getRepository(Post);
|
import DeleteButton from './DeleteButton';
|
||||||
const posts = await postRepository.find({
|
import Pagination from '@/lib/components/Pagination';
|
||||||
relations: ['user'],
|
|
||||||
order: { createdAt: 'DESC' }
|
interface User {
|
||||||
|
id: string;
|
||||||
|
username: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface Post {
|
||||||
|
id: string;
|
||||||
|
title: string;
|
||||||
|
content: string;
|
||||||
|
createdAt: string;
|
||||||
|
modifiedAt: string;
|
||||||
|
user: User;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PaginationInfo {
|
||||||
|
page: number;
|
||||||
|
pageSize: number;
|
||||||
|
totalCount: number;
|
||||||
|
totalPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface PostsResponse {
|
||||||
|
data: Post[];
|
||||||
|
pagination: PaginationInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function AdminPosts() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// Get pagination values from URL params
|
||||||
|
const initialPage = parseInt(searchParams.get('page') || '1');
|
||||||
|
const initialPageSize = parseInt(searchParams.get('pageSize') || '10');
|
||||||
|
|
||||||
|
// State for pagination
|
||||||
|
const [page, setPage] = useState(initialPage);
|
||||||
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
||||||
|
|
||||||
|
// State for data
|
||||||
|
const [posts, setPosts] = useState<Post[]>([]);
|
||||||
|
const [pagination, setPagination] = useState<PaginationInfo>({
|
||||||
|
page: initialPage,
|
||||||
|
pageSize: initialPageSize,
|
||||||
|
totalCount: 0,
|
||||||
|
totalPages: 0
|
||||||
});
|
});
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Fetch posts with pagination
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchPosts = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Build query string with pagination
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('page', initialPage.toString());
|
||||||
|
params.append('pageSize', initialPageSize.toString());
|
||||||
|
|
||||||
|
const response = await fetch(`/api/posts?${params.toString()}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch posts');
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData: PostsResponse = await response.json();
|
||||||
|
setPosts(responseData.data);
|
||||||
|
setPagination(responseData.pagination);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching posts:', err);
|
||||||
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchPosts();
|
||||||
|
}, [initialPage, initialPageSize]);
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
|
||||||
|
// Build query string with new page
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', newPage.toString());
|
||||||
|
|
||||||
|
// Update URL with new page
|
||||||
|
router.push(`/admin/posts?${params.toString()}`);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<h1 className="text-2xl font-semibold text-gray-900">Posts</h1>
|
<h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Posts</h1>
|
||||||
<Link
|
<Link
|
||||||
href="/admin/posts/new"
|
href="/admin/posts/new"
|
||||||
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||||
@ -23,34 +111,56 @@ export default async function AdminPosts() {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Error Message */}
|
||||||
|
{error && (
|
||||||
|
<div className="bg-red-50 border-l-4 border-red-400 p-4 my-4 dark:bg-red-900/20 dark:border-red-500">
|
||||||
|
<div className="flex">
|
||||||
|
<div className="flex-shrink-0">
|
||||||
|
<svg className="h-5 w-5 text-red-400 dark:text-red-300" viewBox="0 0 20 20" fill="currentColor">
|
||||||
|
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3">
|
||||||
|
<p className="text-sm text-red-700 dark:text-red-300">{error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Loading State */}
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="text-center py-8">
|
||||||
|
<p className="text-gray-500 dark:text-gray-400">Loading posts...</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
<div className="mt-8 flex flex-col">
|
<div className="mt-8 flex flex-col">
|
||||||
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
||||||
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
||||||
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
||||||
<table className="min-w-full divide-y divide-gray-300">
|
<table className="min-w-full divide-y divide-gray-300 dark:divide-gray-700">
|
||||||
<thead className="bg-gray-50">
|
<thead className="bg-gray-50 dark:bg-gray-700">
|
||||||
<tr>
|
<tr>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-200 sm:pl-6"
|
||||||
>
|
>
|
||||||
Title
|
Title
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Author
|
Author
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Created
|
Created
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Modified
|
Modified
|
||||||
</th>
|
</th>
|
||||||
@ -59,26 +169,26 @@ export default async function AdminPosts() {
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-200 bg-white">
|
<tbody className="divide-y divide-gray-200 bg-white dark:bg-gray-800 dark:divide-gray-700">
|
||||||
{posts.length > 0 ? (
|
{posts.length > 0 ? (
|
||||||
posts.map((post: Post) => (
|
posts.map((post) => (
|
||||||
<tr key={post.id}>
|
<tr key={post.id}>
|
||||||
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
|
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 dark:text-gray-200 sm:pl-6">
|
||||||
{post.title}
|
{post.title}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{post.user?.username || 'Unknown'}
|
{post.user?.username || 'Unknown'}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(post.createdAt).toLocaleDateString()}
|
{new Date(post.createdAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(post.modifiedAt).toLocaleDateString()}
|
{new Date(post.modifiedAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/posts/edit/${post.id}`}
|
href={`/admin/posts/edit/${post.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900 mr-4"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
|
||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</Link>
|
</Link>
|
||||||
@ -88,17 +198,27 @@ export default async function AdminPosts() {
|
|||||||
))
|
))
|
||||||
) : (
|
) : (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 text-center">
|
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
|
||||||
No posts found. Create your first post!
|
No posts found. Create your first post!
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
{/* Pagination */}
|
||||||
|
<Pagination
|
||||||
|
currentPage={pagination.page}
|
||||||
|
totalPages={pagination.totalPages}
|
||||||
|
totalItems={pagination.totalCount}
|
||||||
|
pageSize={pagination.pageSize}
|
||||||
|
onPageChange={handlePageChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,22 +1,103 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useState, useEffect } from 'react';
|
||||||
import Link from 'next/link';
|
import Link from 'next/link';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { getDataSource, User } from '@/lib/database';
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
import DeleteButton from './DeleteButton';
|
import DeleteButton from './DeleteButton';
|
||||||
|
import Pagination from '@/lib/components/Pagination';
|
||||||
|
|
||||||
export default async function AdminUsers() {
|
interface User {
|
||||||
// Fetch users from the database
|
id: string;
|
||||||
const dataSource = await getDataSource();
|
username: string;
|
||||||
const userRepository = dataSource.getRepository(User);
|
avatar: string | null;
|
||||||
|
createdAt: string;
|
||||||
|
modifiedAt: string;
|
||||||
|
}
|
||||||
|
|
||||||
const users = await userRepository.find({
|
interface PaginationInfo {
|
||||||
select: ['id', 'username', 'avatar', 'createdAt', 'modifiedAt'],
|
page: number;
|
||||||
order: { createdAt: 'DESC' }
|
pageSize: number;
|
||||||
|
totalCount: number;
|
||||||
|
totalPages: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface UsersResponse {
|
||||||
|
data: User[];
|
||||||
|
pagination: PaginationInfo;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function AdminUsers() {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// Get pagination values from URL params
|
||||||
|
const initialPage = parseInt(searchParams.get('page') || '1');
|
||||||
|
const initialPageSize = parseInt(searchParams.get('pageSize') || '10');
|
||||||
|
|
||||||
|
// State for pagination
|
||||||
|
const [page, setPage] = useState(initialPage);
|
||||||
|
const [pageSize, setPageSize] = useState(initialPageSize);
|
||||||
|
|
||||||
|
// State for data
|
||||||
|
const [users, setUsers] = useState<User[]>([]);
|
||||||
|
const [pagination, setPagination] = useState<PaginationInfo>({
|
||||||
|
page: initialPage,
|
||||||
|
pageSize: initialPageSize,
|
||||||
|
totalCount: 0,
|
||||||
|
totalPages: 0
|
||||||
});
|
});
|
||||||
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
// Fetch users with pagination
|
||||||
|
useEffect(() => {
|
||||||
|
const fetchUsers = async () => {
|
||||||
|
setIsLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Build query string with pagination
|
||||||
|
const params = new URLSearchParams();
|
||||||
|
params.append('page', initialPage.toString());
|
||||||
|
params.append('pageSize', initialPageSize.toString());
|
||||||
|
|
||||||
|
const response = await fetch(`/api/users?${params.toString()}`);
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error('Failed to fetch users');
|
||||||
|
}
|
||||||
|
|
||||||
|
const responseData: UsersResponse = await response.json();
|
||||||
|
setUsers(responseData.data);
|
||||||
|
setPagination(responseData.pagination);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error fetching users:', err);
|
||||||
|
setError(err instanceof Error ? err.message : 'An error occurred');
|
||||||
|
} finally {
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
fetchUsers();
|
||||||
|
}, [initialPage, initialPageSize]);
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
|
||||||
|
// Build query string with new page
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', newPage.toString());
|
||||||
|
|
||||||
|
// Update URL with new page
|
||||||
|
router.push(`/admin/users?${params.toString()}`);
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<div className="flex justify-between items-center">
|
<div className="flex justify-between items-center">
|
||||||
<h1 className="text-2xl font-semibold text-gray-900">Users</h1>
|
<h1 className="text-2xl font-semibold text-gray-900 dark:text-gray-100">Users</h1>
|
||||||
<Link
|
<Link
|
||||||
href="/admin/users/new"
|
href="/admin/users/new"
|
||||||
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
className="inline-flex items-center px-4 py-2 border border-transparent text-sm font-medium rounded-md shadow-sm text-white bg-indigo-600 hover:bg-indigo-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
|
||||||
@ -25,34 +106,56 @@ export default async function AdminUsers() {
|
|||||||
</Link>
|
</Link>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
{/* Error Message */}
|
||||||
|
{error && (
|
||||||
|
<div className="bg-red-50 border-l-4 border-red-400 p-4 my-4 dark:bg-red-900/20 dark:border-red-500">
|
||||||
|
<div className="flex">
|
||||||
|
<div className="flex-shrink-0">
|
||||||
|
<svg className="h-5 w-5 text-red-400 dark:text-red-300" viewBox="0 0 20 20" fill="currentColor">
|
||||||
|
<path fillRule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zM8.707 7.293a1 1 0 00-1.414 1.414L8.586 10l-1.293 1.293a1 1 0 101.414 1.414L10 11.414l1.293 1.293a1 1 0 001.414-1.414L11.414 10l1.293-1.293a1 1 0 00-1.414-1.414L10 8.586 8.707 7.293z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</div>
|
||||||
|
<div className="ml-3">
|
||||||
|
<p className="text-sm text-red-700 dark:text-red-300">{error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{/* Loading State */}
|
||||||
|
{isLoading ? (
|
||||||
|
<div className="text-center py-8">
|
||||||
|
<p className="text-gray-500 dark:text-gray-400">Loading users...</p>
|
||||||
|
</div>
|
||||||
|
) : (
|
||||||
<div className="mt-8 flex flex-col">
|
<div className="mt-8 flex flex-col">
|
||||||
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
<div className="-my-2 -mx-4 overflow-x-auto sm:-mx-6 lg:-mx-8">
|
||||||
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
<div className="inline-block min-w-full py-2 align-middle md:px-6 lg:px-8">
|
||||||
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
<div className="overflow-hidden shadow ring-1 ring-black ring-opacity-5 md:rounded-lg">
|
||||||
<table className="min-w-full divide-y divide-gray-300">
|
<table className="min-w-full divide-y divide-gray-300 dark:divide-gray-700">
|
||||||
<thead className="bg-gray-50">
|
<thead className="bg-gray-50 dark:bg-gray-700">
|
||||||
<tr>
|
<tr>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 sm:pl-6"
|
className="py-3.5 pl-4 pr-3 text-left text-sm font-semibold text-gray-900 dark:text-gray-200 sm:pl-6"
|
||||||
>
|
>
|
||||||
Username
|
Username
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Avatar
|
Avatar
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Created
|
Created
|
||||||
</th>
|
</th>
|
||||||
<th
|
<th
|
||||||
scope="col"
|
scope="col"
|
||||||
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900"
|
className="px-3 py-3.5 text-left text-sm font-semibold text-gray-900 dark:text-gray-200"
|
||||||
>
|
>
|
||||||
Modified
|
Modified
|
||||||
</th>
|
</th>
|
||||||
@ -61,14 +164,14 @@ export default async function AdminUsers() {
|
|||||||
</th>
|
</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
<tbody className="divide-y divide-gray-200 bg-white">
|
<tbody className="divide-y divide-gray-200 bg-white dark:bg-gray-800 dark:divide-gray-700">
|
||||||
{users.length > 0 ? (
|
{users.length > 0 ? (
|
||||||
users.map((user) => (
|
users.map((user) => (
|
||||||
<tr key={user.id}>
|
<tr key={user.id}>
|
||||||
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 sm:pl-6">
|
<td className="whitespace-nowrap py-4 pl-4 pr-3 text-sm font-medium text-gray-900 dark:text-gray-200 sm:pl-6">
|
||||||
{user.username}
|
{user.username}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{user.avatar ? (
|
{user.avatar ? (
|
||||||
<Image
|
<Image
|
||||||
src={user.avatar}
|
src={user.avatar}
|
||||||
@ -78,21 +181,21 @@ export default async function AdminUsers() {
|
|||||||
className="rounded-full"
|
className="rounded-full"
|
||||||
/>
|
/>
|
||||||
) : (
|
) : (
|
||||||
<div className="h-10 w-10 rounded-full bg-gray-200 flex items-center justify-center text-gray-500">
|
<div className="h-10 w-10 rounded-full bg-gray-200 dark:bg-gray-600 flex items-center justify-center text-gray-500 dark:text-gray-300">
|
||||||
{user.username.charAt(0).toUpperCase()}
|
{user.username.charAt(0).toUpperCase()}
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(user.createdAt).toLocaleDateString()}
|
{new Date(user.createdAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500">
|
<td className="whitespace-nowrap px-3 py-4 text-sm text-gray-500 dark:text-gray-400">
|
||||||
{new Date(user.modifiedAt).toLocaleDateString()}
|
{new Date(user.modifiedAt).toLocaleDateString()}
|
||||||
</td>
|
</td>
|
||||||
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
<td className="relative whitespace-nowrap py-4 pl-3 pr-4 text-right text-sm font-medium sm:pr-6">
|
||||||
<Link
|
<Link
|
||||||
href={`/admin/users/edit/${user.id}`}
|
href={`/admin/users/edit/${user.id}`}
|
||||||
className="text-indigo-600 hover:text-indigo-900 mr-4"
|
className="text-indigo-600 hover:text-indigo-900 dark:text-indigo-400 dark:hover:text-indigo-300 mr-4"
|
||||||
>
|
>
|
||||||
Edit
|
Edit
|
||||||
</Link>
|
</Link>
|
||||||
@ -102,17 +205,27 @@ export default async function AdminUsers() {
|
|||||||
))
|
))
|
||||||
) : (
|
) : (
|
||||||
<tr>
|
<tr>
|
||||||
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 text-center">
|
<td colSpan={5} className="py-4 pl-4 pr-3 text-sm text-gray-500 dark:text-gray-400 text-center">
|
||||||
No users found. Create your first user!
|
No users found. Create your first user!
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
)}
|
)}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
|
{/* Pagination */}
|
||||||
|
<Pagination
|
||||||
|
currentPage={pagination.page}
|
||||||
|
totalPages={pagination.totalPages}
|
||||||
|
totalItems={pagination.totalCount}
|
||||||
|
pageSize={pagination.pageSize}
|
||||||
|
onPageChange={handlePageChange}
|
||||||
|
/>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -14,49 +14,63 @@ export async function GET(request: NextRequest) {
|
|||||||
const dateFrom = url.searchParams.get('dateFrom');
|
const dateFrom = url.searchParams.get('dateFrom');
|
||||||
const dateTo = url.searchParams.get('dateTo');
|
const dateTo = url.searchParams.get('dateTo');
|
||||||
|
|
||||||
|
// Pagination parameters
|
||||||
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
||||||
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
// Build query
|
// Build query
|
||||||
const queryOptions: any = {
|
let queryBuilder = contactRecordRepository.createQueryBuilder('contactRecord')
|
||||||
order: { createdAt: 'DESC' },
|
.leftJoinAndSelect('contactRecord.customer', 'customer')
|
||||||
relations: ['customer']
|
.orderBy('contactRecord.createdAt', 'DESC');
|
||||||
};
|
|
||||||
|
|
||||||
// Build where clause
|
// Apply filters
|
||||||
let whereClause: any = {};
|
|
||||||
|
|
||||||
// Filter by customer if customerId is provided
|
|
||||||
if (customerId) {
|
if (customerId) {
|
||||||
whereClause.customerId = customerId;
|
queryBuilder = queryBuilder.andWhere('contactRecord.customerId = :customerId', { customerId });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by contact type if provided
|
|
||||||
if (contactType) {
|
if (contactType) {
|
||||||
whereClause.contactType = contactType;
|
queryBuilder = queryBuilder.andWhere('contactRecord.contactType = :contactType', { contactType });
|
||||||
}
|
}
|
||||||
|
|
||||||
// Filter by date range if provided
|
|
||||||
if (dateFrom || dateTo) {
|
|
||||||
whereClause.createdAt = {};
|
|
||||||
|
|
||||||
if (dateFrom) {
|
if (dateFrom) {
|
||||||
whereClause.createdAt.gte = new Date(dateFrom);
|
queryBuilder = queryBuilder.andWhere('contactRecord.createdAt >= :dateFrom', {
|
||||||
|
dateFrom: new Date(dateFrom)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (dateTo) {
|
if (dateTo) {
|
||||||
// Set the date to the end of the day for inclusive filtering
|
// Set the date to the end of the day for inclusive filtering
|
||||||
const endDate = new Date(dateTo);
|
const endDate = new Date(dateTo);
|
||||||
endDate.setHours(23, 59, 59, 999);
|
endDate.setHours(23, 59, 59, 999);
|
||||||
whereClause.createdAt.lte = endDate;
|
queryBuilder = queryBuilder.andWhere('contactRecord.createdAt <= :dateTo', {
|
||||||
}
|
dateTo: endDate
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add where clause to query options if not empty
|
// Get total count for pagination
|
||||||
if (Object.keys(whereClause).length > 0) {
|
const totalCount = await queryBuilder.getCount();
|
||||||
queryOptions.where = whereClause;
|
|
||||||
|
// Apply pagination
|
||||||
|
queryBuilder = queryBuilder
|
||||||
|
.skip(skip)
|
||||||
|
.take(pageSize);
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
const contactRecords = await queryBuilder.getMany();
|
||||||
|
|
||||||
|
// Calculate total pages
|
||||||
|
const totalPages = Math.ceil(totalCount / pageSize);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
data: contactRecords,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
totalCount,
|
||||||
|
totalPages
|
||||||
}
|
}
|
||||||
|
});
|
||||||
const contactRecords = await contactRecordRepository.find(queryOptions);
|
|
||||||
|
|
||||||
return NextResponse.json(contactRecords);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching contact records:', error);
|
console.error('Error fetching contact records:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -7,11 +7,41 @@ export async function GET(request: NextRequest) {
|
|||||||
const dataSource = await getDataSource();
|
const dataSource = await getDataSource();
|
||||||
const customerRepository = dataSource.getRepository(Customer);
|
const customerRepository = dataSource.getRepository(Customer);
|
||||||
|
|
||||||
const customers = await customerRepository.find({
|
// Get query parameters
|
||||||
order: { createdAt: 'DESC' }
|
const url = new URL(request.url);
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json(customers);
|
// Pagination parameters
|
||||||
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
||||||
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
|
// Build query
|
||||||
|
let queryBuilder = customerRepository.createQueryBuilder('customer')
|
||||||
|
.orderBy('customer.createdAt', 'DESC');
|
||||||
|
|
||||||
|
// Get total count for pagination
|
||||||
|
const totalCount = await queryBuilder.getCount();
|
||||||
|
|
||||||
|
// Apply pagination
|
||||||
|
queryBuilder = queryBuilder
|
||||||
|
.skip(skip)
|
||||||
|
.take(pageSize);
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
const customers = await queryBuilder.getMany();
|
||||||
|
|
||||||
|
// Calculate total pages
|
||||||
|
const totalPages = Math.ceil(totalCount / pageSize);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
data: customers,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
totalCount,
|
||||||
|
totalPages
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching customers:', error);
|
console.error('Error fetching customers:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -7,11 +7,41 @@ export async function GET(request: NextRequest) {
|
|||||||
const dataSource = await getDataSource();
|
const dataSource = await getDataSource();
|
||||||
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
const emailTemplateRepository = dataSource.getRepository(EmailTemplate);
|
||||||
|
|
||||||
const emailTemplates = await emailTemplateRepository.find({
|
// Get query parameters
|
||||||
order: { createdAt: 'DESC' }
|
const url = new URL(request.url);
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json(emailTemplates);
|
// Pagination parameters
|
||||||
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
||||||
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
|
// Build query
|
||||||
|
let queryBuilder = emailTemplateRepository.createQueryBuilder('emailTemplate')
|
||||||
|
.orderBy('emailTemplate.createdAt', 'DESC');
|
||||||
|
|
||||||
|
// Get total count for pagination
|
||||||
|
const totalCount = await queryBuilder.getCount();
|
||||||
|
|
||||||
|
// Apply pagination
|
||||||
|
queryBuilder = queryBuilder
|
||||||
|
.skip(skip)
|
||||||
|
.take(pageSize);
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
const emailTemplates = await queryBuilder.getMany();
|
||||||
|
|
||||||
|
// Calculate total pages
|
||||||
|
const totalPages = Math.ceil(totalCount / pageSize);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
data: emailTemplates,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
totalCount,
|
||||||
|
totalPages
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching email templates:', error);
|
console.error('Error fetching email templates:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -11,8 +11,13 @@ export async function GET(request: NextRequest) {
|
|||||||
const url = new URL(request.url);
|
const url = new URL(request.url);
|
||||||
const parentId = url.searchParams.get('parentId');
|
const parentId = url.searchParams.get('parentId');
|
||||||
|
|
||||||
|
// Pagination parameters
|
||||||
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
||||||
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
// Build query
|
// Build query
|
||||||
let query = postRepository.createQueryBuilder('post')
|
let queryBuilder = postRepository.createQueryBuilder('post')
|
||||||
.leftJoinAndSelect('post.user', 'user')
|
.leftJoinAndSelect('post.user', 'user')
|
||||||
.leftJoinAndSelect('post.parent', 'parent')
|
.leftJoinAndSelect('post.parent', 'parent')
|
||||||
.orderBy('post.createdAt', 'DESC');
|
.orderBy('post.createdAt', 'DESC');
|
||||||
@ -21,16 +26,36 @@ export async function GET(request: NextRequest) {
|
|||||||
if (parentId) {
|
if (parentId) {
|
||||||
if (parentId === 'null') {
|
if (parentId === 'null') {
|
||||||
// Get root posts (no parent)
|
// Get root posts (no parent)
|
||||||
query = query.where('post.parentId IS NULL');
|
queryBuilder = queryBuilder.where('post.parentId IS NULL');
|
||||||
} else {
|
} else {
|
||||||
// Get children of specific parent
|
// Get children of specific parent
|
||||||
query = query.where('post.parentId = :parentId', { parentId });
|
queryBuilder = queryBuilder.where('post.parentId = :parentId', { parentId });
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const posts = await query.getMany();
|
// Get total count for pagination
|
||||||
|
const totalCount = await queryBuilder.getCount();
|
||||||
|
|
||||||
return NextResponse.json(posts);
|
// Apply pagination
|
||||||
|
queryBuilder = queryBuilder
|
||||||
|
.skip(skip)
|
||||||
|
.take(pageSize);
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
const posts = await queryBuilder.getMany();
|
||||||
|
|
||||||
|
// Calculate total pages
|
||||||
|
const totalPages = Math.ceil(totalCount / pageSize);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
data: posts,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
totalCount,
|
||||||
|
totalPages
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching posts:', error);
|
console.error('Error fetching posts:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
@ -9,12 +9,42 @@ export async function GET(request: NextRequest) {
|
|||||||
const dataSource = await getDataSource();
|
const dataSource = await getDataSource();
|
||||||
const userRepository = dataSource.getRepository(User);
|
const userRepository = dataSource.getRepository(User);
|
||||||
|
|
||||||
const users = await userRepository.find({
|
// Get query parameters
|
||||||
select: ['id', 'username', 'avatar', 'createdAt', 'modifiedAt'],
|
const url = new URL(request.url);
|
||||||
order: { createdAt: 'DESC' }
|
|
||||||
});
|
|
||||||
|
|
||||||
return NextResponse.json(users);
|
// Pagination parameters
|
||||||
|
const page = parseInt(url.searchParams.get('page') || '1');
|
||||||
|
const pageSize = parseInt(url.searchParams.get('pageSize') || '10');
|
||||||
|
const skip = (page - 1) * pageSize;
|
||||||
|
|
||||||
|
// Build query
|
||||||
|
let queryBuilder = userRepository.createQueryBuilder('user')
|
||||||
|
.select(['user.id', 'user.username', 'user.avatar', 'user.createdAt', 'user.modifiedAt'])
|
||||||
|
.orderBy('user.createdAt', 'DESC');
|
||||||
|
|
||||||
|
// Get total count for pagination
|
||||||
|
const totalCount = await queryBuilder.getCount();
|
||||||
|
|
||||||
|
// Apply pagination
|
||||||
|
queryBuilder = queryBuilder
|
||||||
|
.skip(skip)
|
||||||
|
.take(pageSize);
|
||||||
|
|
||||||
|
// Execute query
|
||||||
|
const users = await queryBuilder.getMany();
|
||||||
|
|
||||||
|
// Calculate total pages
|
||||||
|
const totalPages = Math.ceil(totalCount / pageSize);
|
||||||
|
|
||||||
|
return NextResponse.json({
|
||||||
|
data: users,
|
||||||
|
pagination: {
|
||||||
|
page,
|
||||||
|
pageSize,
|
||||||
|
totalCount,
|
||||||
|
totalPages
|
||||||
|
}
|
||||||
|
});
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error fetching users:', error);
|
console.error('Error fetching users:', error);
|
||||||
return NextResponse.json(
|
return NextResponse.json(
|
||||||
|
|||||||
143
src/lib/components/Pagination/index.tsx
Normal file
143
src/lib/components/Pagination/index.tsx
Normal file
@ -0,0 +1,143 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { useRouter, useSearchParams } from 'next/navigation';
|
||||||
|
|
||||||
|
interface PaginationProps {
|
||||||
|
currentPage: number;
|
||||||
|
totalPages: number;
|
||||||
|
totalItems: number;
|
||||||
|
pageSize: number;
|
||||||
|
onPageChange?: (page: number) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default function Pagination({
|
||||||
|
currentPage,
|
||||||
|
totalPages,
|
||||||
|
totalItems,
|
||||||
|
pageSize,
|
||||||
|
onPageChange
|
||||||
|
}: PaginationProps) {
|
||||||
|
const router = useRouter();
|
||||||
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
|
// Calculate the range of items being displayed
|
||||||
|
const startItem = (currentPage - 1) * pageSize + 1;
|
||||||
|
const endItem = Math.min(currentPage * pageSize, totalItems);
|
||||||
|
|
||||||
|
// Generate page numbers to display
|
||||||
|
const getPageNumbers = () => {
|
||||||
|
const pages = [];
|
||||||
|
const maxPagesToShow = 5; // Show at most 5 page numbers
|
||||||
|
|
||||||
|
let startPage = Math.max(1, currentPage - Math.floor(maxPagesToShow / 2));
|
||||||
|
let endPage = startPage + maxPagesToShow - 1;
|
||||||
|
|
||||||
|
if (endPage > totalPages) {
|
||||||
|
endPage = totalPages;
|
||||||
|
startPage = Math.max(1, endPage - maxPagesToShow + 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (let i = startPage; i <= endPage; i++) {
|
||||||
|
pages.push(i);
|
||||||
|
}
|
||||||
|
|
||||||
|
return pages;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Handle page change
|
||||||
|
const handlePageChange = (page: number) => {
|
||||||
|
if (page < 1 || page > totalPages) return;
|
||||||
|
|
||||||
|
if (onPageChange) {
|
||||||
|
onPageChange(page);
|
||||||
|
} else {
|
||||||
|
// Update URL with new page parameter
|
||||||
|
const params = new URLSearchParams(searchParams.toString());
|
||||||
|
params.set('page', page.toString());
|
||||||
|
router.push(`?${params.toString()}`);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if (totalPages <= 1) return null;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="flex items-center justify-between border-t border-gray-200 bg-white px-4 py-3 sm:px-6 dark:bg-gray-800 dark:border-gray-700">
|
||||||
|
<div className="flex flex-1 justify-between sm:hidden">
|
||||||
|
<button
|
||||||
|
onClick={() => handlePageChange(currentPage - 1)}
|
||||||
|
disabled={currentPage === 1}
|
||||||
|
className={`relative inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200 ${currentPage === 1
|
||||||
|
? 'opacity-50 cursor-not-allowed'
|
||||||
|
: 'hover:bg-gray-50 dark:hover:bg-gray-600'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
Previous
|
||||||
|
</button>
|
||||||
|
<button
|
||||||
|
onClick={() => handlePageChange(currentPage + 1)}
|
||||||
|
disabled={currentPage === totalPages}
|
||||||
|
className={`relative ml-3 inline-flex items-center rounded-md border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 dark:bg-gray-700 dark:border-gray-600 dark:text-gray-200 ${currentPage === totalPages
|
||||||
|
? 'opacity-50 cursor-not-allowed'
|
||||||
|
: 'hover:bg-gray-50 dark:hover:bg-gray-600'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
Next
|
||||||
|
</button>
|
||||||
|
</div>
|
||||||
|
<div className="hidden sm:flex sm:flex-1 sm:items-center sm:justify-between">
|
||||||
|
<div>
|
||||||
|
<p className="text-sm text-gray-700 dark:text-gray-300">
|
||||||
|
Showing <span className="font-medium">{startItem}</span> to{' '}
|
||||||
|
<span className="font-medium">{endItem}</span> of{' '}
|
||||||
|
<span className="font-medium">{totalItems}</span> results
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<nav className="isolate inline-flex -space-x-px rounded-md shadow-sm" aria-label="Pagination">
|
||||||
|
<button
|
||||||
|
onClick={() => handlePageChange(currentPage - 1)}
|
||||||
|
disabled={currentPage === 1}
|
||||||
|
className={`relative inline-flex items-center rounded-l-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 dark:ring-gray-600 ${currentPage === 1
|
||||||
|
? 'opacity-50 cursor-not-allowed'
|
||||||
|
: 'hover:bg-gray-50 dark:hover:bg-gray-700 focus:z-20 focus:outline-offset-0'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<span className="sr-only">Previous</span>
|
||||||
|
<svg className="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
|
||||||
|
<path fillRule="evenodd" d="M12.79 5.23a.75.75 0 01-.02 1.06L8.832 10l3.938 3.71a.75.75 0 11-1.04 1.08l-4.5-4.25a.75.75 0 010-1.08l4.5-4.25a.75.75 0 011.06.02z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
|
||||||
|
{getPageNumbers().map((page) => (
|
||||||
|
<button
|
||||||
|
key={page}
|
||||||
|
onClick={() => handlePageChange(page)}
|
||||||
|
aria-current={page === currentPage ? 'page' : undefined}
|
||||||
|
className={`relative inline-flex items-center px-4 py-2 text-sm font-semibold ${page === currentPage
|
||||||
|
? 'z-10 bg-indigo-600 text-white focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600'
|
||||||
|
: 'text-gray-900 ring-1 ring-inset ring-gray-300 hover:bg-gray-50 focus:z-20 focus:outline-offset-0 dark:text-gray-200 dark:ring-gray-600 dark:hover:bg-gray-700'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
{page}
|
||||||
|
</button>
|
||||||
|
))}
|
||||||
|
|
||||||
|
<button
|
||||||
|
onClick={() => handlePageChange(currentPage + 1)}
|
||||||
|
disabled={currentPage === totalPages}
|
||||||
|
className={`relative inline-flex items-center rounded-r-md px-2 py-2 text-gray-400 ring-1 ring-inset ring-gray-300 dark:ring-gray-600 ${currentPage === totalPages
|
||||||
|
? 'opacity-50 cursor-not-allowed'
|
||||||
|
: 'hover:bg-gray-50 dark:hover:bg-gray-700 focus:z-20 focus:outline-offset-0'
|
||||||
|
}`}
|
||||||
|
>
|
||||||
|
<span className="sr-only">Next</span>
|
||||||
|
<svg className="h-5 w-5" viewBox="0 0 20 20" fill="currentColor" aria-hidden="true">
|
||||||
|
<path fillRule="evenodd" d="M7.21 14.77a.75.75 0 01.02-1.06L11.168 10 7.23 6.29a.75.75 0 111.04-1.08l4.5 4.25a.75.75 0 010 1.08l-4.5 4.25a.75.75 0 01-1.06-.02z" clipRule="evenodd" />
|
||||||
|
</svg>
|
||||||
|
</button>
|
||||||
|
</nav>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user