|
| 1 | +import { Box, CloseIcon, Flex, Heading, Tag } from "@nycplanning/streetscape"; |
| 2 | +import { useLoaderData } from "react-router"; |
| 3 | +import { useUpdateSearchParams } from "~/utils/utils"; |
| 4 | +import { loader } from "~/layouts/ResultsPanel"; |
| 5 | +import { ClearFilterBtn } from "./ClearFilter"; |
| 6 | + |
| 7 | +export function SelectedLocations() { |
| 8 | + const { |
| 9 | + boroughsResponse: { boroughs }, |
| 10 | + } = useLoaderData<typeof loader>(); |
| 11 | + |
| 12 | + const [searchParams, updateSearchParams] = useUpdateSearchParams(); |
| 13 | + const boundaryType = searchParams.get("boundaryType"); |
| 14 | + const boroughId = searchParams.get("boroughId"); |
| 15 | + const boundaryId = searchParams.get("boundaryId"); |
| 16 | + |
| 17 | + const boroughIds = searchParams.get("boroughIds"); |
| 18 | + |
| 19 | + const tagLabel = (() => { |
| 20 | + if (boundaryType === "ccd" && boundaryId !== null) { |
| 21 | + return ( |
| 22 | + <> |
| 23 | + <span style={{ fontWeight: "bold" }}>City Council</span> |
| 24 | + <span style={{ paddingRight: "2px" }}>{` | ${boundaryId}`}</span> |
| 25 | + </> |
| 26 | + ); |
| 27 | + } |
| 28 | + |
| 29 | + const borough = boroughs.find((b) => b.id === (boroughId ?? boroughIds)); |
| 30 | + |
| 31 | + if (!borough) return null; |
| 32 | + |
| 33 | + if (boundaryType === "cd" && boundaryId !== null) { |
| 34 | + return ( |
| 35 | + <> |
| 36 | + <span style={{ fontWeight: "bold" }}>{borough.title}</span> |
| 37 | + <span style={{ paddingRight: "2px" }}>{` | CD ${boundaryId}`}</span> |
| 38 | + </> |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (boundaryType === "borough") { |
| 43 | + return <span style={{ fontWeight: "bold" }}>{borough.title}</span>; |
| 44 | + } |
| 45 | + |
| 46 | + return null; |
| 47 | + })(); |
| 48 | + |
| 49 | + const clearSelections = () => { |
| 50 | + updateSearchParams({ |
| 51 | + boundaryType: null, |
| 52 | + boroughId: null, |
| 53 | + boundaryId: null, |
| 54 | + boroughIds: null, |
| 55 | + }); |
| 56 | + }; |
| 57 | + |
| 58 | + const clearTag = () => { |
| 59 | + if (boundaryType === "ccd") { |
| 60 | + updateSearchParams({ |
| 61 | + boundaryType: null, |
| 62 | + boundaryId: null, |
| 63 | + boroughId: null, |
| 64 | + }); |
| 65 | + } else if (boundaryType === "cd") { |
| 66 | + updateSearchParams({ |
| 67 | + boundaryType: null, |
| 68 | + boroughId: null, |
| 69 | + boundaryId: null, |
| 70 | + }); |
| 71 | + } else if (boundaryType === "borough") { |
| 72 | + updateSearchParams({ |
| 73 | + boundaryType: null, |
| 74 | + boroughIds: null, |
| 75 | + }); |
| 76 | + } |
| 77 | + }; |
| 78 | + |
| 79 | + return ( |
| 80 | + <Flex |
| 81 | + direction="column" |
| 82 | + gap={"10px"} |
| 83 | + border={"1px solid"} |
| 84 | + borderColor={"gray.300"} |
| 85 | + borderRadius={"12px"} |
| 86 | + background={"gray.50"} |
| 87 | + padding={4} |
| 88 | + height={"82px"} |
| 89 | + > |
| 90 | + <Flex |
| 91 | + className="title-section" |
| 92 | + alignItems={"flex-start"} |
| 93 | + justify={"space-between"} |
| 94 | + height={"20px"} |
| 95 | + > |
| 96 | + <Box alignSelf={"center"}> |
| 97 | + <Heading fontSize={"xs"} fontWeight={"bold"}> |
| 98 | + SELECTED LOCATION |
| 99 | + </Heading> |
| 100 | + </Box> |
| 101 | + <Box alignSelf={"center"}> |
| 102 | + <ClearFilterBtn |
| 103 | + textAlign={"right"} |
| 104 | + verticalAlign={"text-bottom"} |
| 105 | + onClear={clearSelections} |
| 106 | + buttonLabel="Clear" |
| 107 | + ></ClearFilterBtn> |
| 108 | + </Box> |
| 109 | + </Flex> |
| 110 | + |
| 111 | + <Flex |
| 112 | + justifyContent={"flex-start"} |
| 113 | + alignItems={"center"} |
| 114 | + height={"20px"} |
| 115 | + padding={"2px 4px"} |
| 116 | + className={"tag-container"} |
| 117 | + > |
| 118 | + {tagLabel !== null && ( |
| 119 | + <Tag |
| 120 | + display={"flex"} |
| 121 | + justifyContent={"space-between"} |
| 122 | + alignItems={"center"} |
| 123 | + gap={"4px"} |
| 124 | + fontSize={"11px"} |
| 125 | + borderRadius={"4px"} |
| 126 | + background={"primary.100"} |
| 127 | + color={"teal.700"} |
| 128 | + > |
| 129 | + <Box className={"tag-information"}>{tagLabel}</Box> |
| 130 | + <Box> |
| 131 | + <CloseIcon |
| 132 | + color={"teal.700"} |
| 133 | + width={"8px"} |
| 134 | + height={"8px"} |
| 135 | + onClick={clearTag} |
| 136 | + aria-label={"closeIcon"} |
| 137 | + sx={{ |
| 138 | + cursor: "pointer", |
| 139 | + }} |
| 140 | + /> |
| 141 | + </Box> |
| 142 | + </Tag> |
| 143 | + )} |
| 144 | + </Flex> |
| 145 | + </Flex> |
| 146 | + ); |
| 147 | +} |
0 commit comments