mirror of
https://github.com/vercel/next-learn.git
synced 2026-06-11 09:51:47 +00:00
@@ -1,5 +1,6 @@
|
||||
import { PencilIcon, PlusIcon, TrashIcon } from '@heroicons/react/24/outline';
|
||||
import Link from 'next/link';
|
||||
import { deleteInvoice } from '@/app/lib/actions';
|
||||
|
||||
export function CreateInvoice() {
|
||||
return (
|
||||
@@ -24,11 +25,14 @@ export function UpdateInvoice({ id }: { id: string }) {
|
||||
);
|
||||
}
|
||||
|
||||
export function DeleteInvoice() {
|
||||
export function DeleteInvoice({ id }: { id: string }) {
|
||||
return (
|
||||
<button className="rounded-md border p-2 hover:bg-gray-100">
|
||||
<span className="sr-only">Delete</span>
|
||||
<TrashIcon className="w-5" />
|
||||
</button>
|
||||
<form action={deleteInvoice}>
|
||||
<input type="hidden" name="id" value={id} />
|
||||
<button className="rounded-md border p-2 hover:bg-gray-100">
|
||||
<span className="sr-only">Delete</span>
|
||||
<TrashIcon className="w-5" />
|
||||
</button>
|
||||
</form>
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,6 +9,7 @@ import {
|
||||
UserCircleIcon,
|
||||
} from '@heroicons/react/24/outline';
|
||||
import { Button } from '../button';
|
||||
import { createInvoice } from '@/app/lib/actions';
|
||||
|
||||
export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||
return (
|
||||
@@ -25,6 +26,7 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||
name="customerId"
|
||||
className="peer block w-full rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500"
|
||||
defaultValue=""
|
||||
aria-describedby="customer-error"
|
||||
>
|
||||
<option value="" disabled>
|
||||
Select a customer
|
||||
@@ -37,6 +39,18 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||
</select>
|
||||
<UserCircleIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500" />
|
||||
</div>
|
||||
|
||||
{state.errors?.customerId ? (
|
||||
<div
|
||||
id="customer-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.customerId.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Invoice Amount */}
|
||||
@@ -58,6 +72,18 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||
<CurrencyDollarIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{state.errors?.amount ? (
|
||||
<div
|
||||
id="amount-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.amount.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Invoice Status */}
|
||||
@@ -99,7 +125,24 @@ export default function Form({ customers }: { customers: CustomerField[] }) {
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{state.errors?.status ? (
|
||||
<div
|
||||
aria-describedby="status-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.status.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{state.message ? (
|
||||
<div aria-live="polite" className="my-2 text-sm text-red-500">
|
||||
<p>{state.message}</p>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="mt-6 flex justify-end gap-4">
|
||||
<Link
|
||||
|
||||
@@ -9,6 +9,9 @@ import {
|
||||
} from '@heroicons/react/24/outline';
|
||||
import Link from 'next/link';
|
||||
import { Button } from '../button';
|
||||
import { updateInvoice } from '@/app/lib/actions';
|
||||
// @ts-ignore React types do not yet include useFormState
|
||||
import { experimental_useFormState as useFormState } from 'react-dom';
|
||||
|
||||
export default function EditInvoiceForm({
|
||||
invoice,
|
||||
@@ -17,8 +20,11 @@ export default function EditInvoiceForm({
|
||||
invoice: InvoiceForm;
|
||||
customers: CustomerField[];
|
||||
}) {
|
||||
const initialState = { message: null, errors: [] };
|
||||
const [state, dispatch] = useFormState(updateInvoice, initialState);
|
||||
|
||||
return (
|
||||
<form>
|
||||
<form action={dispatch}>
|
||||
<div className="rounded-md bg-gray-50 p-4 md:p-6">
|
||||
{/* Invoice ID */}
|
||||
<input type="hidden" name="id" value={invoice.id} />
|
||||
@@ -33,6 +39,7 @@ export default function EditInvoiceForm({
|
||||
name="customerId"
|
||||
className="peer block w-full rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500"
|
||||
defaultValue={invoice.customer_id}
|
||||
aria-describedby="customer-error"
|
||||
>
|
||||
<option value="" disabled>
|
||||
Select a customer
|
||||
@@ -45,6 +52,18 @@ export default function EditInvoiceForm({
|
||||
</select>
|
||||
<UserCircleIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500" />
|
||||
</div>
|
||||
|
||||
{state.errors?.customerId ? (
|
||||
<div
|
||||
id="customer-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.customerId.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Invoice Amount */}
|
||||
@@ -61,10 +80,23 @@ export default function EditInvoiceForm({
|
||||
defaultValue={invoice.amount}
|
||||
placeholder="Enter USD amount"
|
||||
className="peer block w-full rounded-md border border-gray-200 py-2 pl-10 text-sm outline-2 placeholder:text-gray-500"
|
||||
aria-describedby="amount-error"
|
||||
/>
|
||||
<CurrencyDollarIcon className="pointer-events-none absolute left-3 top-1/2 h-[18px] w-[18px] -translate-y-1/2 text-gray-500 peer-focus:text-gray-900" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{state.errors?.amount ? (
|
||||
<div
|
||||
id="amount-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.amount.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{/* Invoice Status */}
|
||||
@@ -108,7 +140,24 @@ export default function EditInvoiceForm({
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{state.errors?.status ? (
|
||||
<div
|
||||
aria-describedby="status-error"
|
||||
aria-live="polite"
|
||||
className="mt-2 text-sm text-red-500"
|
||||
>
|
||||
{state.errors.status.map((error: string) => (
|
||||
<p key={error}>{error}</p>
|
||||
))}
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
|
||||
{state.message ? (
|
||||
<div aria-live="polite" className="my-2 text-sm text-red-500">
|
||||
<p>{state.message}</p>
|
||||
</div>
|
||||
) : null}
|
||||
</div>
|
||||
<div className="mt-6 flex justify-end gap-4">
|
||||
<Link
|
||||
|
||||
Reference in New Issue
Block a user