Optimistic UI profile modal #14
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Steps
What is happening?
The source of truth for the modal's open/closed state is our Page object, which we get from our server. We like this because it allows React to just read the data that comes from the server. When we click the user's Avatar, our React app will make an Inertia request to pull down the user's profile. Our
UserProfileShareableconcern will return both the profile data and the user id that was requested. When React sees that the user id exists, it opens the modal.The downside to this is that modal won't open until the network request completes. So if the request is slow, there will be a pause before the user see the modal appear. You can verify this by removing the click handler in
Avatar.tsx. So, what is that click handler doing? It's making a client side visit. This optimistically updates our modal's state to "open". We do the reverse in theonBeforeevent callback inUserProfileModal.tsx, optimistically closing the modal. The expected back button behavior works because we are usingrouter.push.Also notice that the visit to open the modal adds a query parameter to the URL. When we hard refresh, it's still open! Using this pattern, we can create a shareable URL for these types of UI workflows. If we don't want or need a shareable URL, we could add the
preserveUrlattribute to the<Link />component in<Avatar />.An exercise left for the reader: what happens if you try to visit a non-existent user's profile? Something like
?user_profile_id=300. How might you address what happens?