Skip to content

Commit 070b15b

Browse files
committed
feat: initial work on react-hook-form
1 parent 279fe57 commit 070b15b

File tree

8 files changed

+160
-123
lines changed

8 files changed

+160
-123
lines changed

package-lock.json

Lines changed: 38 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,15 @@
2424
"@dnd-kit/core": "^6.3.1",
2525
"@dnd-kit/sortable": "^10.0.0",
2626
"@hey-api/client-fetch": "^0.7.1",
27+
"@hookform/resolvers": "^4.1.0",
2728
"@jsonforms/core": "^3.5.1",
2829
"@jsonforms/react": "^3.5.1",
2930
"@jsonforms/vanilla-renderers": "^3.5.1",
3031
"@monaco-editor/react": "^4.6.0",
3132
"@radix-ui/react-dialog": "^1.1.4",
3233
"@radix-ui/react-separator": "^1.1.0",
3334
"@radix-ui/react-slot": "^1.1.0",
34-
"@stacklok/ui-kit": "^1.0.1-4",
35+
"@stacklok/ui-kit": "^1.0.1-8",
3536
"@tanstack/react-query": "^5.64.1",
3637
"@tanstack/react-query-devtools": "^5.66.0",
3738
"@types/lodash": "^4.17.15",

src/components/Error.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,9 @@ export function ErrorFallbackContent() {
4545

4646
export function Error() {
4747
return (
48-
<div className="flex h-screen w-screen flex-col items-center justify-center">
49-
<div className="w-full shrink-0">
50-
<Header />
51-
</div>
48+
<>
49+
<Header />
5250
<ErrorFallbackContent />
53-
</div>
51+
</>
5452
)
5553
}

src/features/workspace/components/__tests__/workspace-name.test.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { test, expect } from 'vitest'
2-
import { WorkspaceName } from '../workspace-name'
2+
import { FormWorkspaceName } from '../form-workspace-name'
33
import { render, waitFor } from '@/lib/test-utils'
44
import userEvent from '@testing-library/user-event'
55
import { server } from '@/mocks/msw/node'
@@ -8,7 +8,7 @@ import { mswEndpoint } from '@/test/msw-endpoint'
88

99
test('can rename workspace', async () => {
1010
const { getByRole, getByText } = render(
11-
<WorkspaceName workspaceName="foo-bar" isArchived={false} />
11+
<FormWorkspaceName workspaceName="foo-bar" isArchived={false} />
1212
)
1313

1414
const input = getByRole('textbox', { name: /workspace name/i })
@@ -26,7 +26,7 @@ test('can rename workspace', async () => {
2626

2727
test("can't rename archived workspace", async () => {
2828
const { getByRole } = render(
29-
<WorkspaceName workspaceName="foo" isArchived={true} />
29+
<FormWorkspaceName workspaceName="foo" isArchived={true} />
3030
)
3131

3232
expect(getByRole('textbox', { name: /workspace name/i })).toBeDisabled()
@@ -48,7 +48,7 @@ test("can't rename active workspace", async () => {
4848
)
4949
)
5050
const { getByRole } = render(
51-
<WorkspaceName workspaceName="foo" isArchived={true} />
51+
<FormWorkspaceName workspaceName="foo" isArchived={true} />
5252
)
5353

5454
expect(getByRole('textbox', { name: /workspace name/i })).toBeDisabled()
@@ -57,7 +57,7 @@ test("can't rename active workspace", async () => {
5757

5858
test("can't rename archived workspace", async () => {
5959
const { getByRole, queryByText } = render(
60-
<WorkspaceName workspaceName="foo" isArchived={true} />
60+
<FormWorkspaceName workspaceName="foo" isArchived={true} />
6161
)
6262

6363
expect(getByRole('textbox', { name: /workspace name/i })).toBeDisabled()
@@ -69,7 +69,7 @@ test("can't rename archived workspace", async () => {
6969

7070
test("can't rename default workspace", async () => {
7171
const { getByRole, getByText } = render(
72-
<WorkspaceName workspaceName="default" isArchived={false} />
72+
<FormWorkspaceName workspaceName="default" isArchived={false} />
7373
)
7474

7575
expect(getByRole('textbox', { name: /workspace name/i })).toBeDisabled()
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import {
2+
Card,
3+
CardBody,
4+
CardFooter,
5+
Description,
6+
FormDiscardChangesButton,
7+
FormSubmitButton,
8+
FormTextField,
9+
FormV2,
10+
Input,
11+
Label,
12+
} from '@stacklok/ui-kit'
13+
import { useMutationCreateWorkspace } from '../hooks/use-mutation-create-workspace'
14+
import { useNavigate } from 'react-router-dom'
15+
import { twMerge } from 'tailwind-merge'
16+
import { zodResolver } from '@hookform/resolvers/zod'
17+
import { z } from 'zod'
18+
19+
const schema = z.object({
20+
rename_to: z.string().nonempty(),
21+
})
22+
type FieldValues = z.infer<typeof schema>
23+
const FIELD_NAME = schema.keyof().Enum
24+
25+
export function FormWorkspaceName({
26+
className,
27+
workspaceName,
28+
isArchived,
29+
}: {
30+
className?: string
31+
workspaceName: string
32+
isArchived: boolean | undefined
33+
}) {
34+
const navigate = useNavigate()
35+
const { mutateAsync, isPending } = useMutationCreateWorkspace()
36+
37+
const isDefault = workspaceName === 'default'
38+
const isDisabled = isArchived || isPending || isDefault
39+
40+
const description: string | null = isDefault
41+
? 'Cannot rename the default workspace'
42+
: isArchived
43+
? 'Cannot rename an archived workspace'
44+
: null
45+
46+
const handleSubmit = ({ rename_to }: FieldValues) => {
47+
mutateAsync(
48+
{ body: { name: workspaceName, rename_to } },
49+
{
50+
onSuccess: () => {
51+
navigate(`/workspace/${rename_to}`)
52+
},
53+
}
54+
)
55+
}
56+
57+
return (
58+
<FormV2<FieldValues>
59+
onSubmit={handleSubmit}
60+
data-testid="workspace-name"
61+
options={{
62+
resolver: zodResolver(schema),
63+
defaultValues: {
64+
rename_to: workspaceName,
65+
},
66+
}}
67+
>
68+
<Card className={twMerge(className, 'shrink-0')}>
69+
<CardBody>
70+
<FormTextField
71+
isDisabled={isDisabled}
72+
key={workspaceName}
73+
name={FIELD_NAME.rename_to}
74+
isRequired
75+
>
76+
<Label>Workspace name</Label>
77+
<Input />
78+
{description ? (
79+
<Description className="mt-2 block">{description}</Description>
80+
) : null}
81+
</FormTextField>
82+
</CardBody>
83+
<CardFooter className="justify-end gap-2">
84+
<FormDiscardChangesButton />
85+
<FormSubmitButton />
86+
</CardFooter>
87+
</Card>
88+
</FormV2>
89+
)
90+
}

src/features/workspace/components/workspace-name.tsx

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)