Skip to content

Commit

Permalink
fix: keep tree_id parameters when bounds are updated
Browse files Browse the repository at this point in the history
  • Loading branch information
henlam1 committed Jul 2, 2023
1 parent 7552563 commit b8f8593
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
10 changes: 8 additions & 2 deletions src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
}
Expand Down
8 changes: 3 additions & 5 deletions src/models/pathResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,18 @@ 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
}`;
}
} 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}`;
}
Expand All @@ -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;
}
Expand Down
28 changes: 24 additions & 4 deletions src/models/pathResolver.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ describe('Test pathResolver', () => {
},
{
query: {
tree_id: 14615,
tree_id: '14615',
},
},
);

expect(result).toMatchObject({
pathname: '/wallets/0cdf4219-869a-41ce-953a-a8421d8353f7/tokens',
query: {
tree_id: 14616,
tree_id: '14616',
},
});
});
Expand All @@ -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',
},
});
});
Expand Down Expand Up @@ -171,14 +171,34 @@ describe('Test pathResolver', () => {
},
{
query: {
tree_id: 14615,
tree_id: '14615',
},
},
);
expect(result).toBe(
'/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', () => {
Expand Down

0 comments on commit b8f8593

Please sign in to comment.