update
This commit is contained in:
+41
-41
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user