54 lines · 2.4 KBRaw
| 1 | import { Banner, Button, Input, LayerCard, LinkButton } from "@refs/design-system"; |
| 2 | import { Form, redirect, useNavigation } from "react-router"; |
| 3 | import type { Route } from "./+types/organization-new"; |
| 4 | import { AppShell, PageHeader } from "../components/shell"; |
| 5 | import { failed, formText, namespaceCookie, signedIn } from "../lib/session"; |
| 6 | |
| 7 | export const meta: Route.MetaFunction = () => [{ title: "New organization · refs" }]; |
| 8 | |
| 9 | export function loader({ context }: Route.LoaderArgs) { |
| 10 | signedIn(context); |
| 11 | return null; |
| 12 | } |
| 13 | |
| 14 | export async function action({ request, context }: Route.ActionArgs) { |
| 15 | const form = await request.formData(); |
| 16 | const values = { slug: formText(form, "slug").toLowerCase(), name: formText(form, "name") }; |
| 17 | const outcome = await signedIn(context).createOrganization(values); |
| 18 | if (!outcome.ok) return failed(outcome.problem, { values }); |
| 19 | throw redirect(`/${outcome.value.slug}`, { headers: { "Set-Cookie": namespaceCookie(outcome.value.slug) } }); |
| 20 | } |
| 21 | |
| 22 | export default function NewOrganization({ actionData }: Route.ComponentProps) { |
| 23 | const submitting = useNavigation().state === "submitting"; |
| 24 | const values = actionData?.values; |
| 25 | return ( |
| 26 | <AppShell> |
| 27 | <PageHeader title="New organization" description="An organization owns repositories that its members share. You become its owner." /> |
| 28 | <Form key={JSON.stringify(actionData ?? null)} method="post" className="flex max-w-xl flex-col gap-5"> |
| 29 | {actionData?.error && <Banner variant="error" description={actionData.error} />} |
| 30 | <LayerCard className="flex flex-col gap-4 p-5"> |
| 31 | <Input |
| 32 | label="Organization name" |
| 33 | name="slug" |
| 34 | defaultValue={values?.slug} |
| 35 | maxLength={39} |
| 36 | pattern="[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,37}[a-zA-Z0-9])?" |
| 37 | description="Its URL: repositories live at /name/repo. Usernames and organizations share these names." |
| 38 | required |
| 39 | autoFocus |
| 40 | /> |
| 41 | <Input label="Display name" name="name" defaultValue={values?.name} maxLength={64} description="Optional. Shown in the header instead of the URL name." /> |
| 42 | </LayerCard> |
| 43 | <div className="flex items-center gap-2"> |
| 44 | <Button type="submit" variant="primary" loading={submitting}> |
| 45 | Create organization |
| 46 | </Button> |
| 47 | <LinkButton href="/" variant="secondary"> |
| 48 | Cancel |
| 49 | </LinkButton> |
| 50 | </div> |
| 51 | </Form> |
| 52 | </AppShell> |
| 53 | ); |
| 54 | } |