aboutsummaryrefslogtreecommitdiff
path: root/packages/aml-backoffice-ui/src/route.ts
diff options
context:
space:
mode:
authorSebastian <sebasjm@gmail.com>2023-11-18 09:55:23 -0300
committerSebastian <sebasjm@gmail.com>2023-11-18 09:55:23 -0300
commit7ed3e78f790837479fc2bb2eb6ddc40c78ce59b5 (patch)
treed914627537315e15240034fed441239448dfff07 /packages/aml-backoffice-ui/src/route.ts
parentc797d551d9716924120d6ce6f270793c7bb5a4f9 (diff)
downloadwallet-core-7ed3e78f790837479fc2bb2eb6ddc40c78ce59b5.tar.xz
sync page with history
Diffstat (limited to 'packages/aml-backoffice-ui/src/route.ts')
-rw-r--r--packages/aml-backoffice-ui/src/route.ts77
1 files changed, 39 insertions, 38 deletions
diff --git a/packages/aml-backoffice-ui/src/route.ts b/packages/aml-backoffice-ui/src/route.ts
index 9176ab5e4..091b92d5c 100644
--- a/packages/aml-backoffice-ui/src/route.ts
+++ b/packages/aml-backoffice-ui/src/route.ts
@@ -79,48 +79,17 @@ type Location = {
path: string;
values: Record<string, string>;
};
-export function useCurrentLocation(pageList: Array<PageEntry<any>>) {
- const [currentLocation, setCurrentLocation] = useState<Location>();
- /**
- * Search path in the pageList
- * get the values from the path found
- * add params from searchParams
- *
- * @param path
- * @param params
- */
- function doSync(path: string, params: URLSearchParams) {
- let result: typeof currentLocation;
- for (let idx = 0; idx < pageList.length; idx++) {
- const page = pageList[idx];
- if (typeof page.url === "string") {
- if (page.url === path) {
- const values: Record<string, string> = {};
- params.forEach((v, k) => {
- values[k] = v;
- });
- result = { page, values, path };
- break;
- }
- } else {
- const values = doestUrlMatchToRoute(path, page.url.pattern);
- if (values !== undefined) {
- params.forEach((v, k) => {
- values[k] = v;
- });
- result = { page, values, path };
- break;
- }
- }
- }
- setCurrentLocation(result);
- }
+export function useCurrentLocation(pageList: Array<PageEntry<any>>): Location | undefined {
+ const [currentLocation, setCurrentLocation] = useState<Location | null | undefined>(null);
useEffect(() => {
- doSync(window.location.hash, new URLSearchParams(window.location.search));
return history.listen(() => {
- doSync(window.location.hash, new URLSearchParams(window.location.search));
+ const result = doSync(window.location.hash, new URLSearchParams(window.location.search), pageList);
+ setCurrentLocation(result);
});
}, []);
+ if (currentLocation === null) {
+ return doSync(window.location.hash, new URLSearchParams(window.location.search), pageList);
+ }
return currentLocation;
}
@@ -134,6 +103,38 @@ export function useChangeLocation() {
return location;
}
+/**
+ * Search path in the pageList
+ * get the values from the path found
+ * add params from searchParams
+ *
+ * @param path
+ * @param params
+ */
+export function doSync(path: string, params: URLSearchParams, pageList: Array<PageEntry<any>>): Location | undefined {
+ for (let idx = 0; idx < pageList.length; idx++) {
+ const page = pageList[idx];
+ if (typeof page.url === "string") {
+ if (page.url === path) {
+ const values: Record<string, string> = {};
+ params.forEach((v, k) => {
+ values[k] = v;
+ });
+ return { page, values, path };
+ }
+ } else {
+ const values = doestUrlMatchToRoute(path, page.url.pattern);
+ if (values !== undefined) {
+ params.forEach((v, k) => {
+ values[k] = v;
+ });
+ return { page, values, path };
+ }
+ }
+ }
+ return undefined;
+}
+
function doestUrlMatchToRoute(
url: string,
route: string,