Skip to content

Commit

Permalink
✨ feat (finance): Added dialog box to confirm payment
Browse files Browse the repository at this point in the history
  • Loading branch information
aryanprince committed Feb 28, 2024
1 parent 81d2d2f commit 632d632
Show file tree
Hide file tree
Showing 4 changed files with 216 additions and 15 deletions.
1 change: 1 addition & 0 deletions apps/finance/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
},
"dependencies": {
"@auth/drizzle-adapter": "^0.3.6",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-tooltip": "^1.0.7",
Expand Down
71 changes: 56 additions & 15 deletions apps/finance/src/app/pay/invoice/[id]/pay-invoice-button.tsx
Original file line number Diff line number Diff line change
@@ -1,27 +1,68 @@
"use client";

import { useState } from "react";
import { useRouter } from "next/navigation";
import { CreditCard } from "lucide-react";
import { toast } from "sonner";

import type { Invoice } from "~/server/db/schema";
import type { invoice } from "~/server/db/schema";
import { Button } from "~/components/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
DialogTrigger,
} from "~/components/ui/dialog";

type Invoice = typeof Invoice.$inferSelect;
type Invoice = typeof invoice.$inferSelect;

export function PayInvoiceButton({ invoice }: { invoice: Invoice }) {
const router = useRouter();
const [isOpen, setIsOpen] = useState(false);

return (
<Button
onClick={async () => {
await fetch(`/api/invoices/reference/${invoice.referenceId}/pay`, {
method: "PUT",
});
toast.message(`✅ Invoice paid successfully!`, {
description: `Reference ID: ${invoice.referenceId}`,
});
}}
>
<CreditCard className="mr-2 size-4" />
Pay
</Button>
<Dialog open={isOpen} onOpenChange={setIsOpen}>
<DialogTrigger asChild>
<Button>
<CreditCard className="mr-2 size-4" />
Pay
</Button>
</DialogTrigger>
<DialogContent>
<DialogHeader>
<DialogTitle>Enter amount to pay for invoice</DialogTitle>
<DialogDescription>
Provide an amount to pay, or select the full amount to pay the
invoice.
</DialogDescription>
</DialogHeader>
<DialogFooter>
<Button
onClick={async () => {
try {
await fetch(
`/api/invoices/reference/${invoice.referenceId}/pay`,
{
method: "PUT",
},
);
router.refresh();
setIsOpen(false);
toast.message(`✅ Invoice paid successfully!`, {
description: `Reference ID: ${invoice.referenceId}`,
});
} catch (error) {
toast.error(`❌ Error: Failed to pay invoice!`);
}
}}
>
Pay in Full
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
);
}
122 changes: 122 additions & 0 deletions apps/finance/src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
"use client"

import * as React from "react"
import * as DialogPrimitive from "@radix-ui/react-dialog"
import { X } from "lucide-react"

import { cn } from "~/lib/utils"

const Dialog = DialogPrimitive.Root

const DialogTrigger = DialogPrimitive.Trigger

const DialogPortal = DialogPrimitive.Portal

const DialogClose = DialogPrimitive.Close

const DialogOverlay = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Overlay>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Overlay>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
/>
))
DialogOverlay.displayName = DialogPrimitive.Overlay.displayName

const DialogContent = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Content>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Content>
>(({ className, children, ...props }, ref) => (
<DialogPortal>
<DialogOverlay />
<DialogPrimitive.Content
ref={ref}
className={cn(
"fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] gap-4 border bg-background p-6 shadow-lg duration-200 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0 data-[state=closed]:zoom-out-95 data-[state=open]:zoom-in-95 data-[state=closed]:slide-out-to-left-1/2 data-[state=closed]:slide-out-to-top-[48%] data-[state=open]:slide-in-from-left-1/2 data-[state=open]:slide-in-from-top-[48%] sm:rounded-lg",
className
)}
{...props}
>
{children}
<DialogPrimitive.Close className="absolute right-4 top-4 rounded-sm opacity-70 ring-offset-background transition-opacity hover:opacity-100 focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2 disabled:pointer-events-none data-[state=open]:bg-accent data-[state=open]:text-muted-foreground">
<X className="h-4 w-4" />
<span className="sr-only">Close</span>
</DialogPrimitive.Close>
</DialogPrimitive.Content>
</DialogPortal>
))
DialogContent.displayName = DialogPrimitive.Content.displayName

const DialogHeader = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col space-y-1.5 text-center sm:text-left",
className
)}
{...props}
/>
)
DialogHeader.displayName = "DialogHeader"

const DialogFooter = ({
className,
...props
}: React.HTMLAttributes<HTMLDivElement>) => (
<div
className={cn(
"flex flex-col-reverse sm:flex-row sm:justify-end sm:space-x-2",
className
)}
{...props}
/>
)
DialogFooter.displayName = "DialogFooter"

const DialogTitle = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Title>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Title>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Title
ref={ref}
className={cn(
"text-lg font-semibold leading-none tracking-tight",
className
)}
{...props}
/>
))
DialogTitle.displayName = DialogPrimitive.Title.displayName

const DialogDescription = React.forwardRef<
React.ElementRef<typeof DialogPrimitive.Description>,
React.ComponentPropsWithoutRef<typeof DialogPrimitive.Description>
>(({ className, ...props }, ref) => (
<DialogPrimitive.Description
ref={ref}
className={cn("text-sm text-muted-foreground", className)}
{...props}
/>
))
DialogDescription.displayName = DialogPrimitive.Description.displayName

export {
Dialog,
DialogPortal,
DialogOverlay,
DialogClose,
DialogTrigger,
DialogContent,
DialogHeader,
DialogFooter,
DialogTitle,
DialogDescription,
}
37 changes: 37 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 632d632

Please sign in to comment.