Skip to content

Commit 3b68754

Browse files
authored
feat: rename workspace (#163)
* feat: rename workspaces * test: update workspace name section
1 parent 1d80c9f commit 3b68754

File tree

4 files changed

+55
-14
lines changed

4 files changed

+55
-14
lines changed

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

+10-1
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,24 @@ import {
1111
TextField,
1212
} from "@stacklok/ui-kit";
1313
import { FormEvent, useState } from "react";
14+
import { useNavigate } from "react-router-dom";
1415

1516
export function WorkspaceCreation() {
17+
const navigate = useNavigate();
1618
const [workspaceName, setWorkspaceName] = useState("");
1719
const { mutate, isPending, error } = useCreateWorkspace();
1820
const errorMsg = error?.detail ? `${error?.detail}` : "";
1921

2022
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
2123
e.preventDefault();
22-
mutate({ body: { name: workspaceName } });
24+
mutate(
25+
{
26+
body: { name: workspaceName },
27+
},
28+
{
29+
onSuccess: () => navigate("/workspaces"),
30+
},
31+
);
2332
};
2433

2534
return (
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
import { Card, CardBody, Input, Label, TextField } from "@stacklok/ui-kit";
1+
import {
2+
Button,
3+
Card,
4+
CardBody,
5+
CardFooter,
6+
Form,
7+
Input,
8+
Label,
9+
TextField,
10+
} from "@stacklok/ui-kit";
211
import { twMerge } from "tailwind-merge";
12+
import { useCreateWorkspace } from "../hooks/use-create-workspace";
13+
import { FormEvent, useState } from "react";
314

415
export function WorkspaceName({
516
className,
@@ -8,14 +19,38 @@ export function WorkspaceName({
819
className?: string;
920
workspaceName: string;
1021
}) {
22+
const [name, setName] = useState(workspaceName);
23+
const { mutate, isPending, error } = useCreateWorkspace();
24+
const errorMsg = error?.detail ? `${error?.detail}` : "";
25+
26+
const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
27+
e.preventDefault();
28+
mutate({ body: { name: workspaceName, rename_to: name } });
29+
};
30+
1131
return (
12-
<Card className={twMerge(className, "shrink-0")}>
13-
<CardBody>
14-
<TextField value={workspaceName} isReadOnly>
15-
<Label>Workspace name</Label>
16-
<Input />
17-
</TextField>
18-
</CardBody>
19-
</Card>
32+
<Form onSubmit={handleSubmit} validationBehavior="aria">
33+
<Card className={twMerge(className, "shrink-0")}>
34+
<CardBody>
35+
<TextField
36+
aria-label="Workspace name"
37+
value={name}
38+
name="Workspace name"
39+
validationBehavior="aria"
40+
isRequired
41+
onChange={setName}
42+
>
43+
<Label>Workspace name</Label>
44+
<Input />
45+
{errorMsg && <div className="p-1 text-red-700">{errorMsg}</div>}
46+
</TextField>
47+
</CardBody>
48+
<CardFooter className="justify-end gap-2">
49+
<Button isDisabled={name === ""} isPending={isPending} type="submit">
50+
Save
51+
</Button>
52+
</CardFooter>
53+
</Card>
54+
</Form>
2055
);
2156
}
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
import { useMutation } from "@tanstack/react-query";
2-
import { useNavigate } from "react-router-dom";
32
import { v1CreateWorkspaceMutation } from "@/api/generated/@tanstack/react-query.gen";
43

54
export function useCreateWorkspace() {
6-
const navigate = useNavigate();
75
return useMutation({
86
...v1CreateWorkspaceMutation(),
9-
onSuccess: () => navigate("/workspaces"),
107
});
118
}

src/routes/__tests__/route-workspace.test.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ test("renders title", () => {
3535
test("renders workspace name input", () => {
3636
const { getByRole } = renderComponent();
3737

38-
expect(getByRole("textbox", { name: "Workspace name" })).toBeVisible();
38+
expect(getByRole("textbox", { name: /workspace name/i })).toBeVisible();
3939
});
4040

4141
test("renders system prompt editor", async () => {

0 commit comments

Comments
 (0)