From 9ad3b791ca6f1fd2ca4277e2fbf6f8dde547b47d Mon Sep 17 00:00:00 2001 From: Diego Cohen Date: Fri, 20 Dec 2024 09:40:46 -0500 Subject: [PATCH 1/3] Pass nypl source in camelcase to hold request submission form --- pages/hold/request/[id]/edd.tsx | 2 +- pages/hold/request/[id]/index.tsx | 2 +- src/models/Item.ts | 5 +++++ src/models/modelTests/Item.test.ts | 4 ++++ src/utils/appUtils.ts | 16 ++++++++++++++++ 5 files changed, 27 insertions(+), 2 deletions(-) diff --git a/pages/hold/request/[id]/edd.tsx b/pages/hold/request/[id]/edd.tsx index 5db829334..7c03d38ba 100644 --- a/pages/hold/request/[id]/edd.tsx +++ b/pages/hold/request/[id]/edd.tsx @@ -74,7 +74,7 @@ export default function EDDRequestPage({ const [eddFormState, setEddFormState] = useState({ ...initialEDDFormState, patronId, - source: item.source, + source: item.holdRequestSource, }) const [formPosting, setFormPosting] = useState(false) diff --git a/pages/hold/request/[id]/index.tsx b/pages/hold/request/[id]/index.tsx index c040feb8b..e959aabe7 100644 --- a/pages/hold/request/[id]/index.tsx +++ b/pages/hold/request/[id]/index.tsx @@ -180,7 +180,7 @@ export default function HoldRequestPage({ handleSubmit={handleSubmit} holdId={holdId} patronId={patronId} - source={item.source} + source={item.holdRequestSource} /> ) : null} diff --git a/src/models/Item.ts b/src/models/Item.ts index d3c8c8de7..ca01359c3 100644 --- a/src/models/Item.ts +++ b/src/models/Item.ts @@ -12,6 +12,7 @@ import { locationEndpointsMap, } from "../utils/itemUtils" import { appConfig } from "../config/config" +import { convertCamelToShishKabobCase } from "../utils/appUtils" /** * The Item class contains the data and getter functions @@ -81,6 +82,10 @@ export default class Item { .includes("all") } + get holdRequestSource(): string { + return convertCamelToShishKabobCase(this.source) + } + // Pre-processing logic for setting Item holding location getLocationFromItem(item: DiscoveryItemResult): ItemLocation { let location = defaultNYPLLocation diff --git a/src/models/modelTests/Item.test.ts b/src/models/modelTests/Item.test.ts index db080df42..4d050bae9 100644 --- a/src/models/modelTests/Item.test.ts +++ b/src/models/modelTests/Item.test.ts @@ -104,6 +104,10 @@ describe("Item model", () => { "A history of spaghetti eating and cooking for: spaghetti dinner." ) }) + + it("returns the source in kebabcase for use in hold requests", () => { + expect(item.holdRequestSource).toBe("sierra-nypl") + }) }) describe("ReCAP checks", () => { diff --git a/src/utils/appUtils.ts b/src/utils/appUtils.ts index 67a1f8dbb..09beee057 100644 --- a/src/utils/appUtils.ts +++ b/src/utils/appUtils.ts @@ -140,3 +140,19 @@ export const convertToSentenceCase = (str: string) => str.split(" ").length > 1 ? str.charAt(0).toUpperCase() + str.slice(1).toLowerCase() : str + +/** + * Converts camel case string to shish kabob case + * + * e.g. camelToShishKabobCase("RecapPul") + * => "recap-pul" + * camelToShishKabobCase("firstCharCanBeLowerCase") + * => "first-char-can-be-lower-case" + */ +export const convertCamelToShishKabobCase = (str: string) => + str + // Change capital letters into "-{lowercase letter}" + .replace(/([A-Z])/g, (c, p1, i) => { + // If capital letter is not first character, precede with '-': + return (i > 0 ? "-" : "") + c.toLowerCase() + }) From 2b29f5429612dc320f0367e791c29b3eb79d9b04 Mon Sep 17 00:00:00 2001 From: Diego Cohen Date: Fri, 20 Dec 2024 14:45:15 -0500 Subject: [PATCH 2/3] Rename holdRequestSource to formattedSourceForHoldRequest --- pages/hold/request/[id]/edd.tsx | 2 +- pages/hold/request/[id]/index.tsx | 2 +- src/models/Item.ts | 2 +- src/models/modelTests/Item.test.ts | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/pages/hold/request/[id]/edd.tsx b/pages/hold/request/[id]/edd.tsx index 7c03d38ba..334d59393 100644 --- a/pages/hold/request/[id]/edd.tsx +++ b/pages/hold/request/[id]/edd.tsx @@ -74,7 +74,7 @@ export default function EDDRequestPage({ const [eddFormState, setEddFormState] = useState({ ...initialEDDFormState, patronId, - source: item.holdRequestSource, + source: item.formattedSourceForHoldRequest, }) const [formPosting, setFormPosting] = useState(false) diff --git a/pages/hold/request/[id]/index.tsx b/pages/hold/request/[id]/index.tsx index e959aabe7..1313e5c2d 100644 --- a/pages/hold/request/[id]/index.tsx +++ b/pages/hold/request/[id]/index.tsx @@ -180,7 +180,7 @@ export default function HoldRequestPage({ handleSubmit={handleSubmit} holdId={holdId} patronId={patronId} - source={item.holdRequestSource} + source={item.formattedSourceForHoldRequest} /> ) : null} diff --git a/src/models/Item.ts b/src/models/Item.ts index ca01359c3..ae4e81a88 100644 --- a/src/models/Item.ts +++ b/src/models/Item.ts @@ -82,7 +82,7 @@ export default class Item { .includes("all") } - get holdRequestSource(): string { + get formattedSourceForHoldRequest(): string { return convertCamelToShishKabobCase(this.source) } diff --git a/src/models/modelTests/Item.test.ts b/src/models/modelTests/Item.test.ts index 4d050bae9..d07e92901 100644 --- a/src/models/modelTests/Item.test.ts +++ b/src/models/modelTests/Item.test.ts @@ -106,7 +106,7 @@ describe("Item model", () => { }) it("returns the source in kebabcase for use in hold requests", () => { - expect(item.holdRequestSource).toBe("sierra-nypl") + expect(item.formattedSourceForHoldRequest).toBe("sierra-nypl") }) }) From cd229ba1b8e57559337941fae37ef04da16ec407 Mon Sep 17 00:00:00 2001 From: Diego Cohen Date: Fri, 20 Dec 2024 14:48:20 -0500 Subject: [PATCH 3/3] Update var naming in convertCamelToShishKabobCase --- src/utils/appUtils.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/utils/appUtils.ts b/src/utils/appUtils.ts index 09beee057..41a4c9e28 100644 --- a/src/utils/appUtils.ts +++ b/src/utils/appUtils.ts @@ -152,7 +152,7 @@ export const convertToSentenceCase = (str: string) => export const convertCamelToShishKabobCase = (str: string) => str // Change capital letters into "-{lowercase letter}" - .replace(/([A-Z])/g, (c, p1, i) => { + .replace(/([A-Z])/g, (capitalLetter, placeholderVar, index) => { // If capital letter is not first character, precede with '-': - return (i > 0 ? "-" : "") + c.toLowerCase() + return (index > 0 ? "-" : "") + capitalLetter.toLowerCase() })