Skip to content

Commit

Permalink
add update workspace endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
jsbroks committed Aug 30, 2024
1 parent 2fae456 commit afdaad6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client";

import { useEffect } from "react";
import { useParams } from "next/navigation";
import { useParams, useRouter } from "next/navigation";
import { z } from "zod";

import { Button } from "@ctrlplane/ui/button";
Expand All @@ -28,20 +28,25 @@ export const WorkspaceUpdateSection: React.FC = () => {

const form = useForm({
schema: updateWorkspace,
defaultValues: {
name: "",
slug: "",
},
defaultValues: { name: "", slug: "" },
});

useEffect(() => {
const { data } = workspace;
if (data == null) return;
if (form.getValues("name") !== "") return;
form.setValue("name", data.name);
form.setValue("slug", data.slug);
}, [form, workspace]);

const onSubmit = form.handleSubmit(() => {});
const router = useRouter();
const update = api.workspace.update.useMutation();
const onSubmit = form.handleSubmit(async (data) => {
if (workspace.data == null) return;
await update.mutateAsync({ id: workspace.data.id, data });
router.push(`/${data.slug}/settings/workspace/general`);
router.refresh();
});
return (
<Form {...form}>
<form onSubmit={onSubmit} className="space-y-4">
Expand Down
15 changes: 15 additions & 0 deletions packages/api/src/router/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { z } from "zod";
import { eq, takeFirst, takeFirstOrNull } from "@ctrlplane/db";
import {
createWorkspace,
updateWorkspace,
user,
workspace,
workspaceMember,
Expand Down Expand Up @@ -91,6 +92,20 @@ const integrationsRouter = createTRPCRouter({
});

export const workspaceRouter = createTRPCRouter({
update: protectedProcedure
.meta({
access: ({ ctx, input }) => ctx.accessQuery().workspace.id(input.id),
})
.input(z.object({ id: z.string(), data: updateWorkspace }))
.mutation(async ({ ctx, input }) =>
ctx.db
.update(workspace)
.set(input.data)
.where(eq(workspace.id, input.id))
.returning()
.then(takeFirst),
),

create: protectedProcedure
.input(createWorkspace)
.mutation(async ({ ctx, input }) =>
Expand Down
2 changes: 2 additions & 0 deletions packages/db/src/schema/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ export const createWorkspace = createInsertSchema(workspace, {
}),
}).omit({ id: true });

export const updateWorkspace = createWorkspace.partial();

export type Workspace = InferSelectModel<typeof workspace>;

export const workspaceMember = pgTable(
Expand Down

0 comments on commit afdaad6

Please sign in to comment.