31 lines · 1.5 KBRaw
| 1 | import { redirect } from "react-router"; |
| 2 | import type { Route } from "./+types/new"; |
| 3 | import { RepoForm, readRepoForm } from "../components/repo-form"; |
| 4 | import { AppShell, PageHeader } from "../components/shell"; |
| 5 | import { failed, formText, signedIn } from "../lib/session"; |
| 6 | |
| 7 | export const meta: Route.MetaFunction = () => [{ title: "New repository · refs" }]; |
| 8 | |
| 9 | export function loader({ context }: Route.LoaderArgs) { |
| 10 | const { viewer } = signedIn(context); |
| 11 | const current = viewer.namespaces.find((namespace) => namespace.slug === viewer.namespace?.slug); |
| 12 | return { owners: viewer.namespaces, owner: current?.slug ?? viewer.namespaces[0]?.slug }; |
| 13 | } |
| 14 | |
| 15 | export async function action({ request, context }: Route.ActionArgs) { |
| 16 | const refs = signedIn(context); |
| 17 | const form = await request.formData(); |
| 18 | const values = { ...readRepoForm(form), importUrl: formText(form, "import_url"), setUp: formText(form, "set_up") === "1" }; |
| 19 | const outcome = await refs.createRepo({ ...values, importFrom: values.importUrl || undefined }); |
| 20 | if (!outcome.ok) return failed(outcome.problem, { values }); |
| 21 | throw redirect(`/${outcome.value.name}`); |
| 22 | } |
| 23 | |
| 24 | export default function NewRepository({ loaderData, actionData }: Route.ComponentProps) { |
| 25 | return ( |
| 26 | <AppShell> |
| 27 | <PageHeader title="New repository" description="Start an empty repository, or import one from a public Git URL." /> |
| 28 | <RepoForm submit="Create repository" error={actionData?.error} values={actionData?.values ?? { owner: loaderData.owner }} owners={loaderData.owners} allowImport /> |
| 29 | </AppShell> |
| 30 | ); |
| 31 | } |