| 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 { updateInvoiceNumberTemplateAction } |
| 26 | import { updateInvoiceNumberTemplateSchema } |
| 27 | import { InvoiceNumberTemplateInput } |
| 28 | |
| 29 | type FormValues |
| 30 | |
| 31 | export function InvoiceNumberingCard({ |
| 32 | organizationId, |
| 33 | invoiceNumberTemplate, |
| 34 | }: { |
| 35 | organizationId: string |
| 36 | invoiceNumberTemplate: string |
| 37 | }) { |
| 38 | const router = |
| 39 | |
| 40 | const form = |
| 41 | resolver: zodResolver(updateInvoiceNumberTemplateSchema |
| 42 | defaultValues: { |
| 43 | organizationId, |
| 44 | invoiceNumberTemplate, |
| 45 | }, |
| 46 | }) |
| 47 | |
| 48 | const { execute, |
| 49 | onSuccess() { |
| 50 | toast.success('Invoice number template updated |
| 51 | form.reset(form |
| 52 | router.refresh() |
| 53 | }, |
| 54 | onError({ error |
| 55 | toast.error(error |
| 56 | }, |
| 57 | }) |
| 58 | |
| 59 | return ( |
| 60 | <Card> |
| 61 | <CardHeader> |
| 62 | <CardTitle>Invoice Numbering</CardTitle |
| 63 | <CardDescription> |
| 64 | Define the template used to generate invoice numbers. Add variables |
| 65 | like sequence, date parts, or time, and mix them with your own prefix |
| 66 | and separators. |
| 67 | </CardDescription> |
| 68 | </CardHeader> |
| 69 | <form onSubmit={form |
| 70 | <CardContent> |
| 71 | <Controller |
| 72 | control={form.control |
| 73 | name='invoiceNumberTemplate' |
| 74 | render={({ field |
| 75 | <Field className=' |
| 76 | <FieldLabel>Template</FieldLabel |
| 77 | <InvoiceNumberTemplateInput |
| 78 | onChange={field.onChange |
| 79 | value={field.value |
| 80 | /> |
| 81 | <FieldDescription> |
| 82 | Preview uses sequence #1 and the current date/time. Real |
| 83 | invoices use the next available sequence. |
| 84 | </FieldDescription> |
| 85 | {fieldState.invalid && |
| 86 | <FieldError errors={ |
| 87 | )} |
| 88 | </Field> |
| 89 | )} |
| 90 | /> |
| 91 | </CardContent> |
| 92 | <CardFooter> |
| 93 | <Button |
| 94 | className='mt-4' |
| 95 | disabled={!form. |
| 96 | loading={isPending} |
| 97 | type='submit' |
| 98 | > |
| 99 | <Save className=' |
| 100 | Save Template |
| 101 | </Button> |
| 102 | </CardFooter> |
| 103 | </form> |
| 104 | </Card> |
| 105 | ) |
| 106 | } |