| | | 1 | | import { Link, redirect } from "react-router"; |
| | | 2 | | import type { Route } from "./+types/locations.rooms.index"; |
| | | 3 | | import { deleteRoom, getLocation, getRooms } from "~/api/client"; |
| | | 4 | | import { Breadcrumb } from "~/components/breadcrumb"; |
| | | 5 | | import { RoomsList } from "~/components/rooms"; |
| | | 6 | | |
| | 0 | 7 | | export async function loader({ params, request }: Route.LoaderArgs) { |
| | 0 | 8 | | const locationId = Number(params.id); |
| | 0 | 9 | | if (Number.isNaN(locationId)) throw new Response("Not found", { status: 404 }); |
| | 0 | 10 | | const [location, allRooms] = await Promise.all([ |
| | | 11 | | getLocation(locationId, request), |
| | | 12 | | getRooms(request), |
| | | 13 | | ]); |
| | 0 | 14 | | if (!location) throw new Response("Not found", { status: 404 }); |
| | 0 | 15 | | const rooms = allRooms.filter((r) => r.locationId === locationId); |
| | 0 | 16 | | return { location, rooms }; |
| | | 17 | | } |
| | | 18 | | |
| | 0 | 19 | | export async function action({ request, params }: Route.ActionArgs) { |
| | 0 | 20 | | const locationId = Number(params.id); |
| | 0 | 21 | | if (Number.isNaN(locationId)) throw new Response("Not found", { status: 404 }); |
| | 0 | 22 | | const formData = await request.formData(); |
| | 0 | 23 | | if (formData.get("_action") !== "delete") return null; |
| | 0 | 24 | | const roomId = Number(formData.get("id")); |
| | 0 | 25 | | if (Number.isNaN(roomId)) return null; |
| | 0 | 26 | | await deleteRoom(roomId, request); |
| | 0 | 27 | | return redirect(`/locations/${locationId}/rooms`); |
| | | 28 | | } |
| | | 29 | | |
| | 0 | 30 | | export function meta({ loaderData }: Route.MetaArgs) { |
| | 0 | 31 | | const name = loaderData?.location?.name ?? "Location"; |
| | 0 | 32 | | return [{ title: `Rooms · ${name} | ClutterStock` }]; |
| | | 33 | | } |
| | | 34 | | |
| | 0 | 35 | | export default function LocationsRoomsIndex({ loaderData }: Route.ComponentProps) { |
| | 0 | 36 | | const { location, rooms } = loaderData; |
| | 0 | 37 | | const locationId = location.id!; |
| | | 38 | | |
| | 0 | 39 | | return ( |
| | | 40 | | <> |
| | | 41 | | <Breadcrumb |
| | | 42 | | crumbs={[ |
| | | 43 | | { label: "Locations", to: "/locations" }, |
| | | 44 | | { label: location.name ?? "Location", to: `/locations/${locationId}/edit` }, |
| | | 45 | | { label: "Rooms" }, |
| | | 46 | | ]} |
| | | 47 | | /> |
| | | 48 | | <div className="page-header"> |
| | | 49 | | <h2 className="page-title">Rooms</h2> |
| | | 50 | | <Link to={`/locations/${locationId}/rooms/new`} className="btn-primary"> |
| | | 51 | | + Add room |
| | | 52 | | </Link> |
| | | 53 | | </div> |
| | | 54 | | <RoomsList |
| | | 55 | | locationId={locationId} |
| | | 56 | | locationName={location.name ?? "this location"} |
| | | 57 | | rooms={rooms} |
| | | 58 | | /> |
| | | 59 | | </> |
| | | 60 | | ); |
| | | 61 | | } |