From b8f85936e6671adf8248dc66d9f393c05312dc45 Mon Sep 17 00:00:00 2001 From: henlam Date: Sat, 1 Jul 2023 22:11:36 -0400 Subject: [PATCH] fix: keep tree_id parameters when bounds are updated --- src/components/App.js | 10 ++++++++-- src/models/pathResolver.js | 8 +++----- src/models/pathResolver.test.js | 28 ++++++++++++++++++++++++---- 3 files changed, 35 insertions(+), 11 deletions(-) diff --git a/src/components/App.js b/src/components/App.js index 929e06c2c..bd827b485 100644 --- a/src/components/App.js +++ b/src/components/App.js @@ -94,8 +94,8 @@ function MapComponent() { isRefreshNeeded = true; } else if ( router.query.tree_id && - result.tree_id && - router.query.tree_id !== result.tree_id + result.query.tree_id && + router.query.tree_id !== result.query.tree_id ) { log.warn('tree_id query is different!'); isRefreshNeeded = true; @@ -104,6 +104,12 @@ function MapComponent() { } if (isRefreshNeeded) { + if (result.query) { + log.warn('saving query in router'); + Object.keys(result.query).forEach((field) => { + router.query[field] = result.query[field]; + }); + } router.push(result); } } diff --git a/src/models/pathResolver.js b/src/models/pathResolver.js index 1f4dadfbe..edf557a54 100644 --- a/src/models/pathResolver.js +++ b/src/models/pathResolver.js @@ -56,7 +56,7 @@ function getPathWhenClickTree(tree, location, router, map, options = {}) { pathnameResult = `/trees/${tree.id}`; } else if (path[2] === 'wallets') { pathnameResult = `${path[1]}/tokens`; - optionalParams.tree_id = tree.id; + optionalParams.tree_id = tree.id.toString(); } else { pathnameResult = `${path[1] || ''}/${path[4] || 'trees'}/${ path[5] === 'tokens' ? path[4] : tree.id @@ -64,9 +64,10 @@ function getPathWhenClickTree(tree, location, router, map, options = {}) { } } else { const match2 = pathname.match(/^\/wallets\/([a-z0-9-]+)\/tokens$/); + log.warn('match pattern 2', match2); if (match2) { pathnameResult = `/wallets/${match2[1]}/tokens`; - optionalParams.tree_id = tree.id; + optionalParams.tree_id = tree.id.toString(); } else { pathnameResult = `/trees/${tree.id}`; } @@ -93,9 +94,6 @@ function updatePathWhenMapMoveEnd(location, map, router) { }${router.query.embed ? `&embed=true` : ''}`; if (router.query.tree_id) { result += `&tree_id=${router.query.tree_id}`; - } else if (router.query.walletid) { - const tree_id = router.asPath.match(/tree_id=(\d+)/)[1]; - result += `&tree_id=${tree_id}`; } return result; } diff --git a/src/models/pathResolver.test.js b/src/models/pathResolver.test.js index 3d745bcf1..af6a21fb8 100644 --- a/src/models/pathResolver.test.js +++ b/src/models/pathResolver.test.js @@ -82,7 +82,7 @@ describe('Test pathResolver', () => { }, { query: { - tree_id: 14615, + tree_id: '14615', }, }, ); @@ -90,7 +90,7 @@ describe('Test pathResolver', () => { expect(result).toMatchObject({ pathname: '/wallets/0cdf4219-869a-41ce-953a-a8421d8353f7/tokens', query: { - tree_id: 14616, + tree_id: '14616', }, }); }); @@ -111,7 +111,7 @@ describe('Test pathResolver', () => { expect(result).toMatchObject({ pathname: '/wallets/0cdf4219-869a-41ce-953a-a8421d8353f7/tokens', query: { - tree_id: 14615, + tree_id: '14615', }, }); }); @@ -171,7 +171,7 @@ describe('Test pathResolver', () => { }, { query: { - tree_id: 14615, + tree_id: '14615', }, }, ); @@ -179,6 +179,26 @@ describe('Test pathResolver', () => { '/wallets/0cdf4219-869a-41ce-953a-a8421d8353f7?bounds=37.44990348815919,-3.315482794386477,37.46535301208497,-3.307471024919109&tree_id=14615', ); }); + + it('update path when /wallets/5f5939ae-91ce-49cd-81ba-7fdba81e250a/tokens?tree_id=5413738', () => { + const result = pathResolver.updatePathWhenMapMoveEnd( + { + pathname: '/wallets/5f5939ae-91ce-49cd-81ba-7fdba81e250a/tokens', + }, + { + getCurrentBounds: () => + '46.38155221939087,-15.762146918354096,46.3908863067627,-15.74444839985392', + }, + { + query: { + tree_id: '5413738', + }, + }, + ); + expect(result).toBe( + '/wallets/5f5939ae-91ce-49cd-81ba-7fdba81e250a/tokens?bounds=46.38155221939087,-15.762146918354096,46.3908863067627,-15.74444839985392&tree_id=5413738', + ); + }); }); describe('getContext', () => {