| 1 | 'use client' |
| 2 | |
| 3 | import { zodResolver } from '@hookform/resolvers/zod' |
| 4 | import { useRouter } |
| 5 | import { useState } |
| 6 | import { Controller, useForm |
| 7 | import { toast } |
| 8 | import type { z |
| 9 | import { Button } |
| 10 | import { |
| 11 | Card, |
| 12 | CardContent, |
| 13 | CardDescription, |
| 14 | CardHeader, |
| 15 | CardTitle, |
| 16 | } from '@/components/ui/card' |
| 17 | import { CurrencySelect } |
| 18 | import { |
| 19 | Field, |
| 20 | FieldDescription, |
| 21 | FieldError, |
| 22 | FieldGroup, |
| 23 | FieldLabel, |
| 24 | } from '@/components/ui/field' |
| 25 | import { Input } |
| 26 | import { authClient } |
| 27 | import { titleToSlug } |
| 28 | import { analyticsService } |
| 29 | import { createNewOrganizationSchema } |
| 30 | |
| 31 | export default function OnboardingPageClient |
| 32 | const form = |
| 33 | resolver: zodResolver(createNewOrganizationSchema |
| 34 | defaultValues: { |
| 35 | currency: 'USD', |
| 36 | }, |
| 37 | }) |
| 38 | const [isLoading, |
| 39 | const router = |
| 40 | |
| 41 | async function handleSubmit |
| 42 | data: z |
| 43 | ) { |
| 44 | setIsLoading(true) |
| 45 | try { |
| 46 | const { slugified, |
| 47 | const slugVerify = |
| 48 | slug: slugified, |
| 49 | }) |
| 50 | |
| 51 | const organization = |
| 52 | name: data.name |
| 53 | slug: slugVerify.data |
| 54 | metadata: { |
| 55 | currency: data.currency |
| 56 | }, |
| 57 | }) |
| 58 | if (organization.error |
| 59 | toast.error(organization |
| 60 | return |
| 61 | } |
| 62 | toast.success('Workspace created successfully |
| 63 | analyticsService.track('workspace_created |
| 64 | router.replace(`/ |
| 65 | } catch (error) |
| 66 | console.error(error |
| 67 | } finally { |
| 68 | setIsLoading(false) |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | return ( |
| 73 | <Card className=' |
| 74 | <CardHeader> |
| 75 | <CardTitle>Create a new workspace</CardTitle |
| 76 | <CardDescription> |
| 77 | Create a new workspace to get started. |
| 78 | </CardDescription> |
| 79 | </CardHeader> |
| 80 | <CardContent> |
| 81 | <form className='space-y-4 |
| 82 | <FieldGroup> |
| 83 | <Controller |
| 84 | control={form.control |
| 85 | name='name' |
| 86 | render={({ field |
| 87 | <Field |
| 88 | className='col-span-full gap-1' |
| 89 | data-invalid={fieldState.invalid |
| 90 | > |
| 91 | <FieldLabel>Name</FieldLabel |
| 92 | <Input |
| 93 | {...field} |
| 94 | aria-invalid={fieldState.invalid |
| 95 | onChange={(e) |
| 96 | field.onChange(e |
| 97 | }} |
| 98 | placeholder='Enter your workspace name' |
| 99 | type='text' |
| 100 | /> |
| 101 | <FieldDescription> |
| 102 | This is the name of your workspace. Don't worry, you |
| 103 | can change it later. |
| 104 | </FieldDescription> |
| 105 | {fieldState.invalid && |
| 106 | <FieldError errors={ |
| 107 | )} |
| 108 | </Field> |
| 109 | )} |
| 110 | /> |
| 111 | <Controller |
| 112 | control={form.control |
| 113 | name='currency' |
| 114 | render={({ field |
| 115 | <Field |
| 116 | className='col-span-full gap-1' |
| 117 | data-invalid={fieldState.invalid |
| 118 | > |
| 119 | <FieldLabel>Currency</FieldLabel |
| 120 | <CurrencySelect |
| 121 | {...field} |
| 122 | name='currency' |
| 123 | onCurrencySelect={(currency) |
| 124 | form.setValue('currency |
| 125 | }} |
| 126 | /> |
| 127 | <FieldDescription> |
| 128 | The currency your workspace will use for clients. Don't |
| 129 | worry, you can choose different currencies for different |
| 130 | clients and invoices. |
| 131 | </FieldDescription> |
| 132 | |
| 133 | {fieldState.invalid && |
| 134 | <FieldError errors={ |
| 135 | )} |
| 136 | </Field> |
| 137 | )} |
| 138 | /> |
| 139 | </FieldGroup> |
| 140 | <Button loading={ |
| 141 | Create workspace |
| 142 | </Button> |
| 143 | </form> |
| 144 | </CardContent> |
| 145 | </Card> |
| 146 | ) |
| 147 | } |