From aa065705ce243baa0f7673cc22381cf3ce3a0cba Mon Sep 17 00:00:00 2001 From: Matt Aitken Date: Thu, 15 Dec 2022 14:57:33 +0000 Subject: [PATCH] Create new org page working --- apps/webapp/app/routes/__app/orgs/new.tsx | 116 ++++++++++++++++++++++ 1 file changed, 116 insertions(+) diff --git a/apps/webapp/app/routes/__app/orgs/new.tsx b/apps/webapp/app/routes/__app/orgs/new.tsx index e69de29bb..f409c638b 100644 --- a/apps/webapp/app/routes/__app/orgs/new.tsx +++ b/apps/webapp/app/routes/__app/orgs/new.tsx @@ -0,0 +1,116 @@ +import { BriefcaseIcon } from "@heroicons/react/24/outline"; +import type { ActionFunction } from "@remix-run/node"; +import { json, redirect } from "@remix-run/node"; +import { Form, useActionData } from "@remix-run/react"; +import * as React from "react"; +import { PrimaryButton, SecondaryLink } from "~/components/primitives/Buttons"; +import { createOrganization } from "~/models/organization.server"; +import { requireUserId } from "~/services/session.server"; + +type ActionData = { + errors?: { + title?: string; + body?: string; + }; +}; + +export const action: ActionFunction = async ({ request }) => { + const userId = await requireUserId(request); + + const formData = await request.formData(); + const title = formData.get("title"); + if (typeof title !== "string" || title.length === 0) { + return json( + { errors: { title: "A Organization title is required." } }, + { status: 400 } + ); + } + + try { + const organization = await createOrganization({ title, userId }); + + return redirect(`/orgs/${organization.slug}`); + } catch (error: any) { + return json( + { errors: { body: error.message } }, + { status: 400 } + ); + } +}; + +export default function NewOrganizationPage() { + const actionData = useActionData() as ActionData; + const titleRef = React.useRef(null); + + React.useEffect(() => { + if (actionData?.errors?.title) { + titleRef.current?.focus(); + } + }, [actionData]); + + return ( +
+
+

+ Create a new Organization +

+

+ Use Organizations to hold a collection of Projects. A typical + Organization is named after a company or team. +

+
+
+
+ +
+
+ +
+ +
+
+ {actionData?.errors?.title && ( +
+ {actionData.errors.title} +
+ )} +
+ +
+ + Cancel + + + Create + +
+
+
+
+ ); +}