Skip to content

Commit

Permalink
getting data for single workspace through id, made a custom hook to g…
Browse files Browse the repository at this point in the history
…et that and endpoint too
  • Loading branch information
Diivvuu committed Sep 26, 2024
1 parent 023faaf commit 558438a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 8 deletions.
13 changes: 13 additions & 0 deletions convex/workspaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,16 @@ export const get = query({
return await ctx.db.query("workspaces").collect();
},
});

export const getById = query({
args: { id: v.id("workspaces") },
handler: async (ctx, args) => {
const userId = await auth.getUserId(ctx);

if (!userId) {
throw new Error("Unauthorized");
}
//todo : later allow only memebers to get
return await ctx.db.get(args.id);
},
});
2 changes: 2 additions & 0 deletions src/app/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { ConvexAuthNextjsServerProvider } from "@convex-dev/auth/nextjs/server";

import "../features/workspaces/components/create-workspace-modal";
import { Modals } from "@/components/modals";
import { Toaster } from "sonner";

const inter = Inter({ subsets: ["latin"] });

Expand All @@ -26,6 +27,7 @@ export default function RootLayout({
<html lang="en">
<body className={inter.className}>
<ConvexClientProvider>
<Toaster/>
<Modals />
{children}
</ConvexClientProvider>
Expand Down
16 changes: 9 additions & 7 deletions src/app/workspace/[workspaceId]/page.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
interface WorkSpaceIdPageProps {
params: {
workspaceId: string;
};
}
const workSpaceId = ({ params }: WorkSpaceIdPageProps) => {
return <div>workSpaceId ID : {params.workspaceId}</div>;
"use client";

import { useGetWorkspace } from "@/features/workspaces/api/use-get-workspace";
import { useWorkspaceId } from "@/hooks/use-workspace-id";

const workSpaceId = () => {
const workspaceId = useWorkspaceId();
const { data } = useGetWorkspace({ id: workspaceId });
return <div>workSpaceId ID Data: {JSON.stringify(data)}</div>;
};

export default workSpaceId;
13 changes: 13 additions & 0 deletions src/features/workspaces/api/use-get-workspace.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { useQuery } from "convex/react";
import { api } from "../../../../convex/_generated/api";

interface useGetWorkspaceProps {
id: Id<"workspaces">;
}

export const useGetWorkspace = ({ id }: useGetWorkspaceProps) => {
const data = useQuery(api.workspaces.getById, { id });
const isLoading = data === undefined;

return { data, isLoading };
};
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { Input } from "@/components/ui/input";
import { Button } from "@/components/ui/button";
import { useCreateWorkspace } from "../api/use-create-workspaces";
import { useRouter } from "next/navigation";
import { toast } from "sonner";

export const CreateWorkSpaceModal = () => {
const router = useRouter();
Expand All @@ -32,8 +33,9 @@ export const CreateWorkSpaceModal = () => {
{
onSuccess(Id) {
// console.log(data);
toast.success("Workspace created!")
router.push(`/workspace/${Id}`);
handleClose()
handleClose();
},
}
);
Expand Down
8 changes: 8 additions & 0 deletions src/hooks/use-workspace-id.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { useParams } from "next/navigation";

import { Id } from "../../convex/_generated/dataModel";

export const useWorkspaceId = () => {
const params = useParams();
return params.workspaceId as Id<"workspaces">;
};

0 comments on commit 558438a

Please sign in to comment.