| 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 { InvoiceNumberTemplateInput } |
| 11 | import { updateInvoiceNumberTemplateAction } |
| 12 | import { updateInvoiceNumberTemplateSchema } |
| 13 | import type { projectsService |
| 14 | import { Button } |
| 15 | import { |
| 16 | Card, |
| 17 | CardContent, |
| 18 | CardDescription, |
| 19 | CardFooter, |
| 20 | CardHeader, |
| 21 | CardTitle, |
| 22 | } from '@/components/ui/card' |
| 23 | import { |
| 24 | Field, |
| 25 | FieldDescription, |
| 26 | FieldError, |
| 27 | FieldLabel, |
| 28 | } from '@/components/ui/field' |
| 29 | |
| 30 | type FormValues |
| 31 | |
| 32 | export function InvoiceNumberingCard({ |
| 33 | projectId, |
| 34 | organizationId, |
| 35 | settings, |
| 36 | }: { |
| 37 | projectId: string |
| 38 | organizationId: string |
| 39 | settings: Awaited |
| 40 | }) { |
| 41 | const router = |
| 42 | |
| 43 | const form = |
| 44 | resolver: zodResolver(updateInvoiceNumberTemplateSchema |
| 45 | defaultValues: { |
| 46 | organizationId, |
| 47 | projectId, |
| 48 | invoiceNumberTemplate: settings.invoiceNumberTemplate |
| 49 | }, |
| 50 | }) |
| 51 | |
| 52 | const { execute, |
| 53 | onSuccess() { |
| 54 | toast.success('Invoice number template 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>Invoice Numbering</CardTitle |
| 67 | <CardDescription> |
| 68 | Override the workspace-level invoice number template for this project |
| 69 | only. |
| 70 | </CardDescription> |
| 71 | </CardHeader> |
| 72 | <form onSubmit={form |
| 73 | <CardContent> |
| 74 | <Controller |
| 75 | control={form.control |
| 76 | name='invoiceNumberTemplate' |
| 77 | render={({ field |
| 78 | <Field className=' |
| 79 | <FieldLabel>Template</FieldLabel |
| 80 | <InvoiceNumberTemplateInput |
| 81 | onChange={field.onChange |
| 82 | value={field.value |
| 83 | /> |
| 84 | <FieldDescription> |
| 85 | Preview uses sequence #1 and the current date/time. Real |
| 86 | invoices use the next available sequence. |
| 87 | </FieldDescription> |
| 88 | {fieldState.invalid && |
| 89 | <FieldError errors={ |
| 90 | )} |
| 91 | </Field> |
| 92 | )} |
| 93 | /> |
| 94 | </CardContent> |
| 95 | <CardFooter> |
| 96 | <Button |
| 97 | className='mt-4' |
| 98 | disabled={!form. |
| 99 | loading={isPending} |
| 100 | type='submit' |
| 101 | > |
| 102 | <Save className=' |
| 103 | Save Template |
| 104 | </Button> |
| 105 | </CardFooter> |
| 106 | </form> |
| 107 | </Card> |
| 108 | ) |
| 109 | } |