| | | 1 | | import { redirect } from "react-router"; |
| | | 2 | | import type { Route } from "./+types/locations.rooms.new"; |
| | | 3 | | import { getLocation, createRoom } from "~/api/client"; |
| | | 4 | | import { Breadcrumb } from "~/components/breadcrumb"; |
| | | 5 | | import { RoomForm } from "~/components/rooms"; |
| | | 6 | | import { tryApi } from "~/lib/action-helpers.server"; |
| | | 7 | | import { useToastFromActionData } from "~/lib/toasts"; |
| | | 8 | | import { pushFlash } from "~/lib/toasts.server"; |
| | | 9 | | |
| | 0 | 10 | | export async function loader({ params, request }: Route.LoaderArgs) { |
| | 0 | 11 | | const locationId = Number(params.id); |
| | 0 | 12 | | if (Number.isNaN(locationId)) throw new Response("Not found", { status: 404 }); |
| | 0 | 13 | | const location = await getLocation(locationId, request); |
| | 0 | 14 | | if (!location) throw new Response("Not found", { status: 404 }); |
| | 0 | 15 | | return { location }; |
| | | 16 | | } |
| | | 17 | | |
| | 0 | 18 | | export async function action({ request, params }: Route.ActionArgs) { |
| | 0 | 19 | | const locationId = Number(params.id); |
| | 0 | 20 | | if (Number.isNaN(locationId)) throw new Response("Not found", { status: 404 }); |
| | 0 | 21 | | const formData = await request.formData(); |
| | 0 | 22 | | const name = formData.get("name"); |
| | 0 | 23 | | const description = formData.get("description"); |
| | 0 | 24 | | if (typeof name !== "string" || !name.trim()) { |
| | 0 | 25 | | return { ok: false as const, error: "Name is required" }; |
| | | 26 | | } |
| | 0 | 27 | | const result = await tryApi(() => |
| | | 28 | | createRoom( |
| | | 29 | | { |
| | | 30 | | locationId, |
| | | 31 | | name: name.trim(), |
| | | 32 | | description: |
| | | 33 | | typeof description === "string" && description.trim() |
| | | 34 | | ? description.trim() |
| | | 35 | | : undefined, |
| | | 36 | | }, |
| | | 37 | | request, |
| | | 38 | | ), |
| | | 39 | | ); |
| | 0 | 40 | | if (!result.ok) return result; |
| | 0 | 41 | | await pushFlash(request, { |
| | | 42 | | kind: "success", |
| | | 43 | | message: `Room "${result.data.name}" created`, |
| | | 44 | | }); |
| | 0 | 45 | | return redirect(`/locations/${locationId}/rooms`); |
| | | 46 | | } |
| | | 47 | | |
| | 0 | 48 | | export function meta({ loaderData }: Route.MetaArgs) { |
| | 0 | 49 | | const name = loaderData?.location?.name ?? "Location"; |
| | 0 | 50 | | return [{ title: `Add room · ${name} | ClutterStock` }]; |
| | | 51 | | } |
| | | 52 | | |
| | 0 | 53 | | export default function LocationsRoomsNew({ |
| | | 54 | | loaderData, |
| | | 55 | | actionData, |
| | | 56 | | }: Route.ComponentProps) { |
| | 0 | 57 | | useToastFromActionData(actionData); |
| | 0 | 58 | | const location = loaderData.location; |
| | 0 | 59 | | const locationId = location.id!; |
| | | 60 | | const error = |
| | 0 | 61 | | actionData && "error" in actionData ? actionData.error : undefined; |
| | | 62 | | const fieldErrors = |
| | 0 | 63 | | actionData && "fieldErrors" in actionData ? actionData.fieldErrors : undefined; |
| | | 64 | | |
| | 0 | 65 | | return ( |
| | | 66 | | <> |
| | | 67 | | <Breadcrumb |
| | | 68 | | crumbs={[ |
| | | 69 | | { label: "Locations", to: "/locations" }, |
| | | 70 | | { label: location.name ?? "Location", to: `/locations/${locationId}/edit` }, |
| | | 71 | | { label: "Rooms", to: `/locations/${locationId}/rooms` }, |
| | | 72 | | { label: "Add room" }, |
| | | 73 | | ]} |
| | | 74 | | /> |
| | | 75 | | <RoomForm |
| | | 76 | | title="Add room" |
| | | 77 | | submitLabel="Create" |
| | | 78 | | error={error} |
| | | 79 | | fieldErrors={fieldErrors} |
| | | 80 | | locationId={locationId} |
| | | 81 | | cancelTo={`/locations/${locationId}/rooms`} |
| | | 82 | | /> |
| | | 83 | | </> |
| | | 84 | | ); |
| | | 85 | | } |