Skip to content

Commit

Permalink
feat: rename workspace (#163)
Browse files Browse the repository at this point in the history
* feat: rename workspaces

* test: update workspace name section
  • Loading branch information
peppescg authored Jan 22, 2025
1 parent 1d80c9f commit 3b68754
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 14 deletions.
11 changes: 10 additions & 1 deletion src/features/workspace/components/workspace-creation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,24 @@ import {
TextField,
} from "@stacklok/ui-kit";
import { FormEvent, useState } from "react";
import { useNavigate } from "react-router-dom";

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

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
mutate({ body: { name: workspaceName } });
mutate(
{
body: { name: workspaceName },
},
{
onSuccess: () => navigate("/workspaces"),
},
);
};

return (
Expand Down
53 changes: 44 additions & 9 deletions src/features/workspace/components/workspace-name.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
import { Card, CardBody, Input, Label, TextField } from "@stacklok/ui-kit";
import {
Button,
Card,
CardBody,
CardFooter,
Form,
Input,
Label,
TextField,
} from "@stacklok/ui-kit";
import { twMerge } from "tailwind-merge";
import { useCreateWorkspace } from "../hooks/use-create-workspace";
import { FormEvent, useState } from "react";

export function WorkspaceName({
className,
Expand All @@ -8,14 +19,38 @@ export function WorkspaceName({
className?: string;
workspaceName: string;
}) {
const [name, setName] = useState(workspaceName);
const { mutate, isPending, error } = useCreateWorkspace();
const errorMsg = error?.detail ? `${error?.detail}` : "";

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
e.preventDefault();
mutate({ body: { name: workspaceName, rename_to: name } });
};

return (
<Card className={twMerge(className, "shrink-0")}>
<CardBody>
<TextField value={workspaceName} isReadOnly>
<Label>Workspace name</Label>
<Input />
</TextField>
</CardBody>
</Card>
<Form onSubmit={handleSubmit} validationBehavior="aria">
<Card className={twMerge(className, "shrink-0")}>
<CardBody>
<TextField
aria-label="Workspace name"
value={name}
name="Workspace name"
validationBehavior="aria"
isRequired
onChange={setName}
>
<Label>Workspace name</Label>
<Input />
{errorMsg && <div className="p-1 text-red-700">{errorMsg}</div>}
</TextField>
</CardBody>
<CardFooter className="justify-end gap-2">
<Button isDisabled={name === ""} isPending={isPending} type="submit">
Save
</Button>
</CardFooter>
</Card>
</Form>
);
}
3 changes: 0 additions & 3 deletions src/features/workspace/hooks/use-create-workspace.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { useMutation } from "@tanstack/react-query";
import { useNavigate } from "react-router-dom";
import { v1CreateWorkspaceMutation } from "@/api/generated/@tanstack/react-query.gen";

export function useCreateWorkspace() {
const navigate = useNavigate();
return useMutation({
...v1CreateWorkspaceMutation(),
onSuccess: () => navigate("/workspaces"),
});
}
2 changes: 1 addition & 1 deletion src/routes/__tests__/route-workspace.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ test("renders title", () => {
test("renders workspace name input", () => {
const { getByRole } = renderComponent();

expect(getByRole("textbox", { name: "Workspace name" })).toBeVisible();
expect(getByRole("textbox", { name: /workspace name/i })).toBeVisible();
});

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

0 comments on commit 3b68754

Please sign in to comment.