Skip to content

Commit

Permalink
Generalize query params with new type and existing change param func
Browse files Browse the repository at this point in the history
Generalize considering pervious query params

Reset page to 1 when going to new district

Remove unused import

Use existing set search params util function to merge params

Include admin params only when going to new district

Remove unused optional param list

Combine types with SearchParamChanges and update tests

Remove unused AdminParams
  • Loading branch information
pratishta committed Sep 23, 2024
1 parent bce9648 commit 5c2e626
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 104 deletions.
9 changes: 6 additions & 3 deletions app/components/AdminDropdown/BoroughDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ describe("BoroughDropdown", () => {
const updateSearchParams = vi.fn();
render(
<BoroughDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
boroughs={boroughs}
/>,
);
Expand All @@ -28,7 +28,7 @@ describe("BoroughDropdown", () => {
const firstBoroughId = boroughs[0].id;
render(
<BoroughDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
selectValue={firstBoroughId}
boroughs={boroughs}
/>,
Expand All @@ -37,6 +37,8 @@ describe("BoroughDropdown", () => {
await act(() => userEvent.selectOptions(screen.getByRole("combobox"), ""));
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "cd",
boroughId: null,
districtId: null,
});
});

Expand All @@ -45,7 +47,7 @@ describe("BoroughDropdown", () => {
const firstBoroughId = boroughs[0].id;
render(
<BoroughDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
boroughs={boroughs}
/>,
);
Expand All @@ -56,6 +58,7 @@ describe("BoroughDropdown", () => {
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "cd",
boroughId: firstBoroughId,
districtId: null,
});
});
});
22 changes: 8 additions & 14 deletions app/components/AdminDropdown/BoroughDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,24 @@
import { Borough } from "~/gen";
import { AdminDropdownProps, AdminDropdown } from ".";
import { BoroughId } from "~/root";
import { AdminParams, BoroughId } from "~/utils/types";

export interface BoroughDropdownProps
extends Pick<AdminDropdownProps, "selectValue"> {
updateSearchParams: (value: Record<string, string>) => void;
boroughs: Array<Borough> | null;
setAdminParams: (value: AdminParams) => void;
}

export function BoroughDropdown({
selectValue,
updateSearchParams,
boroughs,
setAdminParams,
}: BoroughDropdownProps) {
const updateBoroughId = (nextBoroughId: BoroughId) => {
const nextSearchParams: Record<string, string> =
nextBoroughId !== null
? {
districtType: "cd",
boroughId: nextBoroughId,
}
: {
districtType: "cd",
};

updateSearchParams(nextSearchParams);
setAdminParams({
districtType: "cd",
boroughId: nextBoroughId,
districtId: null,
});
};

const boroughOptions = boroughs?.map((borough) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe("CityCouncilDistrictDropdown", () => {
const updateSearchParams = vi.fn();
render(
<CityCouncilDistrictDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
cityCouncilDistricts={cityCouncilDistricts}
/>,
);
Expand All @@ -31,7 +31,7 @@ describe("CityCouncilDistrictDropdown", () => {
const firstCityCouncilDistrictId = cityCouncilDistricts[0].id;
render(
<CityCouncilDistrictDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
cityCouncilDistricts={cityCouncilDistricts}
selectValue={firstCityCouncilDistrictId}
/>,
Expand All @@ -40,6 +40,8 @@ describe("CityCouncilDistrictDropdown", () => {
await act(() => userEvent.selectOptions(screen.getByRole("combobox"), ""));
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "ccd",
boroughId: null,
districtId: null,
});
});

Expand All @@ -48,7 +50,7 @@ describe("CityCouncilDistrictDropdown", () => {
const firstCityCouncilDistrictId = cityCouncilDistricts[0].id;
render(
<CityCouncilDistrictDropdown
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
cityCouncilDistricts={cityCouncilDistricts}
/>,
);
Expand All @@ -61,6 +63,7 @@ describe("CityCouncilDistrictDropdown", () => {
);
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "ccd",
boroughId: null,
districtId: firstCityCouncilDistrictId,
});
});
Expand Down
26 changes: 8 additions & 18 deletions app/components/AdminDropdown/CityCouncilDistrictDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,23 @@
import { CityCouncilDistrict } from "~/gen";
import { AdminDropdownProps, AdminDropdown } from ".";
import { DistrictId } from "~/root";
import { AdminParams, DistrictId } from "~/utils/types";

export interface CityCouncilDistrictDropdownProps
extends Pick<AdminDropdownProps, "selectValue"> {
updateSearchParams: (value: Record<string, string>) => void;
cityCouncilDistricts: Array<CityCouncilDistrict> | null;
setAdminParams: (value: AdminParams) => void;
}
export function CityCouncilDistrictDropdown({
selectValue,
updateSearchParams,
cityCouncilDistricts,
setAdminParams,
}: CityCouncilDistrictDropdownProps) {
const updateDistrictId = (nextDistrictId: DistrictId) => {
const districtType = "ccd";

if (nextDistrictId === null) {
updateSearchParams({
districtType,
});
return;
}
if (nextDistrictId !== null) {
updateSearchParams({
districtType,
districtId: nextDistrictId,
});
return;
}
setAdminParams({
districtType: "ccd",
boroughId: null,
districtId: nextDistrictId,
});
};

const cityCouncilDistrictOptions = cityCouncilDistricts?.map((cd) => (
Expand Down
15 changes: 10 additions & 5 deletions app/components/AdminDropdown/CommunityDistrictDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ describe("CommunityDistrictDropdown", () => {
render(
<CommunityDistrictDropdown
boroughId={boroughId}
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
communityDistricts={communityDistricts}
/>,
);
Expand All @@ -36,14 +36,18 @@ describe("CommunityDistrictDropdown", () => {
render(
<CommunityDistrictDropdown
boroughId={null}
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
selectValue={firstCommunityDistrictId}
communityDistricts={communityDistricts}
/>,
);

await act(() => userEvent.selectOptions(screen.getByRole("combobox"), ""));
expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "cd" });
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "cd",
boroughId: null,
districtId: null,
});
});

it("should set search params when nextDistrictID is empty", async () => {
Expand All @@ -53,7 +57,7 @@ describe("CommunityDistrictDropdown", () => {
render(
<CommunityDistrictDropdown
boroughId={boroughId}
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
selectValue={firstCommunityDistrictId}
communityDistricts={communityDistricts}
/>,
Expand All @@ -63,6 +67,7 @@ describe("CommunityDistrictDropdown", () => {
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "cd",
boroughId,
districtId: null,
});
});

Expand All @@ -72,7 +77,7 @@ describe("CommunityDistrictDropdown", () => {
render(
<CommunityDistrictDropdown
boroughId={boroughId}
updateSearchParams={updateSearchParams}
setAdminParams={updateSearchParams}
selectValue={null}
communityDistricts={communityDistricts}
/>,
Expand Down
37 changes: 10 additions & 27 deletions app/components/AdminDropdown/CommunityDistrictDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
import { CommunityDistrict } from "~/gen";
import { AdminDropdown, AdminDropdownProps } from ".";
import { BoroughId, DistrictId } from "~/root";

import { AdminParams, BoroughId, DistrictId } from "~/utils/types";
export interface CommunityDistrictDropdownProps
extends Pick<AdminDropdownProps, "selectValue"> {
boroughId: BoroughId;
updateSearchParams: (value: Record<string, string>) => void;
communityDistricts: Array<CommunityDistrict> | null;
setAdminParams: (value: AdminParams) => void;
}

export function CommunityDistrictDropdown({
selectValue,
updateSearchParams,
boroughId,
communityDistricts,
setAdminParams,
}: CommunityDistrictDropdownProps) {
const updateDistrictId = (nextDistrictId: DistrictId) => {
const districtType = "cd";
if (boroughId === null) {
updateSearchParams({
districtType,
});
return;
}

if (boroughId !== null && nextDistrictId === null) {
updateSearchParams({
districtType,
boroughId,
});
return;
}
if (boroughId !== null && nextDistrictId !== null) {
updateSearchParams({
districtType,
boroughId,
districtId: nextDistrictId,
});
return;
}
setAdminParams({
districtType: "cd",
boroughId: boroughId,
districtId: nextDistrictId,
});
};

const communityDistrictOptions = communityDistricts?.map((cd) => (
<option key={cd.id} value={cd.id}>
{cd.id}
Expand Down
10 changes: 7 additions & 3 deletions app/components/AdminDropdown/DistrictTypeDropdown.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@ import { act } from "react";
describe("DistrictTypeDropdown", () => {
it("should render district type form details and options", () => {
const updateSearchParams = vi.fn();
render(<DistrictTypeDropdown updateSearchParams={updateSearchParams} />);
render(<DistrictTypeDropdown setAdminParams={updateSearchParams} />);
expect(screen.getByLabelText("District Type")).toBeInTheDocument();
expect(screen.getByText("Community District")).toBeInTheDocument();
expect(screen.getByText("City Council District")).toBeInTheDocument();
});

it("should update search params when changing the district type", async () => {
const updateSearchParams = vi.fn();
render(<DistrictTypeDropdown updateSearchParams={updateSearchParams} />);
render(<DistrictTypeDropdown setAdminParams={updateSearchParams} />);
await act(() =>
userEvent.selectOptions(screen.getByRole("combobox"), "cd"),
);
expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "cd" });
expect(updateSearchParams).toHaveBeenCalledWith({
districtType: "cd",
boroughId: null,
districtId: null,
});
});
});
20 changes: 15 additions & 5 deletions app/components/AdminDropdown/DistrictTypeDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,34 @@
import { AdminParams } from "~/utils/types";
import { AdminDropdownProps, AdminDropdown } from ".";
import { analytics } from "../../utils/analytics";

export interface DistrictTypeDropdownProps
extends Pick<AdminDropdownProps, "selectValue"> {
updateSearchParams: (value: Record<string, string>) => void;
setAdminParams: (value: AdminParams) => void;
}

export function DistrictTypeDropdown({
selectValue,
updateSearchParams,
setAdminParams,
}: DistrictTypeDropdownProps) {
const updateDistrictType = (nextDistrictType: string | null) => {
const nextSearchParams: Record<string, string> =
nextDistrictType === null ? {} : { districtType: nextDistrictType };
if (
nextDistrictType !== "cd" &&
nextDistrictType !== "ccd" &&
nextDistrictType !== null
)
throw new Error("invalid district type selected");

analytics({
category: "Dropdown Menu",
action: "Change District Type",
name: nextDistrictType,
});
updateSearchParams(nextSearchParams);
setAdminParams({
districtType: nextDistrictType,
boroughId: null,
districtId: null,
});
};

return (
Expand Down
10 changes: 5 additions & 5 deletions app/components/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ChevronLeftIcon, ChevronRightIcon } from "@chakra-ui/icons";
import { Box, HStack } from "@nycplanning/streetscape";
import { Link, useSearchParams } from "@remix-run/react";
import { setNewSearchParamsString } from "~/utils/utils";
import { setNewSearchParams } from "~/utils/utils";

export interface PaginationProps {
total: number;
Expand All @@ -19,9 +19,9 @@ export const Pagination = ({ total }: PaginationProps) => {
<HStack gap={2}>
<Link
to={{
search: setNewSearchParamsString(searchParams, {
search: setNewSearchParams(searchParams, {
page: page - 1,
}),
}).toString(),
}}
>
<Box
Expand Down Expand Up @@ -49,9 +49,9 @@ export const Pagination = ({ total }: PaginationProps) => {
</Box>
<Link
to={{
search: setNewSearchParamsString(searchParams, {
search: setNewSearchParams(searchParams, {
page: page + 1,
}),
}).toString(),
}}
>
<Box
Expand Down
Loading

0 comments on commit 5c2e626

Please sign in to comment.