Skip to content

Commit

Permalink
Add join workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
devleejb committed Jan 23, 2024
1 parent 36e4c77 commit 5cb6863
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
4 changes: 2 additions & 2 deletions backend/src/workspaces/workspaces.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { CreateInvitationTokenResponse } from "./types/create-inviation-token-re
import { WorkspaceRoleConstants } from "src/utils/constants/auth-role";
import slugify from "slugify";
import { generateRandomKey } from "src/utils/functions/random-string";
import moment from "moment";
import * as moment from "moment";

@Injectable()
export class WorkspacesService {
Expand Down Expand Up @@ -174,7 +174,7 @@ export class WorkspacesService {
},
});

if (!userWorkspace) {
if (userWorkspace) {
return userWorkspace.workspace;
}

Expand Down
6 changes: 6 additions & 0 deletions frontend/src/hooks/api/types/workspace.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,9 @@ export class CreateWorkspaceInviteTokenRequest {
export class CreateWorkspaceInviteTokenResponse {
invitationToken: string;
}

export class JoinWorkspaceRequest {
invitationToken: string;
}

export class JoinWorkspaceResponse extends Workspace {}
12 changes: 12 additions & 0 deletions frontend/src/hooks/api/workspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import {
CreateWorkspaceResponse,
GetWorkspaceListResponse,
GetWorkspaceResponse,
JoinWorkspaceRequest,
JoinWorkspaceResponse,
} from "./types/workspace";

export const generateGetWorkspaceQueryKey = (workspaceSlug: string) => {
Expand Down Expand Up @@ -74,3 +76,13 @@ export const useCreateWorkspaceInvitationTokenMutation = (workspaceId: string) =
},
});
};

export const useJoinWorkspaceMutation = () => {
return useMutation({
mutationFn: async (data: JoinWorkspaceRequest) => {
const res = await axios.post<JoinWorkspaceResponse>("/workspaces/join", data);

return res.data;
},
});
};
26 changes: 26 additions & 0 deletions frontend/src/pages/workspace/join/Index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Backdrop, CircularProgress } from "@mui/material";
import { useNavigate, useParams } from "react-router";
import { useJoinWorkspaceMutation } from "../../../hooks/api/workspace";
import { useEffect } from "react";

function JoinIndex() {
const params = useParams();
const navigate = useNavigate();
const { mutateAsync: joinWorkspace } = useJoinWorkspaceMutation();

useEffect(() => {
if (!params.invitationToken) return;

joinWorkspace({ invitationToken: params.invitationToken }).then((data) => {
navigate(`/workspace/${data.slug}`);
});
}, [joinWorkspace, navigate, params.invitationToken]);

return (
<Backdrop open>
<CircularProgress color="inherit" />
</Backdrop>
);
}

export default JoinIndex;
6 changes: 6 additions & 0 deletions frontend/src/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import GuestRoute from "./components/common/GuestRoute";
import PrivateRoute from "./components/common/PrivateRoute";
import WorkspaceIndex from "./pages/workspace/Index";
import CodePairError from "./components/common/CodePairError";
import JoinIndex from "./pages/workspace/join/Index";

interface CodePairRoute {
path: string;
Expand Down Expand Up @@ -65,6 +66,11 @@ const codePairRoutes: Array<CodePairRoute> = [
accessType: AccessType.GUEST,
element: <CallbackIndex />,
},
{
path: "join/:invitationToken",
accessType: AccessType.PRIVATE,
element: <JoinIndex />,
},
];

const injectProtectedRoute = (routes: typeof codePairRoutes) => {
Expand Down

0 comments on commit 5cb6863

Please sign in to comment.