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

Refactor createRedirectWithUsername #2987

Conversation

adityagarg06
Copy link
Contributor

@adityagarg06 adityagarg06 commented Feb 4, 2024

Progress on #2042

Changes:

  • Used useSelector instead of connect to handle the state access.
  • Removed the mapStateToProps function as no longer needed.
  • The createRedirectWithUsername function is removed as the URL is directly passed as a prop to RedirectToUser .
  • Replace createRedirectWithUsername with RedirectToUser in routing.
  • Added Proptypes.

I have verified that this pull request:

  • has no linting errors (npm run lint)
  • has no test errors (npm run test)
  • is from a uniquely-named feature branch and is up to date with the develop branch.
  • is descriptively named and links to an issue number, i.e. Fixes #123

@lindapaiste
Copy link
Collaborator

Love it! This is good stuff and my feedback is that we can take the cleanup even further.

  • Used useSelector instead of connect to handle the state access.
  • Removed the mapStateToProps function as no longer needed.

Good change 👍.
I would like to see us using more selector function vs defining selectors inline, as it adds a layer of separation between the component and the shape of the Redux state. I thought that we already had a selectCurrentUsername selector but I guess we don't. So maybe we should create one and use it here?

  • The createRedirectWithUsername function is removed as the URL is directly passed as a prop to RedirectToUser .
  • Replace createRedirectWithUsername with RedirectToUser in routing.

This syntax change (switching from an HOC to a component) is going to be really helpful if we ever upgrade to react-router v6.

  • Added Proptypes.

It definitely should have PropTypes and I don't know why we're not getting any errors for that right now. This will fix one of the issues that came up when updating our eslint configs (#2220). 🔧

Other suggestions:

  • Let's access the history variable from a react-router hook instead of using the global constant. const browserHistory = useHistory();
  • The existing if (username == null) { return; } logic would be more clean if we handled the if in the opposite direction. That is, if (username) { browserHistory.replace(url.replace(':username', username)); }. It won't do anything if there's no username, but we don't need to explicitly return.

Side note:

I starting digging into the code and got confused by where we initiate the redirect to the login page if the user is not logged in. If I go to https://editor.p5js.org/sketches when not logged in the redirect is working properly, but I don't see that logic anywhere in the routing.

It's actually happening on the server:

router.get('/sketches', (req, res) => {
if (req.user) {
res.send(renderIndex());
} else {
res.redirect('/login');
}
});

And now I'm starting to think about the bigger picture, and wondering if we should move the redirect to username part to the server too? Like after writing all that about how we can improve this component, I'm now wondering if we actually should delete the whole thing. Instead of the res.send(renderIndex()); we would have res.redirect(/${req.user.username}/sketches); and then a whole bunch of front-end code can be deleted.

@raclim
Copy link
Collaborator

raclim commented Jun 13, 2024

Thanks for your work on this! This ended up getting resolved by #3147. I'm really sorry that we couldn't get this in, but please feel free to check out our other issues!

@raclim raclim closed this Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants