| 1 | 'use client' |
| 2 | |
| 3 | import { useRouter } from '@bprogress/next/app' |
| 4 | import { zodResolver } |
| 5 | import { Save } |
| 6 | import { useAction } |
| 7 | import { Controller, useForm |
| 8 | import { toast } |
| 9 | import type { z |
| 10 | import { Button } |
| 11 | import { |
| 12 | Card, |
| 13 | CardContent, |
| 14 | CardDescription, |
| 15 | CardFooter, |
| 16 | CardHeader, |
| 17 | CardTitle, |
| 18 | } from '@/components/ui/card' |
| 19 | import { |
| 20 | Field, |
| 21 | FieldDescription, |
| 22 | FieldError, |
| 23 | FieldLabel, |
| 24 | } from '@/components/ui/field' |
| 25 | import { Input } |
| 26 | import { Textarea } |
| 27 | import { updateInvoiceFromDetailsAction } |
| 28 | import { updateInvoiceFromDetailsSchema } |
| 29 | |
| 30 | type FormValues |
| 31 | |
| 32 | export function InvoiceFromCard({ |
| 33 | organizationId, |
| 34 | invoiceFromName, |
| 35 | invoiceFromAddress, |
| 36 | }: { |
| 37 | organizationId: string |
| 38 | invoiceFromName: string |
| 39 | invoiceFromAddress: string |
| 40 | }) { |
| 41 | const router = |
| 42 | |
| 43 | const form = |
| 44 | resolver: zodResolver(updateInvoiceFromDetailsSchema |
| 45 | defaultValues: { |
| 46 | organizationId, |
| 47 | invoiceFromName: invoiceFromName ?? |
| 48 | invoiceFromAddress: invoiceFromAddress ?? |
| 49 | }, |
| 50 | }) |
| 51 | |
| 52 | const { execute, |
| 53 | onSuccess() { |
| 54 | toast.success('Billing details updated |
| 55 | form.reset(form |
| 56 | router.refresh() |
| 57 | }, |
| 58 | onError({ error |
| 59 | toast.error(error |
| 60 | }, |
| 61 | }) |
| 62 | |
| 63 | return ( |
| 64 | <Card> |
| 65 | <CardHeader> |
| 66 | <CardTitle>Billing Details</CardTitle |
| 67 | <CardDescription> |
| 68 | The default "From" details shown on invoices across this workspace. |
| 69 | Projects can override these. |
| 70 | </CardDescription> |
| 71 | </CardHeader> |
| 72 | <form onSubmit={form |
| 73 | <CardContent className=' |
| 74 | <Controller |
| 75 | control={form.control |
| 76 | name='invoiceFromName' |
| 77 | render={({ field |
| 78 | <Field className=' |
| 79 | <FieldLabel>From Name</FieldLabel |
| 80 | <Input |
| 81 | placeholder='Acme Inc.' |
| 82 | {...field} |
| 83 | value={field.value |
| 84 | /> |
| 85 | <FieldDescription> |
| 86 | The sender name shown on invoices. |
| 87 | </FieldDescription> |
| 88 | {fieldState.invalid && |
| 89 | <FieldError errors={ |
| 90 | )} |
| 91 | </Field> |
| 92 | )} |
| 93 | /> |
| 94 | <Controller |
| 95 | control={form.control |
| 96 | name='invoiceFromAddress' |
| 97 | render={({ field |
| 98 | <Field className=' |
| 99 | <FieldLabel>From Address</FieldLabel |
| 100 | <Textarea |
| 101 | placeholder='Ticklemore Street, Devon, UK' |
| 102 | rows={3} |
| 103 | {...field} |
| 104 | value={field.value |
| 105 | /> |
| 106 | <FieldDescription> |
| 107 | The sender address shown on invoices. |
| 108 | </FieldDescription> |
| 109 | {fieldState.invalid && |
| 110 | <FieldError errors={ |
| 111 | )} |
| 112 | </Field> |
| 113 | )} |
| 114 | /> |
| 115 | </CardContent> |
| 116 | <CardFooter> |
| 117 | <Button |
| 118 | className='mt-4' |
| 119 | disabled={!form. |
| 120 | loading={isPending} |
| 121 | type='submit' |
| 122 | > |
| 123 | <Save className=' |
| 124 | Save Billing Details |
| 125 | </Button> |
| 126 | </CardFooter> |
| 127 | </form> |
| 128 | </Card> |
| 129 | ) |
| 130 | } |