This episode is for members only

Sign up to access "Build a Forum with Inertia and Laravel" right now.

Get started
Already a member? Sign in to continue
Playing
40. Automatically scrolling to posts

Episodes

0%
Your progress
  • Total: 6h 54m
  • Played: 0m
  • Remaining: 6h 54m
Join or sign in to track your progress
01. Introduction and demo
6m 59s
0%
02. Getting set up
10m 36s
0%
03. Modifying registration for usernames
7m 15s
0%
04. Figuring out the forum layout
5m 57s
0%
05. Creating and listing topics
9m 15s
0%
06. Basic discussion listing
13m 33s
0%
07. Pinning discussions
4m 1s
0%
08. Tackling pagination in Inertia
8m 23s
0%
09. Customising pagination text in Laravel
52s
0%
10. Showing a discussion
6m 4s
0%
11. Setting up discussion posts
5m 53s
0%
12. Listing through discussion posts
5m 28s
0%
13. Adding more data to posts
8m 24s
0%
14. Adding pagination to posts
1m 35s
0%
15. Adding a post preview to discussions
4m 52s
0%
16. Adding the last reply to discussions
5m 54s
0%
17. Outputting discussion participants
8m 4s
0%
18. Limiting participants in the UI
5m 56s
0%
19. Ordering discussions by last post
4m 30s
0%
20. Handling deleted users
2m 31s
0%
21. Counting replies
8m 13s
0%
22. Building our first filter
8m 31s
0%
23. Highlighting current filters, and merging with pagination
5m 37s
0%
24. Adding auth specific filters
6m 40s
0%
25. Adding the topic filter
8m 18s
0%
26. Scaffolding the new discussion form
13m 29s
0%
27. Toggling the create discussion form
9m 2s
0%
28. Keeping form state
4m 59s
0%
29. Storing a new discussion
11m 29s
0%
30. Discussion validation and authorization
5m 1s
0%
31. Generating markdown for posts
8m 37s
0%
32. Toggling the markdown preview
7m 43s
0%
33. Fetching and displaying markdown
8m 6s
0%
34. Adding a markdown shortcut toolbar
5m 53s
0%
35. Dealing with SVG icons
7m 46s
0%
36. Creating the reply form
7m 48s
0%
37. Basic Inertia permission checking
6m 26s
0%
38. Creating replies to discussions
5m 37s
0%
39. Jumping to posts
11m 40s
0%
40. Automatically scrolling to posts
6m 18s
0%
41. Toggling post editing
7m 32s
0%
42. Editing posts
3m 36s
0%
43. Deleting posts
4m 21s
0%
44. Deleting discussions
6m 7s
0%
45. Setting up for best answers
7m 29s
0%
46. Toggling the best discussion answer
12m 14s
0%
47. Solved and unsolved filters
2m 23s
0%
48. Indexing discussions for search
8m 6s
0%
49. Searching discussions
12m 35s
0%
50. Debouncing search
2m 47s
0%
51. Adding mentionable functionality to forms
6m 32s
0%
52. Indexing users for mentioning
9m 9s
0%
53. Hooking up users for mentions
10m 10s
0%
54. Detecting and storing mentioned users
9m 54s
0%
55. Adding the mentioned filter
2m 26s
0%
56. Adding mentions to the markdown toolbar
1m 1s
0%
57. Adding mentions to the reply form
7m 21s
0%
58. Fixing up some unauthenticated state
1m 1s
0%
59. Fixing up post scrolling
1m 48s
0%
60. Reviewing SSR (Server-side rendering)
8m 20s
0%
61. Preventing parent posts from being deleted
2m 31s
0%
62. Improving solution marking
4m 9s
0%

Transcript

00:00
Now that we're jumping to the correct page, how about scrolling down to the post that we're referencing here? Well, let's go ahead and start by just changing over the pagination in our discussion show controller. We're going to set this to maybe five.
00:13
So if we head back over to the page one on page two, let's just add a few more replies to this. So we'll just create a bunch of replies here. Just so we've got a little bit of room to scroll. OK, so yeah, let's go and just delete that last post just so we scroll all the way to the bottom of the page.
00:37
Because we don't want to roll over into the next page or we're not going to get the opportunity to see this scroll down. OK, so we're going to go ahead and use the ViewScrollTo library, which is incredibly easy to use. Let's go ahead and install this first of all, and then I'll walk you through how we're going to use this. So let's pull that in and we are first of all going to pass through the post ID down to this component via a prop.
01:02
So let's come down to here and let's set post ID here. And very simply, we're just going to cast this to an integer from the request. Grab the post ID now over in the discussion show component. Let's go ahead and accept that into here as a number.
01:25
And then what we can do is create some sort of trigger to scroll to this post. Now, when do we want this to happen? We want it to happen when this whole page is mounted, but we also want it to happen when this page is potentially updated as well. For example, if we edit a post or we do anything else, we want this to always scroll down if it has the post ID in there.
01:45
So to do this, we're going to go and import. We don't look like we have any imports from Vue at the moment. We're going to import on mounted and on updated from Vue. And we're then going to go down and define this out.
01:59
This is just one of the Vue lifecycle hooks. And then in here, we're going to check if we have a post ID within our props. And then we're going to go ahead and scroll to that. So let's just try this out first.
02:12
I'm going to do a little bit of refactoring just to make this a little bit cleaner. So if we don't have within our props a post ID, and let's just make sure we are assigning that here, then we're going to just go ahead and return. So if there's no post ID, we don't want to do anything. Now, if that is the case, we want to import the Vue scroll to library.
02:36
So let's pull that in from Vue scroll to. And we're going to go ahead and use this to scroll down the page. So we're going to say Vue scroll to, scroll to, and then we're going to pick the element. Now, we know that this is a ID.
02:53
So we're going to use a hash and it's post hyphen and then the post ID. Remember, we added that to the post component just over here. Okay, so post and then it will be props.post ID. And then in here, we can give a couple of options.
03:08
There are loads of options we can use for this library, but we're going to use the amount of time this is going to take to actually scroll. And then we're actually going to set an offset to this because we don't want the top of the post to be at the very top of the browser. So we're going to get set an offset here of minus 50 pixels. Okay, so if we come over now and we hit this page, nothing's actually happening at the moment.
03:31
Now, the reason for that is that we're not giving Vue a chance to go onto the next render to actually do this. So the page is being mounted and we have that on mount firing. We can prove that by just going ahead and console logging out mounted here. So that is actually working if we bring up our tools here.
03:52
So that is being mounted, but it's not quite catching the element. So to do this, we're going to use next tick. So let's go ahead and pull in next tick from Vue. And if we just wrap this in here, that's going to wait for the next render to then find that element and go ahead and scroll down to it.
04:13
So let's go over to this page, hit this, and this isn't actually on the page at the moment by the looks of it. So let's see what we've got here. Post 22 should be this one. OK, yeah. OK, so we're trying to scroll to post 22, which is the one that we deleted. Of course, that's not going to work. Let's scroll to 21 and you can see it worked.
04:38
So no matter how many times I refresh this page, it's always going to go ahead and scroll down. So we have the latest post in Vue. So that's working really nicely. But what we're going to do is just tidy this up because we want this to happen on update as well. So if we just grab all of the code that we've used here and we move this over to another method or a function, let's call this scroll to post.
05:05
Kind of makes sense. And we're going to accept the post ID into here and then we can just swap this out. And that just means that we can call this from multiple places. So that's now going to be the post ID here and we should be good. So now on mounted, we're going to scroll to the post with the props post ID.
05:26
And when the page gets updated. Then we're going to do the same thing. So that should have exactly the same effect if we just refresh the page. You can see we're scrolling down. So the scroll is quite quick. But just to demo this, if we bump this up to five seconds just to be a little bit silly,
05:42
you can see that it does actually smoothly scroll down to the post that we want. We'll leave that at 500, though, because I think that works really nicely. So now what's happening, if we count how many posts we've got on this page, we've got four. So if we were to go to page one and just sort of view this topic as we were landing on it,
06:00
we wanted to post a reply here and just say new reply. When we reply, we're going to be jumped over to the second page, but also be scrolled down so we can see the new reply. So I think this is all working nicely now. And it's exactly what we do expect from a user experience point of view.
62 episodes6 hrs 54 mins

Overview

Ready to build a forum with Inertia and Laravel?

Why a forum? A forum touches a whole load of concepts that you'll use throughout your development career – particularly on the client-side, where we'll be doing most of the heavy lifting.

So, let's build a clean, modern forum with features like markdown support, code highlighting, advanced filtering, user mentions, full-text search, the ability to mark best answers, and more.

Alex Garrett-Smith
Alex Garrett-Smith
Hey, I'm the founder of Codecourse!

Comments

No comments, yet. Be the first to leave a comment.