-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2579 from quantified-uncertainty/invite-link
Reusable invite links
- Loading branch information
Showing
49 changed files
with
1,123 additions
and
360 deletions.
There are no files selected for viewing
2 changes: 2 additions & 0 deletions
2
packages/hub/prisma/migrations/20231123202517_reusable_invite_token/migration.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
-- AlterTable | ||
ALTER TABLE "Group" ADD COLUMN "reusableInviteToken" TEXT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
135 changes: 135 additions & 0 deletions
135
packages/hub/src/app/groups/[slug]/invite-link/AcceptGroupInvitePage.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
"use client"; | ||
import { useSession } from "next-auth/react"; | ||
import { redirect, useRouter, useSearchParams } from "next/navigation"; | ||
import { FC, useEffect } from "react"; | ||
import { graphql } from "relay-runtime"; | ||
|
||
import { AcceptGroupInvitePageMutation } from "@/__generated__/AcceptGroupInvitePageMutation.graphql"; | ||
import { MutationButton } from "@/components/ui/MutationButton"; | ||
import { usePageQuery } from "@/relay/usePageQuery"; | ||
import { SerializablePreloadedQuery } from "@/relay/loadPageQuery"; | ||
import { AcceptGroupInvitePageQuery } from "@/__generated__/AcceptGroupInvitePageQuery.graphql"; | ||
import { useIsGroupMember } from "../hooks"; | ||
import { extractFromGraphqlErrorUnion } from "@/lib/graphqlHelpers"; | ||
import { groupRoute } from "@/routes"; | ||
import { useAsyncMutation } from "@/hooks/useAsyncMutation"; | ||
import { AcceptGroupInvitePage_ValidateMutation } from "@/__generated__/AcceptGroupInvitePage_ValidateMutation.graphql"; | ||
import { useToast } from "@quri/ui"; | ||
|
||
export const AcceptGroupInvitePage: FC<{ | ||
query: SerializablePreloadedQuery<AcceptGroupInvitePageQuery>; | ||
}> = ({ query }) => { | ||
useSession({ required: true }); | ||
|
||
const [{ result }] = usePageQuery( | ||
graphql` | ||
query AcceptGroupInvitePageQuery($slug: String!) { | ||
result: group(slug: $slug) { | ||
__typename | ||
... on Group { | ||
slug | ||
...hooks_useIsGroupMember | ||
} | ||
} | ||
} | ||
`, | ||
query | ||
); | ||
const group = extractFromGraphqlErrorUnion(result, "Group"); | ||
const isGroupMember = useIsGroupMember(group); | ||
|
||
if (isGroupMember) { | ||
redirect(groupRoute({ slug: group.slug })); | ||
} | ||
|
||
const params = useSearchParams(); | ||
const inviteToken = params.get("token"); | ||
if (!inviteToken) { | ||
throw new Error("Token is missing"); | ||
} | ||
|
||
const [validateMutation] = useAsyncMutation< | ||
AcceptGroupInvitePage_ValidateMutation, | ||
"ValidateReusableGroupInviteTokenResult" | ||
>({ | ||
mutation: graphql` | ||
mutation AcceptGroupInvitePage_ValidateMutation( | ||
$input: MutationValidateReusableGroupInviteTokenInput! | ||
) { | ||
result: validateReusableGroupInviteToken(input: $input) { | ||
__typename | ||
... on BaseError { | ||
message | ||
} | ||
... on ValidateReusableGroupInviteTokenResult { | ||
ok | ||
} | ||
} | ||
} | ||
`, | ||
expectedTypename: "ValidateReusableGroupInviteTokenResult", | ||
}); | ||
|
||
const toast = useToast(); | ||
const router = useRouter(); | ||
|
||
useEffect(() => { | ||
validateMutation({ | ||
variables: { | ||
input: { | ||
groupSlug: group.slug, | ||
inviteToken, | ||
}, | ||
}, | ||
onCompleted({ ok }) { | ||
if (!ok) { | ||
toast("Invalid token", "error"); | ||
router.replace(groupRoute({ slug: group.slug })); | ||
} | ||
}, | ||
}); | ||
}, []); | ||
|
||
return ( | ||
<div> | ||
<p className="mb-4">{`You've been invited to join ${group.slug} group.`}</p> | ||
<MutationButton< | ||
AcceptGroupInvitePageMutation, | ||
"AcceptReusableGroupInviteTokenResult" | ||
> | ||
mutation={graphql` | ||
mutation AcceptGroupInvitePageMutation( | ||
$input: MutationAcceptReusableGroupInviteTokenInput! | ||
) { | ||
result: acceptReusableGroupInviteToken(input: $input) { | ||
__typename | ||
... on BaseError { | ||
message | ||
} | ||
... on AcceptReusableGroupInviteTokenResult { | ||
__typename | ||
membership { | ||
group { | ||
id | ||
slug | ||
...hooks_useIsGroupMember | ||
} | ||
} | ||
} | ||
} | ||
} | ||
`} | ||
expectedTypename="AcceptReusableGroupInviteTokenResult" | ||
variables={{ | ||
input: { | ||
groupSlug: group.slug, | ||
inviteToken, | ||
}, | ||
}} | ||
title="Join this group" | ||
theme="primary" | ||
onCompleted={() => toast("Joined", "confirmation")} | ||
/> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { loadPageQuery } from "@/relay/loadPageQuery"; | ||
import { AcceptGroupInvitePage } from "./AcceptGroupInvitePage"; | ||
import QueryNode, { | ||
AcceptGroupInvitePageQuery, | ||
} from "@/__generated__/AcceptGroupInvitePageQuery.graphql"; | ||
|
||
type Props = { | ||
params: { slug: string }; | ||
}; | ||
|
||
export default async function OuterAcceptGroupInvitePage({ params }: Props) { | ||
const query = await loadPageQuery<AcceptGroupInvitePageQuery>(QueryNode, { | ||
slug: params.slug, | ||
}); | ||
|
||
return <AcceptGroupInvitePage query={query} />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
8e2eac0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
quri-hub – ./packages/hub
quri-hub-quantified-uncertainty.vercel.app
quri-hub-git-main-quantified-uncertainty.vercel.app
squigglehub.org
squiggle-hub.org
squigglehub.com
www.squiggle-hub.org
www.squigglehub.com
www.squiggle-hub.com
www.squigglehub.org
squiggle-hub.com