-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
191 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// From https://github.com/elastic/search-ui/blob/main/packages/search-ui/src/preserveTypesEncoder.ts | ||
// License: Apache-2.0 | ||
|
||
function isTypeNumber(value) { | ||
return value !== undefined && value !== null && typeof value === "number"; | ||
} | ||
|
||
function isTypeBoolean(value) { | ||
return value && typeof value === "boolean"; | ||
} | ||
|
||
function toBoolean(value) { | ||
if (value === "true") return true; | ||
if (value === "false") return false; | ||
throw "Invalid type parsed as Boolean value"; | ||
} | ||
|
||
/* Encoder for qs library which preserve number types on the URL. Numbers | ||
are padded with "n_{number}_n", and booleans with "b_{boolean}_b"*/ | ||
|
||
export default { | ||
encode(value, encode) { | ||
if (isTypeNumber(value)) { | ||
return `n_${value}_n`; | ||
} | ||
if (isTypeBoolean(value)) { | ||
return `b_${value}_b`; | ||
} | ||
return encode(value); | ||
}, | ||
decode(value, decode) { | ||
//eslint-disable-next-line | ||
if (/n_-?[\d\.]*_n/.test(value)) { | ||
const numericValueString = value.substring(2, value.length - 2); | ||
return Number(numericValueString); | ||
} | ||
if (/^b_(true|false)*_b$/.test(value)) { | ||
const booleanValueString = value.substring(2, value.length - 2); | ||
return toBoolean(booleanValueString); | ||
} | ||
return decode(value); | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { | ||
Filter, | ||
RequestState, | ||
SearchDriverOptions, | ||
SortOption, | ||
} from "@elastic/search-ui"; | ||
import { usePathname, useRouter } from "next/navigation"; | ||
var qs = require("qs"); | ||
import preserveTypesEncoder from "./preserveTypesEncoder"; | ||
|
||
export const useNextRouting = ( | ||
config: SearchDriverOptions, | ||
basePathUrl: string, | ||
) => { | ||
const router = useRouter(); | ||
const pathName = usePathname(); | ||
|
||
// From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L9 | ||
type QueryParams = { | ||
filters?: Filter[]; | ||
current?: number; | ||
q?: string; | ||
size?: number; | ||
"sort-field"?: string; | ||
"sort-direction"?: string; | ||
sort?: SortOption[]; | ||
}; | ||
|
||
// From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L84 | ||
function stateToParams({ | ||
searchTerm, | ||
current, | ||
filters, | ||
resultsPerPage, | ||
sortDirection, | ||
sortField, | ||
sortList, | ||
}: RequestState): QueryParams { | ||
const params: QueryParams = {}; | ||
if (current !== undefined && current > 1) params.current = current; | ||
if (searchTerm) params.q = searchTerm; | ||
if (resultsPerPage) params.size = resultsPerPage; | ||
if (filters && filters.length > 0) { | ||
params["filters"] = filters; | ||
} | ||
if (sortList && sortList.length > 0) { | ||
params["sort"] = sortList; | ||
} else if (sortField) { | ||
params["sort-field"] = sortField; | ||
params["sort-direction"] = sortDirection; | ||
} | ||
return params; | ||
} | ||
|
||
// From https://github.com/elastic/search-ui/blob/6583ad0c03056b3df0541d585337dfad12c16272/packages/search-ui/src/URLManager.ts#L109 | ||
function stateToQueryString(state: RequestState): string { | ||
return qs.stringify(stateToParams(state), { | ||
encoder: preserveTypesEncoder.encode, | ||
}); | ||
} | ||
|
||
const routingOptions = { | ||
stateToUrl: (state: RequestState) => { | ||
// if URL contains ?uid=... then we are in modal view, and hence we should not update the URL | ||
if (window.location.search.includes("uid=")) { | ||
console.log( | ||
"stateToUrl :: in modal view, not updating URL. URL: ", | ||
window.location.href, | ||
); | ||
return window.location.search.replace("?", ""); // get the query string without the ?, eg. uid=123 | ||
} else { | ||
console.log("stateToUrl :: updating URL with state: ", state); | ||
return stateToQueryString(state); | ||
} | ||
}, | ||
}; | ||
|
||
return { | ||
...config, | ||
routingOptions, | ||
}; | ||
}; |