From 5c2e62648269119b1d648a5080d89ddf219fad11 Mon Sep 17 00:00:00 2001 From: pratishta Date: Wed, 11 Sep 2024 11:03:53 -0400 Subject: [PATCH] Generalize query params with new type and existing change param func 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 --- .../AdminDropdown/BoroughDropdown.test.tsx | 9 ++-- .../AdminDropdown/BoroughDropdown.tsx | 22 ++++------ .../CityCouncilDistrictDropdown.test.tsx | 9 ++-- .../CityCouncilDistrictDropdown.tsx | 26 ++++------- .../CommunityDistrictDropdown.test.tsx | 15 ++++--- .../CommunityDistrictDropdown.tsx | 37 +++++----------- .../DistrictTypeDropdown.test.tsx | 10 +++-- .../AdminDropdown/DistrictTypeDropdown.tsx | 20 ++++++--- app/components/Pagination.tsx | 10 ++--- app/root.tsx | 44 ++++++++++++------- app/utils/types.ts | 14 ++++++ app/utils/utils.ts | 10 +++-- 12 files changed, 122 insertions(+), 104 deletions(-) create mode 100644 app/utils/types.ts diff --git a/app/components/AdminDropdown/BoroughDropdown.test.tsx b/app/components/AdminDropdown/BoroughDropdown.test.tsx index 1695b0e..70d6d6b 100644 --- a/app/components/AdminDropdown/BoroughDropdown.test.tsx +++ b/app/components/AdminDropdown/BoroughDropdown.test.tsx @@ -14,7 +14,7 @@ describe("BoroughDropdown", () => { const updateSearchParams = vi.fn(); render( , ); @@ -28,7 +28,7 @@ describe("BoroughDropdown", () => { const firstBoroughId = boroughs[0].id; render( , @@ -37,6 +37,8 @@ describe("BoroughDropdown", () => { await act(() => userEvent.selectOptions(screen.getByRole("combobox"), "")); expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "cd", + boroughId: null, + districtId: null, }); }); @@ -45,7 +47,7 @@ describe("BoroughDropdown", () => { const firstBoroughId = boroughs[0].id; render( , ); @@ -56,6 +58,7 @@ describe("BoroughDropdown", () => { expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "cd", boroughId: firstBoroughId, + districtId: null, }); }); }); diff --git a/app/components/AdminDropdown/BoroughDropdown.tsx b/app/components/AdminDropdown/BoroughDropdown.tsx index a2a1d23..3e08526 100644 --- a/app/components/AdminDropdown/BoroughDropdown.tsx +++ b/app/components/AdminDropdown/BoroughDropdown.tsx @@ -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 { - updateSearchParams: (value: Record) => void; boroughs: Array | null; + setAdminParams: (value: AdminParams) => void; } export function BoroughDropdown({ selectValue, - updateSearchParams, boroughs, + setAdminParams, }: BoroughDropdownProps) { const updateBoroughId = (nextBoroughId: BoroughId) => { - const nextSearchParams: Record = - nextBoroughId !== null - ? { - districtType: "cd", - boroughId: nextBoroughId, - } - : { - districtType: "cd", - }; - - updateSearchParams(nextSearchParams); + setAdminParams({ + districtType: "cd", + boroughId: nextBoroughId, + districtId: null, + }); }; const boroughOptions = boroughs?.map((borough) => ( diff --git a/app/components/AdminDropdown/CityCouncilDistrictDropdown.test.tsx b/app/components/AdminDropdown/CityCouncilDistrictDropdown.test.tsx index 386cea2..5730324 100644 --- a/app/components/AdminDropdown/CityCouncilDistrictDropdown.test.tsx +++ b/app/components/AdminDropdown/CityCouncilDistrictDropdown.test.tsx @@ -17,7 +17,7 @@ describe("CityCouncilDistrictDropdown", () => { const updateSearchParams = vi.fn(); render( , ); @@ -31,7 +31,7 @@ describe("CityCouncilDistrictDropdown", () => { const firstCityCouncilDistrictId = cityCouncilDistricts[0].id; render( , @@ -40,6 +40,8 @@ describe("CityCouncilDistrictDropdown", () => { await act(() => userEvent.selectOptions(screen.getByRole("combobox"), "")); expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "ccd", + boroughId: null, + districtId: null, }); }); @@ -48,7 +50,7 @@ describe("CityCouncilDistrictDropdown", () => { const firstCityCouncilDistrictId = cityCouncilDistricts[0].id; render( , ); @@ -61,6 +63,7 @@ describe("CityCouncilDistrictDropdown", () => { ); expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "ccd", + boroughId: null, districtId: firstCityCouncilDistrictId, }); }); diff --git a/app/components/AdminDropdown/CityCouncilDistrictDropdown.tsx b/app/components/AdminDropdown/CityCouncilDistrictDropdown.tsx index 251c00e..6638457 100644 --- a/app/components/AdminDropdown/CityCouncilDistrictDropdown.tsx +++ b/app/components/AdminDropdown/CityCouncilDistrictDropdown.tsx @@ -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 { - updateSearchParams: (value: Record) => void; cityCouncilDistricts: Array | 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) => ( diff --git a/app/components/AdminDropdown/CommunityDistrictDropdown.test.tsx b/app/components/AdminDropdown/CommunityDistrictDropdown.test.tsx index 6bee34c..2e23fdf 100644 --- a/app/components/AdminDropdown/CommunityDistrictDropdown.test.tsx +++ b/app/components/AdminDropdown/CommunityDistrictDropdown.test.tsx @@ -20,7 +20,7 @@ describe("CommunityDistrictDropdown", () => { render( , ); @@ -36,14 +36,18 @@ describe("CommunityDistrictDropdown", () => { render( , ); 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 () => { @@ -53,7 +57,7 @@ describe("CommunityDistrictDropdown", () => { render( , @@ -63,6 +67,7 @@ describe("CommunityDistrictDropdown", () => { expect(updateSearchParams).toHaveBeenCalledWith({ districtType: "cd", boroughId, + districtId: null, }); }); @@ -72,7 +77,7 @@ describe("CommunityDistrictDropdown", () => { render( , diff --git a/app/components/AdminDropdown/CommunityDistrictDropdown.tsx b/app/components/AdminDropdown/CommunityDistrictDropdown.tsx index 601f857..998469c 100644 --- a/app/components/AdminDropdown/CommunityDistrictDropdown.tsx +++ b/app/components/AdminDropdown/CommunityDistrictDropdown.tsx @@ -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 { boroughId: BoroughId; - updateSearchParams: (value: Record) => void; communityDistricts: Array | 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) => (