42 lines · 1.9 KBRaw
| 1 | import { NONE, taskLink } from "@refs/backend/contract"; |
| 2 | import { redirect } from "react-router"; |
| 3 | import type { Route } from "./+types/task-new"; |
| 4 | import { AppShell, PageHeader } from "../components/shell"; |
| 5 | import { TaskForm } from "../components/task-form"; |
| 6 | import { failed, formList, formText, signedIn } from "../lib/session"; |
| 7 | |
| 8 | export const meta: Route.MetaFunction = () => [{ title: "New task · refs" }]; |
| 9 | |
| 10 | export async function loader({ request, context }: Route.LoaderArgs) { |
| 11 | const choices = await signedIn(context).taskChoices(); |
| 12 | const requested = new URL(request.url).searchParams.get("repo"); |
| 13 | const repo = choices.repos.find((choice) => choice.name === requested)?.name ?? ""; |
| 14 | return { choices, values: { title: "", body: "", repo, assignee: "" }, cancelHref: repo ? `/${repo}/tasks` : "/tasks" }; |
| 15 | } |
| 16 | |
| 17 | export async function action({ request, context }: Route.ActionArgs) { |
| 18 | const form = await request.formData(); |
| 19 | const assignee = formText(form, "assignee"); |
| 20 | const values = { title: formText(form, "title"), body: formText(form, "body"), repo: formText(form, "repo"), assignee: assignee === NONE ? "" : assignee }; |
| 21 | const labels = formList(form, "label"); |
| 22 | const outcome = await signedIn(context).createTask({ ...values, assignee: values.assignee || null, labels }); |
| 23 | if (!outcome.ok) return failed(outcome.problem, { values, labels }); |
| 24 | throw redirect(taskLink(outcome.value.repo, outcome.value.number)); |
| 25 | } |
| 26 | |
| 27 | export default function NewTask({ loaderData, actionData }: Route.ComponentProps) { |
| 28 | return ( |
| 29 | <AppShell> |
| 30 | <PageHeader title="New task" /> |
| 31 | <TaskForm |
| 32 | values={actionData?.values ?? loaderData.values} |
| 33 | initialLabels={actionData?.labels} |
| 34 | error={actionData?.error} |
| 35 | repos={loaderData.choices.repos} |
| 36 | people={loaderData.choices.people} |
| 37 | labels={loaderData.choices.labels} |
| 38 | cancelHref={loaderData.cancelHref} |
| 39 | /> |
| 40 | </AppShell> |
| 41 | ); |
| 42 | } |