Merged in feat/sw-2857-refactor-booking-flow-url-updates (pull request #2302)
feat(SW-2857): Refactor booking flow url updates * Add support for removing parameters when using initial values in serializeSearchParams * Don't manually write search params in rate store * Booking is already from live search params so no need * Fix input type in serializeBookingSearchParams Approved-by: Linus Flood
This commit is contained in:
@@ -150,7 +150,11 @@ type SerializeOptions = {
|
||||
* @param obj - The object to serialize
|
||||
* @param options.keyRenameMap - Optional mapping of keys to rename, ie { "oldKey": "newKey" }
|
||||
* @param options.typeHints - Optional type hints to force certain keys to be treated as comma separated arrays
|
||||
* @param options.initialSearchParams - Optional initial URL search parameters to merge with the serialized object
|
||||
* @returns URLSearchParams - The serialized URL search parameters
|
||||
*
|
||||
* To force a key to be removed when merging with initialSearchParams, set its value to `null` in the object.
|
||||
* Arrays are not merged, they will always replace existing values.
|
||||
*/
|
||||
export function serializeSearchParams(
|
||||
obj: Record<string, any>,
|
||||
@@ -173,23 +177,31 @@ export function serializeSearchParams(
|
||||
const value = obj[key]
|
||||
|
||||
const renamedKey = keyRenameMap[key] || key
|
||||
const paramKey = prefix ? `${prefix}.${renamedKey}` : renamedKey
|
||||
|
||||
if (value === null) {
|
||||
params.delete(paramKey)
|
||||
continue
|
||||
}
|
||||
|
||||
if (Array.isArray(value)) {
|
||||
if (typeHints[key] === "COMMA_SEPARATED_ARRAY") {
|
||||
const paramKey = prefix ? `${prefix}.${renamedKey}` : renamedKey
|
||||
params.set(paramKey, value.join(","))
|
||||
continue
|
||||
}
|
||||
|
||||
// If an array value already exists (from initialSearchParams),
|
||||
// we need to first remove it since it can't be merged.
|
||||
deleteAllKeysStartingWith(params, renamedKey)
|
||||
value.forEach((item, index) => {
|
||||
const indexedKey = `${renamedKey}[${index}]`
|
||||
const paramKey = prefix ? `${prefix}.${indexedKey}` : indexedKey
|
||||
buildParams(item, paramKey)
|
||||
const arrayKey = prefix ? `${prefix}.${indexedKey}` : indexedKey
|
||||
|
||||
buildParams(item, arrayKey)
|
||||
})
|
||||
continue
|
||||
}
|
||||
|
||||
const paramKey = prefix ? `${prefix}.${renamedKey}` : renamedKey
|
||||
if (typeof value === "object" && value !== null) {
|
||||
buildParams(value, paramKey)
|
||||
continue
|
||||
@@ -207,3 +219,12 @@ export function serializeSearchParams(
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null
|
||||
}
|
||||
|
||||
function deleteAllKeysStartingWith(searchParams: URLSearchParams, key: string) {
|
||||
const keysToDelete = Array.from(searchParams.keys()).filter(
|
||||
(k) => k.startsWith(key) || k === key
|
||||
)
|
||||
for (const k of keysToDelete) {
|
||||
searchParams.delete(k)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user