generated from t3-oss/create-t3-turbo
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from trevorpfiz/franklinjaradev/ttp-22-patient…
…-can-pay-bills Franklinjaradev/ttp 22 patient can pay bills
- Loading branch information
Showing
11 changed files
with
649 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
121 changes: 121 additions & 0 deletions
121
apps/nextjs/src/app/(authenticated)/patient/_components/create-payment.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
"use client"; | ||
|
||
import { useRouter } from "next/navigation"; | ||
import { zodResolver } from "@hookform/resolvers/zod"; | ||
import { useForm } from "react-hook-form"; | ||
import { z } from "zod"; | ||
|
||
import { Button } from "@acme/ui/button"; | ||
import { | ||
Form, | ||
FormControl, | ||
FormDescription, | ||
FormField, | ||
FormItem, | ||
FormLabel, | ||
FormMessage, | ||
} from "@acme/ui/form"; | ||
import { Input } from "@acme/ui/input"; | ||
import { Popover, PopoverContent, PopoverTrigger } from "@acme/ui/popover"; | ||
import { toast } from "@acme/ui/use-toast"; | ||
|
||
import { api } from "~/trpc/react"; | ||
|
||
const CreatePayment = ({ reference }: { reference: string }) => { | ||
const router = useRouter(); | ||
|
||
const CreatePaymentFormSchema = z.object({ | ||
amount: z.coerce.number().gt(0, { | ||
message: "Amount must be greater than 0", | ||
}), | ||
}); | ||
|
||
const createPayment = api.payment.createPayment.useMutation({ | ||
onSuccess: () => { | ||
toast({ | ||
title: "Payment created", | ||
description: "Payment created successfully", | ||
}); | ||
router.refresh(); | ||
}, | ||
onError: (error) => { | ||
toast({ | ||
title: "Payment creation failed", | ||
description: error.message, | ||
variant: "destructive", | ||
}); | ||
}, | ||
}); | ||
|
||
const createPaymentForm = useForm<z.infer<typeof CreatePaymentFormSchema>>({ | ||
resolver: zodResolver(CreatePaymentFormSchema), | ||
defaultValues: { | ||
amount: 0, | ||
}, | ||
}); | ||
|
||
const onSubmitCreatePayment = async ( | ||
data: z.infer<typeof CreatePaymentFormSchema>, | ||
) => { | ||
try { | ||
await createPayment.mutateAsync({ | ||
body: { | ||
resourceType: "PaymentNotice", | ||
status: "active", | ||
recipient: {}, | ||
request: { | ||
reference: reference, | ||
}, | ||
payment: {}, | ||
created: new Date().toISOString(), | ||
amount: { | ||
value: data.amount, | ||
currency: "USD", | ||
}, | ||
}, | ||
}); | ||
|
||
createPaymentForm.reset(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}; | ||
|
||
return ( | ||
<Popover> | ||
<PopoverTrigger asChild> | ||
<Button>Create Payment</Button> | ||
</PopoverTrigger> | ||
<PopoverContent> | ||
<Form {...createPaymentForm}> | ||
<form | ||
onSubmit={createPaymentForm.handleSubmit(onSubmitCreatePayment)} | ||
className="w-full space-y-6" | ||
> | ||
<FormField | ||
control={createPaymentForm.control} | ||
name="amount" | ||
render={({ field }) => ( | ||
<FormItem> | ||
<FormLabel>Amount</FormLabel> | ||
<FormControl> | ||
<Input type="number" placeholder="10.00" {...field} /> | ||
</FormControl> | ||
<FormDescription> | ||
The amount of the payment to be made. | ||
</FormDescription> | ||
<FormMessage /> | ||
</FormItem> | ||
)} | ||
/> | ||
<Button type="submit" className="w-full"> | ||
Submit Payment | ||
</Button> | ||
</form> | ||
</Form> | ||
</PopoverContent> | ||
</Popover> | ||
); | ||
}; | ||
|
||
export default CreatePayment; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.