| | | 1 | | import { redirect } from "react-router"; |
| | | 2 | | import type { Route } from "./+types/locations.rooms.items.new"; |
| | | 3 | | import { getLocation, getRoom, createItem } from "~/api/client"; |
| | | 4 | | import { Breadcrumb } from "~/components/breadcrumb"; |
| | | 5 | | import { ItemForm } from "~/components/items"; |
| | | 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 | | const roomId = Number(params.roomId); |
| | 0 | 13 | | if (Number.isNaN(locationId) || Number.isNaN(roomId)) { |
| | 0 | 14 | | throw new Response("Not found", { status: 404 }); |
| | | 15 | | } |
| | 0 | 16 | | const [location, room] = await Promise.all([ |
| | | 17 | | getLocation(locationId, request), |
| | | 18 | | getRoom(roomId, request), |
| | | 19 | | ]); |
| | 0 | 20 | | if (!location || !room || room.locationId !== locationId) { |
| | 0 | 21 | | throw new Response("Not found", { status: 404 }); |
| | | 22 | | } |
| | 0 | 23 | | return { location, room }; |
| | | 24 | | } |
| | | 25 | | |
| | 0 | 26 | | export async function action({ request, params }: Route.ActionArgs) { |
| | 0 | 27 | | const roomId = Number(params.roomId); |
| | 0 | 28 | | const locationId = Number(params.id); |
| | 0 | 29 | | if (Number.isNaN(roomId) || Number.isNaN(locationId)) { |
| | 0 | 30 | | throw new Response("Not found", { status: 404 }); |
| | | 31 | | } |
| | 0 | 32 | | const formData = await request.formData(); |
| | 0 | 33 | | const name = formData.get("name"); |
| | 0 | 34 | | const description = formData.get("description"); |
| | 0 | 35 | | const category = formData.get("category"); |
| | 0 | 36 | | const notes = formData.get("notes"); |
| | 0 | 37 | | if (typeof name !== "string" || !name.trim()) { |
| | 0 | 38 | | return { ok: false as const, error: "Name is required" }; |
| | | 39 | | } |
| | 0 | 40 | | const result = await tryApi(() => |
| | | 41 | | createItem( |
| | | 42 | | { |
| | | 43 | | roomId, |
| | | 44 | | name: name.trim(), |
| | | 45 | | description: |
| | | 46 | | typeof description === "string" && description.trim() |
| | | 47 | | ? description.trim() |
| | | 48 | | : undefined, |
| | | 49 | | category: |
| | | 50 | | typeof category === "string" && category.trim() |
| | | 51 | | ? category.trim() |
| | | 52 | | : undefined, |
| | | 53 | | notes: |
| | | 54 | | typeof notes === "string" && notes.trim() ? notes.trim() : undefined, |
| | | 55 | | }, |
| | | 56 | | request, |
| | | 57 | | ), |
| | | 58 | | ); |
| | 0 | 59 | | if (!result.ok) return result; |
| | 0 | 60 | | await pushFlash(request, { |
| | | 61 | | kind: "success", |
| | | 62 | | message: `Item "${result.data.name}" created`, |
| | | 63 | | }); |
| | 0 | 64 | | return redirect(`/locations/${locationId}/rooms/${roomId}/items`); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | export function meta({ loaderData }: Route.MetaArgs) { |
| | 0 | 68 | | const roomName = loaderData?.room?.name ?? "Room"; |
| | 0 | 69 | | return [{ title: `Add item · ${roomName} | ClutterStock` }]; |
| | | 70 | | } |
| | | 71 | | |
| | 0 | 72 | | export default function LocationsRoomsItemsNew({ |
| | | 73 | | loaderData, |
| | | 74 | | actionData, |
| | | 75 | | }: Route.ComponentProps) { |
| | 0 | 76 | | useToastFromActionData(actionData); |
| | 0 | 77 | | const { location, room } = loaderData; |
| | 0 | 78 | | const locationId = location.id!; |
| | 0 | 79 | | const roomId = room.id!; |
| | | 80 | | const error = |
| | 0 | 81 | | actionData && "error" in actionData ? actionData.error : undefined; |
| | | 82 | | const fieldErrors = |
| | 0 | 83 | | actionData && "fieldErrors" in actionData ? actionData.fieldErrors : undefined; |
| | | 84 | | |
| | 0 | 85 | | return ( |
| | | 86 | | <> |
| | | 87 | | <Breadcrumb |
| | | 88 | | crumbs={[ |
| | | 89 | | { label: "Locations", to: "/locations" }, |
| | | 90 | | { label: location.name ?? "Location", to: `/locations/${locationId}/edit` }, |
| | | 91 | | { label: "Rooms", to: `/locations/${locationId}/rooms` }, |
| | | 92 | | { label: room.name ?? "Room", to: `/locations/${locationId}/rooms/${roomId}/edit` }, |
| | | 93 | | { label: "Items", to: `/locations/${locationId}/rooms/${roomId}/items` }, |
| | | 94 | | { label: "Add item" }, |
| | | 95 | | ]} |
| | | 96 | | /> |
| | | 97 | | <ItemForm |
| | | 98 | | title="Add item" |
| | | 99 | | submitLabel="Create" |
| | | 100 | | error={error} |
| | | 101 | | fieldErrors={fieldErrors} |
| | | 102 | | roomId={roomId} |
| | | 103 | | cancelTo={`/locations/${locationId}/rooms/${roomId}/items`} |
| | | 104 | | /> |
| | | 105 | | </> |
| | | 106 | | ); |
| | | 107 | | } |