diff --git a/src/features/workspaces/api/use-create-workspace.ts b/src/features/workspaces/api/use-create-workspace.ts new file mode 100644 index 0000000..06730d5 --- /dev/null +++ b/src/features/workspaces/api/use-create-workspace.ts @@ -0,0 +1,35 @@ +import { toast } from "sonner"; +import { client } from "@/lib/rpc"; +import { InferRequestType, InferResponseType } from "hono"; +import { useMutation, useQueryClient } from "@tanstack/react-query"; + +type ResponseType = InferResponseType<(typeof client.api.workspaces)["$post"]>; + +type RequestType = InferRequestType<(typeof client.api.workspaces)["$post"]>; + +export const useCreateWorkspace = () => { + const queryClient = useQueryClient(); + + const mutation = useMutation({ + mutationFn: async ({ json }) => { + const response = await client.api.workspaces["$post"]({ + json, + }); + + if (!response.ok) { + throw new Error("Failed to create workspace"); + } + + return await response.json(); + }, + onSuccess: () => { + toast.success("Workspace created successfully"); + queryClient.invalidateQueries({ queryKey: ["workspaces"] }); + }, + onError: () => { + toast.error("Failed to create workspace"); + }, + }); + + return mutation; +};