| | | 1 | | import { redirect } from "react-router"; |
| | | 2 | | import type { Route } from "./+types/locations.rooms.edit"; |
| | | 3 | | import { routes } from "~/constants/routes"; |
| | | 4 | | import { deleteRoom, getLocation, getRoom, updateRoom } from "~/api/client"; |
| | | 5 | | import { Breadcrumb } from "~/components/breadcrumb"; |
| | | 6 | | import { RoomForm } from "~/features/rooms"; |
| | | 7 | | import { tryApi } from "~/lib/action-helpers.server"; |
| | | 8 | | import { useToastFromActionData } from "~/lib/toasts"; |
| | | 9 | | import { pushFlash } from "~/lib/toasts.server"; |
| | | 10 | | |
| | 0 | 11 | | export async function loader({ params, request }: Route.LoaderArgs) { |
| | 0 | 12 | | const locationId = Number(params.id); |
| | 0 | 13 | | const roomId = Number(params.roomId); |
| | 0 | 14 | | if (Number.isNaN(locationId) || Number.isNaN(roomId)) { |
| | 0 | 15 | | throw new Response("Not found", { status: 404 }); |
| | | 16 | | } |
| | 0 | 17 | | const [location, room] = await Promise.all([ |
| | | 18 | | getLocation(locationId, request), |
| | | 19 | | getRoom(roomId, request), |
| | | 20 | | ]); |
| | 0 | 21 | | if (!location || !room || room?.locationId !== locationId) { |
| | 0 | 22 | | throw new Response("Not found", { status: 404 }); |
| | | 23 | | } |
| | 0 | 24 | | return { location, room } as const; |
| | | 25 | | } |
| | | 26 | | |
| | 0 | 27 | | export async function action({ request, params }: Route.ActionArgs) { |
| | 0 | 28 | | const roomId = Number(params.roomId); |
| | 0 | 29 | | const locationId = Number(params.id); |
| | 0 | 30 | | if (Number.isNaN(roomId) || Number.isNaN(locationId)) { |
| | 0 | 31 | | throw new Response("Not found", { status: 404 }); |
| | | 32 | | } |
| | 0 | 33 | | const formData = await request.formData(); |
| | 0 | 34 | | if (formData.get("_action") === "delete") { |
| | 0 | 35 | | const del = await tryApi(() => deleteRoom(roomId, request)); |
| | 0 | 36 | | if (!del.ok) return del; |
| | 0 | 37 | | await pushFlash(request, { kind: "success", message: "Room deleted" }); |
| | 0 | 38 | | return redirect(routes.locations.rooms(locationId)); |
| | | 39 | | } |
| | 0 | 40 | | const name = formData.get("name"); |
| | 0 | 41 | | const description = formData.get("description"); |
| | 0 | 42 | | if (typeof name !== "string" || !name.trim()) { |
| | 0 | 43 | | return { ok: false as const, error: "Name is required" }; |
| | | 44 | | } |
| | 0 | 45 | | const result = await tryApi(() => |
| | | 46 | | updateRoom( |
| | | 47 | | roomId, |
| | | 48 | | { |
| | | 49 | | locationId, |
| | | 50 | | name: name.trim(), |
| | | 51 | | description: |
| | | 52 | | typeof description === "string" && description.trim() |
| | | 53 | | ? description.trim() |
| | | 54 | | : undefined, |
| | | 55 | | }, |
| | | 56 | | request, |
| | | 57 | | ), |
| | | 58 | | ); |
| | 0 | 59 | | if (!result.ok) return result; |
| | 0 | 60 | | await pushFlash(request, { |
| | | 61 | | kind: "success", |
| | | 62 | | message: `Room "${result.data.name}" updated`, |
| | | 63 | | }); |
| | 0 | 64 | | return redirect(routes.locations.rooms(locationId)); |
| | | 65 | | } |
| | | 66 | | |
| | 0 | 67 | | export function meta({ loaderData }: Route.MetaArgs) { |
| | 0 | 68 | | const name = loaderData?.room?.name ?? "Room"; |
| | 0 | 69 | | return [{ title: `Edit ${name} | ClutterStock` }]; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | type LoaderData = Awaited<ReturnType<typeof loader>>; |
| | | 73 | | |
| | 0 | 74 | | export default function LocationsRoomsEdit({ |
| | | 75 | | loaderData, |
| | | 76 | | actionData, |
| | | 77 | | }: Route.ComponentProps) { |
| | 0 | 78 | | useToastFromActionData(actionData); |
| | 0 | 79 | | if (!loaderData) return null; |
| | 0 | 80 | | const { location, room } = loaderData as LoaderData; |
| | 0 | 81 | | const locationId = location.id!; |
| | | 82 | | const error = |
| | 0 | 83 | | actionData != null && |
| | | 84 | | typeof actionData === "object" && |
| | | 85 | | "error" in actionData |
| | | 86 | | ? (actionData as { error: string }).error |
| | | 87 | | : undefined; |
| | | 88 | | const fieldErrors = |
| | 0 | 89 | | actionData != null && |
| | | 90 | | typeof actionData === "object" && |
| | | 91 | | "fieldErrors" in actionData |
| | | 92 | | ? (actionData as { fieldErrors?: Record<string, string[]> }).fieldErrors |
| | | 93 | | : undefined; |
| | | 94 | | |
| | 0 | 95 | | return ( |
| | | 96 | | <> |
| | | 97 | | <Breadcrumb |
| | | 98 | | crumbs={[ |
| | | 99 | | { label: "Locations", to: routes.locations.list() }, |
| | | 100 | | { label: location.name ?? "Location", to: routes.locations.edit(locationId) }, |
| | | 101 | | { label: "Rooms", to: routes.locations.rooms(locationId) }, |
| | | 102 | | { label: room.name ?? "Room" }, |
| | | 103 | | ]} |
| | | 104 | | /> |
| | | 105 | | <RoomForm |
| | | 106 | | title="Edit room" |
| | | 107 | | submitLabel="Save" |
| | | 108 | | error={error} |
| | | 109 | | fieldErrors={fieldErrors} |
| | | 110 | | room={room} |
| | | 111 | | locationId={locationId} |
| | | 112 | | cancelTo={routes.locations.rooms(locationId)} |
| | | 113 | | /> |
| | | 114 | | </> |
| | | 115 | | ); |
| | | 116 | | } |