From 45425fe3b9327f761ef41a1022448903e5d51f3d Mon Sep 17 00:00:00 2001 From: krzysztof ziecina Date: Fri, 11 Aug 2023 10:37:51 +0200 Subject: [PATCH] fix: format currency before filter form submit --- sites/public/src/lib/helpers.tsx | 3 +++ sites/public/src/pages/finder.tsx | 11 +++++++++++ sites/public/src/pages/listings.tsx | 10 +++++++++- sites/public/src/pages/listings/filtered.tsx | 12 +++++++++--- 4 files changed, 32 insertions(+), 4 deletions(-) diff --git a/sites/public/src/lib/helpers.tsx b/sites/public/src/lib/helpers.tsx index 6f1860dd19..016d378b5f 100644 --- a/sites/public/src/lib/helpers.tsx +++ b/sites/public/src/lib/helpers.tsx @@ -45,6 +45,9 @@ import DetroitIcon from "../components/core/DetroitIcon" export const emailRegex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/ +export const removeCommas = (data: string | number) => + typeof data === "string" ? data.replaceAll(",", "") : data + export const getGenericAddress = (bloomAddress: Address) => { return bloomAddress ? { diff --git a/sites/public/src/pages/finder.tsx b/sites/public/src/pages/finder.tsx index ca74e014f9..c9990c9cd0 100644 --- a/sites/public/src/pages/finder.tsx +++ b/sites/public/src/pages/finder.tsx @@ -24,6 +24,7 @@ import Layout from "../layouts/application" import FinderDisclaimer from "../components/finder/FinderDisclaimer" import FinderMultiselect from "../components/finder/FinderMultiselect" import FinderRentalCosts from "../components/finder/FinderRentalCosts" +import { removeCommas } from "../lib/helpers" interface FinderField { label: string @@ -128,6 +129,16 @@ const Finder = () => { }) } }) + if (formSelections[FrontendListingFilterStateKeys.minRent]) { + formSelections[FrontendListingFilterStateKeys.minRent] = removeCommas( + formSelections[FrontendListingFilterStateKeys.minRent] + ) + } + if (formSelections[FrontendListingFilterStateKeys.maxRent]) { + formSelections[FrontendListingFilterStateKeys.maxRent] = removeCommas( + formSelections[FrontendListingFilterStateKeys.maxRent] + ) + } void router.push( `/listings/filtered?page=${1}&limit=${8}${encodeToFrontendFilterString(formSelections)}` ) diff --git a/sites/public/src/pages/listings.tsx b/sites/public/src/pages/listings.tsx index 021973ad64..ee73c1a9eb 100644 --- a/sites/public/src/pages/listings.tsx +++ b/sites/public/src/pages/listings.tsx @@ -12,7 +12,7 @@ import Layout from "../layouts/application" import { MetaTags } from "../components/shared/MetaTags" import { useRouter } from "next/router" import FilterForm from "../components/filters/FilterForm" -import { getListings } from "../lib/helpers" +import { getListings, removeCommas } from "../lib/helpers" import { fetchBaseListingData } from "../lib/hooks" import { ListingList, @@ -20,6 +20,7 @@ import { encodeToFrontendFilterString, ListingFilterState, AuthContext, + FrontendListingFilterStateKeys, } from "@bloom-housing/shared-helpers" import { ListingPagination } from "../components/listing/ListingPagination" import { UserStatus } from "../lib/constants" @@ -35,6 +36,13 @@ const ListingsPage = ({ initialListings }) => { const metaImage = "" // TODO: replace with hero image const onSubmit = (page: number, limit: number, data: ListingFilterState) => { + data[FrontendListingFilterStateKeys.minRent] = removeCommas( + data[FrontendListingFilterStateKeys.minRent] + ) + data[FrontendListingFilterStateKeys.maxRent] = removeCommas( + data[FrontendListingFilterStateKeys.maxRent] + ) + setFilterModalVisible(false) void router.push( `/listings/filtered?page=${page}&limit=${limit}${encodeToFrontendFilterString(data)}` diff --git a/sites/public/src/pages/listings/filtered.tsx b/sites/public/src/pages/listings/filtered.tsx index 52b5d5e602..e09a7a78c4 100644 --- a/sites/public/src/pages/listings/filtered.tsx +++ b/sites/public/src/pages/listings/filtered.tsx @@ -14,10 +14,10 @@ import { import { faSliders } from "@fortawesome/free-solid-svg-icons" import DetroitIcon from "../../components/core/DetroitIcon" import { - encodeToFrontendFilterString, decodeFiltersFromFrontendUrl, - ListingFilterState, + encodeToFrontendFilterString, FrontendListingFilterStateKeys, + ListingFilterState, } from "@bloom-housing/shared-helpers" import Layout from "../../layouts/application" import { MetaTags } from "../../components/shared/MetaTags" @@ -27,7 +27,7 @@ import { useListingsData } from "../../lib/hooks" import { EnumListingFilterParamsStatus, OrderByFieldsEnum } from "@bloom-housing/backend-core/types" import FilterForm from "../../components/filters/FilterForm" import ListingPagination from "../../components/listing/ListingPagination" -import { getListings } from "../../lib/helpers" +import { getListings, removeCommas } from "../../lib/helpers" const FilteredListingsPage = () => { const router = useRouter() @@ -90,6 +90,12 @@ const FilteredListingsPage = () => { const onSubmit = (page: number, limit: number, data: ListingFilterState) => { // hide status filter delete data[FrontendListingFilterStateKeys.status] + data[FrontendListingFilterStateKeys.minRent] = removeCommas( + data[FrontendListingFilterStateKeys.minRent] + ) + data[FrontendListingFilterStateKeys.maxRent] = removeCommas( + data[FrontendListingFilterStateKeys.maxRent] + ) setFilterModalVisible(false) setQueryString(page, limit, data) }