Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: wrong previousRoute after back journey #57

Merged
merged 5 commits into from
Dec 5, 2023

Conversation

bchelkowski
Copy link
Member

What did you implement:

Closes #56

How did you implement it:

Save the latest item from kept history items instead of the current activeRoute.
It will also do that if the activeRoute should be skipped in the history.

How can we verify it:

The previousRoute should have the correct value always.

Todos:

  • Write documentation (if required)
  • Fix linting errors
  • Enable "Allow edits from maintainers" for this PR
  • Update the messages below

Is this ready for review?: YES
Is it a breaking change?: NO

@bchelkowski bchelkowski requested a review from a team as a code owner November 30, 2023 12:52
@bchelkowski bchelkowski requested review from adamczopek and shirishapitta and removed request for a team November 30, 2023 12:52
Copy link
Contributor

@RadoslawZambrowski RadoslawZambrowski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and quick job @bchelkowski, well done 👏 thank you 🙇

Left some suggestions.


if (isBackJourney OR m.top.activatedRoute.shouldSkip)
previousRoute = CreateObject("roSGNode", "ActivatedRoute")
previousRouteData = m._history[m._history.count() - 1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
previousRouteData = m._history[m._history.count() - 1]
previousRouteData = m._history.peek()

Comment on lines 88 to 92
sub _deleteKeys(assocArray as Object, keys as Object)
for each key in keys
assocArray.delete(key)
end for
end sub
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that looks like a utility function that could be added to ObjectUtils, something like omit from lodash

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but such a function does not exist yet, so I left a TODO comment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have ObjectUtils().pick()

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want to use the pick, as it will require you to modify it each time you would like to add something.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the opposite of ObjectUtils().pick() here 😅

Comment on lines 82 to 85
historyItem = route.getFields()
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"])

return historyItem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

having the omit function added to the ObjectUtils we could simply do here as per below

Suggested change
historyItem = route.getFields()
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"])
return historyItem
return ObjectUtils().omit(route.getFields(), ["change", "focusable", "focusedChild", "id"])

rootRoute = CreateObject("roSGNode", "ActivatedRoute")
rootRoute.setFields({ path: rootPath })

m._history.push(rootRoute.getFields())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_createHistoryItem seems to be missing

Suggested change
m._history.push(rootRoute.getFields())
m._history.push(_createHistoryItem(rootRoute.getFields()))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not missing, but yes, it would be better to use _createHistoryItem.

Suggested change
m._history.push(rootRoute.getFields())
m._history.push(_createHistoryItem (rootRoute))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

by missing I meant to keep the consistent interface of the objects stored in the history 😊

Comment on lines 51 to 52
rootRoute = CreateObject("roSGNode", "ActivatedRoute")
rootRoute.setFields({ path: rootPath })
Copy link
Contributor

@RadoslawZambrowski RadoslawZambrowski Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could add helper function like _createActivatedRoute(data) and use here and in _getPreviousRoute

function _getPreviousRoute(data as Object) as Object
isBackJourney = getProperty(data, "isBackJourney", false)

if (isBackJourney OR m.top.activatedRoute.shouldSkip)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we really need to apply this logic when m.top.activatedRoute.shouldSkip is true. This is a different flag than skipInHistory and it is not set on back journey, but could be set by the middleware. Meaning that it is possible to skip routes only in the non-back journey (skipped routes are not in the history), and in that case previousRoute should stay as is.

E.g. we navigate from route A to route B, middleware updates route B to be skipped and redirects to route C, then we should not set route B as the previousRoute and route A should be still there. If we apply the below logic, then we would update the previousRoute with the route A, but it seems to be redundant as it is already there.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is basically what I mean we could do:

Suggested change
if (isBackJourney OR m.top.activatedRoute.shouldSkip)
if (m.top.activatedRoute.shouldSkip)
return m.top.previousRoute
end if
if (isBackJourney)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

now both solutions point to the same object, so I would just skip that additional check

Copy link
Contributor

@pawelhertman pawelhertman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌


m.top.activatedRoute.path = data.path
m.top.activatedRoute.params = ternary(data.params <> Invalid, data.params, {})
' Needs to be set before activatedRoute as setPreviousRoute uses the previous value of activatedRoute.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
' Needs to be set before activatedRoute as setPreviousRoute uses the previous value of activatedRoute.
' Needs to be set before activatedRoute as getPreviousRoute uses the previous value of activatedRoute.

BTW this comment makes sense only if you put an empty line after setting m.top.previousRoute

sub _updateHistory()
if (m.top.url = "" OR m.top.activatedRoute.shouldSkip)
return
function _getPreviousRoute(data as Object) as Object
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It lacks context on what kind of data it is. You could pass here only isBackJourney, but it's still a bit strange why anything about the back journey should have an impact on the previous route. That's why I think the if (isBackJourney OR m.top.activatedRoute.shouldSkip) check should be part of the navigate method and this one should only return indeed the previous route

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to navigateData.
I don't agree, the function _getPreviousRoute is responsible for returning the previous route, so it shouldn't be shocking that it has all checks for that purpose.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I could argue that during navigation the current route is not the previous route until we switch it to the new one 😅

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The output of the _getPreviousRoute is set to the m.top.previousRoute always.
So in my opinion whole logic should be there, even if previousRoute would be the current activatedRoute.

Comment on lines 62 to 67
previousRoute = CreateObject("roSGNode", "ActivatedRoute")
previousRouteData = m._history[m._history.count() - 1]

if (previousRouteData = Invalid) then return Invalid

previousRoute.setFields(previousRouteData)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
previousRoute = CreateObject("roSGNode", "ActivatedRoute")
previousRouteData = m._history[m._history.count() - 1]
if (previousRouteData = Invalid) then return Invalid
previousRoute.setFields(previousRouteData)
previousRouteData = m._history[m._history.count() - 1]
if (previousRouteData = Invalid) then return Invalid
previousRoute = CreateObject("roSGNode", "ActivatedRoute")
previousRoute.setFields(previousRouteData)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed that according to Radek's comment

Comment on lines 82 to 85
historyItem = route.getFields()
_deleteKeys(historyItem, ["change", "focusable", "focusedChild", "id"])

return historyItem
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interface change - for some purpose fields like renderedUrl or isBackJourney aren't stored (especially the renderedUrl makes no sense), so I think we shouldn't change this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe it is needed to recreate properly the ActivatedRoute instance from the history item

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How could we live without this then? 🤔 Anyway, makes sense 🙈

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an interface change, but non-breaking, as it added fields that didn't exist before and didn't change the existing ones.
And it would be better in my opinion to keep all data related to the route instead of selecting some of it, we should not decide what would be useful to the user.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we were not recreating the ActivatedRoute from the history item before, it was introduced by Błazej within this PR 😄

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we could just switch to storing the ActivatedRoute node? 🤔

Comment on lines 58 to 59
function _getPreviousRoute(navigationData as Object) as Object
isBackJourney = getProperty(navigationData, "isBackJourney", false)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about passing boolean parameter instead, ie isBackJourney? currently we use various naming for parameters across various functions, and it does not seem to be straightforward what are they. We have data, navigationData, and routeData 👀

Copy link
Contributor

@pawelhertman pawelhertman Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be even clearer?

if isBackJourney
    m.top.previousRoute = _getPreviousRoute()
else
   m.top.previousRoute = NodeUtils().cloneNode(m.top.activatedRoute)
end if

Anyway, no hard feelings, up to you @bchelkowski

Copy link
Contributor

@RadoslawZambrowski RadoslawZambrowski Nov 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and rename _getPreviousRoute to _getPreviousRouteFromTheHistory then

but we could still pass the boolean parameter to the _getPreviousRoute and apply proper logic inside as it is right now

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry, but I don't understand why I should introduce yet another if statement in the navigate function if I could move it to the _getPreviousRoute

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my idea was to extract the isBackJourney in the navigate function and pass it as a param, so the if stays in the _getPreviousRoute

in navigate:

m.top.previousRoute = _getPreviousRoute(getProperty(data, "isBackJourney", false))`

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So like it is now?

@bchelkowski bchelkowski force-pushed the fix--wrong-previousRoute-after-back-journey branch from b7d068e to 6b8e5d6 Compare December 5, 2023 09:00
@bchelkowski bchelkowski merged commit cdd6652 into master Dec 5, 2023
1 check passed
@bchelkowski bchelkowski deleted the fix--wrong-previousRoute-after-back-journey branch December 5, 2023 09:02
github-actions bot pushed a commit that referenced this pull request Dec 5, 2023
## [2.1.5](v2.1.4...v2.1.5) (2023-12-05)

### Bug Fixes

* wrong previousRoute after back journey ([#57](#57)) ([cdd6652](cdd6652))
Copy link

github-actions bot commented Dec 5, 2023

🎉 This PR is included in version 2.1.5 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Router's previousRoute field has wrong value after back journey
3 participants