| 1 | import type { Route } from "./+types/invites"; |
| 2 | import { page } from "../../lib/meta"; |
| 3 | import { assertSameOrigin, requireUser } from "../../lib/session.server"; |
| 4 | import { InvitesSection } from "../../components/invites-section"; |
| 5 | import { inviteAction, loadBringInto, loadInvites } from "../../lib/invites.server"; |
| 6 | |
| 7 | export function meta(args: Route.MetaArgs) { |
| 8 | return page(args, { title: "Invites to g1t · Settings · g1t" }); |
| 9 | } |
| 10 | |
| 11 | /** Invite a friend: your invites to g1t, how many are left, and who joined. */ |
| 12 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 13 | const user = requireUser(context, request); |
| 14 | const url = new URL(request.url); |
| 15 | const [invites, bringInto] = await Promise.all([loadInvites(user), loadBringInto(user).catch(() => [])]); |
| 16 | // `?new` comes from the account menu's Invite a friend: the dialog opens at once. |
| 17 | return { invites, bringInto, origin: url.origin, openOnLoad: url.searchParams.has("new") }; |
| 18 | } |
| 19 | |
| 20 | export async function action({ request, context }: Route.ActionArgs) { |
| 21 | assertSameOrigin(request); |
| 22 | const user = requireUser(context, request); |
| 23 | // Making, revoking and resending invites (lib/invites.server.ts). |
| 24 | return inviteAction(user, await request.formData()); |
| 25 | } |
| 26 | |
| 27 | export default function InviteSettings({ loaderData }: Route.ComponentProps) { |
| 28 | return ( |
| 29 | <InvitesSection |
| 30 | overview={loaderData.invites} |
| 31 | bringInto={loaderData.bringInto} |
| 32 | origin={loaderData.origin} |
| 33 | openOnLoad={loaderData.openOnLoad} |
| 34 | /> |
| 35 | ); |
| 36 | } |