This commit is contained in:
2026-05-12 17:35:38 +03:00
parent d9ffcb4b92
commit e3f3e62482
51 changed files with 882 additions and 3413 deletions
+41 -41
View File
@@ -1,24 +1,24 @@
'use server';
"use server";
import { z } from 'zod';
import { revalidatePath } from 'next/cache';
import { redirect } from 'next/navigation';
import postgres from 'postgres';
import { signIn } from '@/auth';
import { AuthError } from 'next-auth';
import { revalidatePath } from "next/cache";
import { redirect } from "next/navigation";
import { AuthError } from "next-auth";
import postgres from "postgres";
import { z } from "zod";
import { signIn } from "@/auth";
const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });
const sql = postgres(process.env.POSTGRES_URL!, { ssl: "require" });
const FormSchema = z.object({
id: z.string(),
customerId: z.string({
invalid_type_error: 'Please select a customer',
invalid_type_error: "Please select a customer",
}),
amount: z.coerce
.number()
.gt(0, { message: 'Please enter an amount greater than $0.' }),
status: z.enum(['pending', 'paid'], {
invalid_type_error: 'Please select an invoice status. ',
.gt(0, { message: "Please enter an amount greater than $0." }),
status: z.enum(["pending", "paid"], {
invalid_type_error: "Please select an invoice status. ",
}),
date: z.string(),
});
@@ -35,53 +35,53 @@ export type State = {
message?: string | null;
};
export async function createInvoice(prevState: State, formData: FormData) {
export async function createInvoice(_prevState: State, formData: FormData) {
const validateFields = CreateInvoice.safeParse({
customerId: formData.get('customerId'),
amount: formData.get('amount'),
status: formData.get('status'),
customerId: formData.get("customerId"),
amount: formData.get("amount"),
status: formData.get("status"),
});
if (!validateFields.success) {
return {
errors: validateFields.error.flatten().fieldErrors,
message: 'Missing Fields. Failed to Create Invoice. ',
message: "Missing Fields. Failed to Create Invoice. ",
};
}
const { customerId, amount, status } = validateFields.data;
const amountInCents = amount * 100;
const date = new Date().toISOString().split('T')[0];
const date = new Date().toISOString().split("T")[0];
try {
await sql`
INSERT INTO invoices (customer_id, amount, status, date)
VALUES (${customerId}, ${amountInCents}, ${status}, ${date})
`;
} catch (error) {
} catch (_error) {
return {
message: 'Database Error: Failed to Create Invoice.',
message: "Database Error: Failed to Create Invoice.",
};
}
revalidatePath('/dashboard/invoices');
redirect('/dashboard/invoices');
revalidatePath("/dashboard/invoices");
redirect("/dashboard/invoices");
}
export async function updateInvoice(
id: string,
prevState: State,
_prevState: State,
formData: FormData,
) {
const validateFields = UpdateInvoice.safeParse({
customerId: formData.get('customerId'),
amount: formData.get('amount'),
status: formData.get('status'),
customerId: formData.get("customerId"),
amount: formData.get("amount"),
status: formData.get("status"),
});
if (!validateFields.success) {
return {
errors: validateFields.error.flatten().fieldErrors,
message: 'Missing Fields. Failed to update Invoice',
message: "Missing Fields. Failed to update Invoice",
};
}
@@ -93,14 +93,14 @@ export async function updateInvoice(
SET customer_id = ${customerId}, amount = ${amountInCents}, status = ${status}
WHERE id = ${id}
`;
} catch (error) {
} catch (_error) {
return {
message: 'Database Failed: Error while updating the Invoice',
message: "Database Failed: Error while updating the Invoice",
};
}
revalidatePath('/dashboard/invoices');
redirect('/dashboard/invoices');
revalidatePath("/dashboard/invoices");
redirect("/dashboard/invoices");
}
export async function deleteInvoice(id: string) {
@@ -110,30 +110,30 @@ export async function deleteInvoice(id: string) {
DELETE FROM invoices
WHERE id = ${id}
`;
revalidatePath('/dashboard/invoices');
return { message: 'Deleted Invoice. ' };
} catch (error) {
revalidatePath("/dashboard/invoices");
return { message: "Deleted Invoice. " };
} catch (_error) {
return {
message: 'Database Error: Failed while deleting record from Invoice. ',
message: "Database Error: Failed while deleting record from Invoice. ",
};
}
}
export async function authenticate(
prevState: string | undefined,
_prevState: string | undefined,
formData: FormData,
) {
try {
await signIn('credentials', formData);
await signIn("credentials", formData);
} catch (error) {
if (error instanceof AuthError) {
switch (error.type) {
case 'CredentialsSignin':
return 'Invalid credentials';
case "CredentialsSignin":
return "Invalid credentials";
default:
return 'Something went wrong.';
return "Something went wrong.";
}
}
throw error;
}
}
}
+33 -32
View File
@@ -1,15 +1,16 @@
import postgres from 'postgres';
import {
import postgres from "postgres";
import type {
CustomerField,
CustomersTable,
InvoiceForm,
InvoicesTable,
LatestInvoiceRaw,
User,
Revenue,
} from './definitions';
import { formatCurrency } from './utils';
const sql = postgres(process.env.POSTGRES_URL!, { ssl: 'require' });
User,
} from "./definitions";
import { formatCurrency } from "./utils";
const sql = postgres(process.env.POSTGRES_URL!, { ssl: "require" });
export async function fetchRevenue() {
// Add noStore() here prevent the response from being cached.
// This is equivalent to in fetch(..., {cache: 'no-store'}).
@@ -18,17 +19,17 @@ export async function fetchRevenue() {
// Artificially delay a reponse for demo purposes.
// Don't do this in real life :)
console.log('Fetching revenue data...');
await new Promise((resolve) => setTimeout(resolve, 3000));
console.log("Fetching revenue data...");
await new Promise((resolve) => setTimeout(resolve, 3000));
const data = sql<Revenue[]>`SELECT * FROM revenue`;
const data = sql<Revenue[]>`SELECT * FROM revenue`;
console.log('Data fetch complete after 3 seconds.');
console.log("Data fetch complete after 3 seconds.");
return data;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch revenue data.');
console.error("Database Error:", error);
throw new Error("Failed to fetch revenue data.");
}
}
@@ -47,8 +48,8 @@ export async function fetchLatestInvoices() {
}));
return latestInvoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch the latest invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch the latest invoices.");
}
}
@@ -70,10 +71,10 @@ export async function fetchCardData() {
invoiceStatusPromise,
]);
const numberOfInvoices = Number(data[0][0].count ?? '0');
const numberOfCustomers = Number(data[1][0].count ?? '0');
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? '0');
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? '0');
const numberOfInvoices = Number(data[0][0].count ?? "0");
const numberOfCustomers = Number(data[1][0].count ?? "0");
const totalPaidInvoices = formatCurrency(data[2][0].paid ?? "0");
const totalPendingInvoices = formatCurrency(data[2][0].pending ?? "0");
return {
numberOfCustomers,
@@ -82,8 +83,8 @@ export async function fetchCardData() {
totalPendingInvoices,
};
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to card data.');
console.error("Database Error:", error);
throw new Error("Failed to card data.");
}
}
@@ -118,8 +119,8 @@ export async function fetchFilteredInvoices(
return invoices;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch invoices.");
}
}
@@ -139,8 +140,8 @@ export async function fetchInvoicesPages(query: string) {
const totalPages = Math.ceil(Number(count[0].count) / ITEMS_PER_PAGE);
return totalPages;
} catch (error) {
console.error('Database Error:', error);
throw new Error('Failed to fetch total number of invoices.');
console.error("Database Error:", error);
throw new Error("Failed to fetch total number of invoices.");
}
}
@@ -165,7 +166,7 @@ export async function fetchInvoiceById(id: string) {
console.log(invoice); // Invoice is an empty array []
return invoice[0];
} catch (error) {
console.error('Database Error:', error);
console.error("Database Error:", error);
}
}
@@ -182,8 +183,8 @@ export async function fetchCustomers() {
const customers = data;
return customers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch all customers.');
console.error("Database Error:", err);
throw new Error("Failed to fetch all customers.");
}
}
@@ -215,8 +216,8 @@ export async function fetchFilteredCustomers(query: string) {
return customers;
} catch (err) {
console.error('Database Error:', err);
throw new Error('Failed to fetch customer table.');
console.error("Database Error:", err);
throw new Error("Failed to fetch customer table.");
}
}
@@ -225,7 +226,7 @@ export async function getUser(email: string) {
const user = await sql`SELECT * from USERS where email=${email}`;
return user[0] as User;
} catch (error) {
console.error('Failed to fetch user:', error);
throw new Error('Failed to fetch user.');
console.error("Failed to fetch user:", error);
throw new Error("Failed to fetch user.");
}
}
}
+4 -4
View File
@@ -23,7 +23,7 @@ export type Invoice = {
date: string;
// In TypeScript, this is called a string union type.
// It means that the "status" property can only be one of the two strings: 'pending' or 'paid'.
status: 'pending' | 'paid';
status: "pending" | "paid";
};
export type Revenue = {
@@ -40,7 +40,7 @@ export type LatestInvoice = {
};
// The database returns a number for amount, but we later format it to a string with the formatCurrency function
export type LatestInvoiceRaw = Omit<LatestInvoice, 'amount'> & {
export type LatestInvoiceRaw = Omit<LatestInvoice, "amount"> & {
amount: number;
};
@@ -52,7 +52,7 @@ export type InvoicesTable = {
image_url: string;
date: string;
amount: number;
status: 'pending' | 'paid';
status: "pending" | "paid";
};
export type CustomersTable = {
@@ -84,5 +84,5 @@ export type InvoiceForm = {
id: string;
customer_id: string;
amount: number;
status: 'pending' | 'paid';
status: "pending" | "paid";
};
+86 -86
View File
@@ -2,73 +2,73 @@
// https://nextjs.org/learn/dashboard-app/fetching-data
const users = [
{
id: '410544b2-4001-4271-9855-fec4b6a6442a',
name: 'User',
email: 'user@nextmail.com',
password: '123456',
id: "410544b2-4001-4271-9855-fec4b6a6442a",
name: "User",
email: "user@nextmail.com",
password: "123456",
},
];
const customers = [
{
id: '3958dc9e-712f-4377-85e9-fec4b6a6442a',
name: 'Delba de Oliveira',
email: 'delba@oliveira.com',
image_url: '/customers/delba-de-oliveira.png',
id: "3958dc9e-712f-4377-85e9-fec4b6a6442a",
name: "Delba de Oliveira",
email: "delba@oliveira.com",
image_url: "/customers/delba-de-oliveira.png",
},
{
id: '3958dc9e-742f-4377-85e9-fec4b6a6442a',
name: 'Lee Robinson',
email: 'lee@robinson.com',
image_url: '/customers/lee-robinson.png',
id: "3958dc9e-742f-4377-85e9-fec4b6a6442a",
name: "Lee Robinson",
email: "lee@robinson.com",
image_url: "/customers/lee-robinson.png",
},
{
id: '3958dc9e-737f-4377-85e9-fec4b6a6442a',
name: 'Hector Simpson',
email: 'hector@simpson.com',
image_url: '/customers/hector-simpson.png',
id: "3958dc9e-737f-4377-85e9-fec4b6a6442a",
name: "Hector Simpson",
email: "hector@simpson.com",
image_url: "/customers/hector-simpson.png",
},
{
id: '50ca3e18-62cd-11ee-8c99-0242ac120002',
name: 'Steven Tey',
email: 'steven@tey.com',
image_url: '/customers/steven-tey.png',
id: "50ca3e18-62cd-11ee-8c99-0242ac120002",
name: "Steven Tey",
email: "steven@tey.com",
image_url: "/customers/steven-tey.png",
},
{
id: '3958dc9e-787f-4377-85e9-fec4b6a6442a',
name: 'Steph Dietz',
email: 'steph@dietz.com',
image_url: '/customers/steph-dietz.png',
id: "3958dc9e-787f-4377-85e9-fec4b6a6442a",
name: "Steph Dietz",
email: "steph@dietz.com",
image_url: "/customers/steph-dietz.png",
},
{
id: '76d65c26-f784-44a2-ac19-586678f7c2f2',
name: 'Michael Novotny',
email: 'michael@novotny.com',
image_url: '/customers/michael-novotny.png',
id: "76d65c26-f784-44a2-ac19-586678f7c2f2",
name: "Michael Novotny",
email: "michael@novotny.com",
image_url: "/customers/michael-novotny.png",
},
{
id: 'd6e15727-9fe1-4961-8c5b-ea44a9bd81aa',
name: 'Evil Rabbit',
email: 'evil@rabbit.com',
image_url: '/customers/evil-rabbit.png',
id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81aa",
name: "Evil Rabbit",
email: "evil@rabbit.com",
image_url: "/customers/evil-rabbit.png",
},
{
id: '126eed9c-c90c-4ef6-a4a8-fcf7408d3c66',
name: 'Emil Kowalski',
email: 'emil@kowalski.com',
image_url: '/customers/emil-kowalski.png',
id: "126eed9c-c90c-4ef6-a4a8-fcf7408d3c66",
name: "Emil Kowalski",
email: "emil@kowalski.com",
image_url: "/customers/emil-kowalski.png",
},
{
id: 'CC27C14A-0ACF-4F4A-A6C9-D45682C144B9',
name: 'Amy Burns',
email: 'amy@burns.com',
image_url: '/customers/amy-burns.png',
id: "CC27C14A-0ACF-4F4A-A6C9-D45682C144B9",
name: "Amy Burns",
email: "amy@burns.com",
image_url: "/customers/amy-burns.png",
},
{
id: '13D07535-C59E-4157-A011-F8D2EF4E0CBB',
name: 'Balazs Orban',
email: 'balazs@orban.com',
image_url: '/customers/balazs-orban.png',
id: "13D07535-C59E-4157-A011-F8D2EF4E0CBB",
name: "Balazs Orban",
email: "balazs@orban.com",
image_url: "/customers/balazs-orban.png",
},
];
@@ -76,108 +76,108 @@ const invoices = [
{
customer_id: customers[0].id,
amount: 15795,
status: 'pending',
date: '2022-12-06',
status: "pending",
date: "2022-12-06",
},
{
customer_id: customers[1].id,
amount: 20348,
status: 'pending',
date: '2022-11-14',
status: "pending",
date: "2022-11-14",
},
{
customer_id: customers[4].id,
amount: 3040,
status: 'paid',
date: '2022-10-29',
status: "paid",
date: "2022-10-29",
},
{
customer_id: customers[3].id,
amount: 44800,
status: 'paid',
date: '2023-09-10',
status: "paid",
date: "2023-09-10",
},
{
customer_id: customers[5].id,
amount: 34577,
status: 'pending',
date: '2023-08-05',
status: "pending",
date: "2023-08-05",
},
{
customer_id: customers[7].id,
amount: 54246,
status: 'pending',
date: '2023-07-16',
status: "pending",
date: "2023-07-16",
},
{
customer_id: customers[6].id,
amount: 666,
status: 'pending',
date: '2023-06-27',
status: "pending",
date: "2023-06-27",
},
{
customer_id: customers[3].id,
amount: 32545,
status: 'paid',
date: '2023-06-09',
status: "paid",
date: "2023-06-09",
},
{
customer_id: customers[4].id,
amount: 1250,
status: 'paid',
date: '2023-06-17',
status: "paid",
date: "2023-06-17",
},
{
customer_id: customers[5].id,
amount: 8546,
status: 'paid',
date: '2023-06-07',
status: "paid",
date: "2023-06-07",
},
{
customer_id: customers[1].id,
amount: 500,
status: 'paid',
date: '2023-08-19',
status: "paid",
date: "2023-08-19",
},
{
customer_id: customers[5].id,
amount: 8945,
status: 'paid',
date: '2023-06-03',
status: "paid",
date: "2023-06-03",
},
{
customer_id: customers[2].id,
amount: 8945,
status: 'paid',
date: '2023-06-18',
status: "paid",
date: "2023-06-18",
},
{
customer_id: customers[0].id,
amount: 8945,
status: 'paid',
date: '2023-10-04',
status: "paid",
date: "2023-10-04",
},
{
customer_id: customers[2].id,
amount: 1000,
status: 'paid',
date: '2022-06-05',
status: "paid",
date: "2022-06-05",
},
];
const revenue = [
{ month: 'Jan', revenue: 2000 },
{ month: 'Feb', revenue: 1800 },
{ month: 'Mar', revenue: 2200 },
{ month: 'Apr', revenue: 2500 },
{ month: 'May', revenue: 2300 },
{ month: 'Jun', revenue: 3200 },
{ month: 'Jul', revenue: 3500 },
{ month: 'Aug', revenue: 3700 },
{ month: 'Sep', revenue: 2500 },
{ month: 'Oct', revenue: 2800 },
{ month: 'Nov', revenue: 3000 },
{ month: 'Dec', revenue: 4800 },
{ month: "Jan", revenue: 2000 },
{ month: "Feb", revenue: 1800 },
{ month: "Mar", revenue: 2200 },
{ month: "Apr", revenue: 2500 },
{ month: "May", revenue: 2300 },
{ month: "Jun", revenue: 3200 },
{ month: "Jul", revenue: 3500 },
{ month: "Aug", revenue: 3700 },
{ month: "Sep", revenue: 2500 },
{ month: "Oct", revenue: 2800 },
{ month: "Nov", revenue: 3000 },
{ month: "Dec", revenue: 4800 },
];
module.exports = {
+67 -67
View File
@@ -2,49 +2,49 @@
// https://nextjs.org/learn/dashboard-app/fetching-data
const users = [
{
id: '410544b2-4001-4271-9855-fec4b6a6442a',
name: 'User',
email: 'user@nextmail.com',
password: '123456',
id: "410544b2-4001-4271-9855-fec4b6a6442a",
name: "User",
email: "user@nextmail.com",
password: "123456",
},
];
const customers = [
{
id: 'd6e15727-9fe1-4961-8c5b-ea44a9bd81aa',
name: 'Evil Rabbit',
email: 'evil@rabbit.com',
image_url: '/customers/evil-rabbit.png',
id: "d6e15727-9fe1-4961-8c5b-ea44a9bd81aa",
name: "Evil Rabbit",
email: "evil@rabbit.com",
image_url: "/customers/evil-rabbit.png",
},
{
id: '3958dc9e-712f-4377-85e9-fec4b6a6442a',
name: 'Delba de Oliveira',
email: 'delba@oliveira.com',
image_url: '/customers/delba-de-oliveira.png',
id: "3958dc9e-712f-4377-85e9-fec4b6a6442a",
name: "Delba de Oliveira",
email: "delba@oliveira.com",
image_url: "/customers/delba-de-oliveira.png",
},
{
id: '3958dc9e-742f-4377-85e9-fec4b6a6442a',
name: 'Lee Robinson',
email: 'lee@robinson.com',
image_url: '/customers/lee-robinson.png',
id: "3958dc9e-742f-4377-85e9-fec4b6a6442a",
name: "Lee Robinson",
email: "lee@robinson.com",
image_url: "/customers/lee-robinson.png",
},
{
id: '76d65c26-f784-44a2-ac19-586678f7c2f2',
name: 'Michael Novotny',
email: 'michael@novotny.com',
image_url: '/customers/michael-novotny.png',
id: "76d65c26-f784-44a2-ac19-586678f7c2f2",
name: "Michael Novotny",
email: "michael@novotny.com",
image_url: "/customers/michael-novotny.png",
},
{
id: 'CC27C14A-0ACF-4F4A-A6C9-D45682C144B9',
name: 'Amy Burns',
email: 'amy@burns.com',
image_url: '/customers/amy-burns.png',
id: "CC27C14A-0ACF-4F4A-A6C9-D45682C144B9",
name: "Amy Burns",
email: "amy@burns.com",
image_url: "/customers/amy-burns.png",
},
{
id: '13D07535-C59E-4157-A011-F8D2EF4E0CBB',
name: 'Balazs Orban',
email: 'balazs@orban.com',
image_url: '/customers/balazs-orban.png',
id: "13D07535-C59E-4157-A011-F8D2EF4E0CBB",
name: "Balazs Orban",
email: "balazs@orban.com",
image_url: "/customers/balazs-orban.png",
},
];
@@ -52,96 +52,96 @@ const invoices = [
{
customer_id: customers[0].id,
amount: 15795,
status: 'pending',
date: '2022-12-06',
status: "pending",
date: "2022-12-06",
},
{
customer_id: customers[1].id,
amount: 20348,
status: 'pending',
date: '2022-11-14',
status: "pending",
date: "2022-11-14",
},
{
customer_id: customers[4].id,
amount: 3040,
status: 'paid',
date: '2022-10-29',
status: "paid",
date: "2022-10-29",
},
{
customer_id: customers[3].id,
amount: 44800,
status: 'paid',
date: '2023-09-10',
status: "paid",
date: "2023-09-10",
},
{
customer_id: customers[5].id,
amount: 34577,
status: 'pending',
date: '2023-08-05',
status: "pending",
date: "2023-08-05",
},
{
customer_id: customers[2].id,
amount: 54246,
status: 'pending',
date: '2023-07-16',
status: "pending",
date: "2023-07-16",
},
{
customer_id: customers[0].id,
amount: 666,
status: 'pending',
date: '2023-06-27',
status: "pending",
date: "2023-06-27",
},
{
customer_id: customers[3].id,
amount: 32545,
status: 'paid',
date: '2023-06-09',
status: "paid",
date: "2023-06-09",
},
{
customer_id: customers[4].id,
amount: 1250,
status: 'paid',
date: '2023-06-17',
status: "paid",
date: "2023-06-17",
},
{
customer_id: customers[5].id,
amount: 8546,
status: 'paid',
date: '2023-06-07',
status: "paid",
date: "2023-06-07",
},
{
customer_id: customers[1].id,
amount: 500,
status: 'paid',
date: '2023-08-19',
status: "paid",
date: "2023-08-19",
},
{
customer_id: customers[5].id,
amount: 8945,
status: 'paid',
date: '2023-06-03',
status: "paid",
date: "2023-06-03",
},
{
customer_id: customers[2].id,
amount: 1000,
status: 'paid',
date: '2022-06-05',
status: "paid",
date: "2022-06-05",
},
];
const revenue = [
{ month: 'Jan', revenue: 2000 },
{ month: 'Feb', revenue: 1800 },
{ month: 'Mar', revenue: 2200 },
{ month: 'Apr', revenue: 2500 },
{ month: 'May', revenue: 2300 },
{ month: 'Jun', revenue: 3200 },
{ month: 'Jul', revenue: 3500 },
{ month: 'Aug', revenue: 3700 },
{ month: 'Sep', revenue: 2500 },
{ month: 'Oct', revenue: 2800 },
{ month: 'Nov', revenue: 3000 },
{ month: 'Dec', revenue: 4800 },
{ month: "Jan", revenue: 2000 },
{ month: "Feb", revenue: 1800 },
{ month: "Mar", revenue: 2200 },
{ month: "Apr", revenue: 2500 },
{ month: "May", revenue: 2300 },
{ month: "Jun", revenue: 3200 },
{ month: "Jul", revenue: 3500 },
{ month: "Aug", revenue: 3700 },
{ month: "Sep", revenue: 2500 },
{ month: "Oct", revenue: 2800 },
{ month: "Nov", revenue: 3000 },
{ month: "Dec", revenue: 4800 },
];
export { users, customers, invoices, revenue };
export { customers, invoices, revenue, users };
+12 -12
View File
@@ -1,21 +1,21 @@
import { Revenue } from './definitions';
import type { Revenue } from "./definitions";
export const formatCurrency = (amount: number) => {
return (amount / 100).toLocaleString('en-US', {
style: 'currency',
currency: 'USD',
return (amount / 100).toLocaleString("en-US", {
style: "currency",
currency: "USD",
});
};
export const formatDateToLocal = (
dateStr: string,
locale: string = 'en-US',
locale: string = "en-US",
) => {
const date = new Date(dateStr);
const options: Intl.DateTimeFormatOptions = {
day: 'numeric',
month: 'short',
year: 'numeric',
day: "numeric",
month: "short",
year: "numeric",
};
const formatter = new Intl.DateTimeFormat(locale, options);
return formatter.format(date);
@@ -45,13 +45,13 @@ export const generatePagination = (currentPage: number, totalPages: number) => {
// If the current page is among the first 3 pages,
// show the first 3, an ellipsis, and the last 2 pages.
if (currentPage <= 3) {
return [1, 2, 3, '...', totalPages - 1, totalPages];
return [1, 2, 3, "...", totalPages - 1, totalPages];
}
// If the current page is among the last 3 pages,
// show the first 2, an ellipsis, and the last 3 pages.
if (currentPage >= totalPages - 2) {
return [1, 2, '...', totalPages - 2, totalPages - 1, totalPages];
return [1, 2, "...", totalPages - 2, totalPages - 1, totalPages];
}
// If the current page is somewhere in the middle,
@@ -59,11 +59,11 @@ export const generatePagination = (currentPage: number, totalPages: number) => {
// another ellipsis, and the last page.
return [
1,
'...',
"...",
currentPage - 1,
currentPage,
currentPage + 1,
'...',
"...",
totalPages,
];
};