A long list is usually loaded a page at a time, with each new page added to the ones already on screen. In this chapter we'll send one page of a list as a scroll prop, and read the metadata the crate attaches so the client can ask for the next one.
This chapter assumes Chapter 6. It covers what the server sends. The client side is Inertia's <InfiniteScroll> component, which this chapter names and does not run. Page-number paginators with links are not in this crate.
A page that describes itself
The struct carried current_page and last_page and serialized neither. They were for the server's use. The client still learned that page 2 came next, from a field the struct never mentioned.
Sometimes you have one page of items and need the client to know where it sits among the others. Thankfully, the page can say so itself. Take a look at Example 10-1.
Example 10-1. A page of invoices
#[derive(Serialize)]
pub struct InvoicePage {
pub data: Vec<&'static str>,
#[serde(skip)]
pub current_page: u64,
#[serde(skip)]
pub last_page: u64,
}
impl inertia::ProvidesScrollMetadata for InvoicePage {
fn scroll_metadata(&self) -> inertia::ScrollMetadata {
inertia::ScrollMetadata::pages("page", self.current_page, self.last_page)
}
}
pub async fn feed(inertia: Inertia) -> inertia::Response {
let page = InvoicePage { data: vec!["INV-7", "INV-8"], current_page: 1, last_page: 3 };
inertia.render("Invoices/Feed", props! { "invoices" => inertia::scroll(page) })
}
There are three new pieces here. ProvidesScrollMetadata is a trait with one method, which returns the paging state. ScrollMetadata::pages builds that state from a query parameter's name, the current page and the last page. inertia::scroll turns the page into a prop.
Example 10-2 is the part of the page object that concerns the list.
Example 10-2. The scroll prop on the wire
{"props":{"invoices":{"data":["INV-7","INV-8"]}},
"mergeProps":["invoices.data"],
"scrollProps":{"invoices":{"pageName":"page","previousPage":null,"nextPage":2,"currentPage":1,"reset":false}}}
As you can see, the items are under data, which is where scroll expects them. mergeProps names invoices.data, so the client adds the next page's items to the ones it has. scrollProps carries the paging state: no previous page, and page 2 next.
You can also page by cursor. ScrollMetadata::cursors takes the parameter's name and the previous, next and current cursors as strings.
You can also load the page inside a callback with scroll_with, so the query runs only when the prop is part of the response. If you're unsure which to use, I'd start with scroll_with; the plain form runs your query on partial reloads that never asked for the list.
What this chapter leaves open
So the client knows page 2 follows page 1. Now suppose an invoice is created between the two requests. With page numbers and a list sorted newest first, the last item of page 1 moves to the top of page 2 and the user sees it twice. Does your list change while people read it? Would a cursor fix that for you, and is there a column to build one from?
Testing
Example 10-3 fails if the items stop arriving under data.
Example 10-3. Items under data
AssertablePage::from_body(&reply.body).has_count("invoices.data", 2);
TL;DR
A scroll prop is a page of items plus metadata. Implement ProvidesScrollMetadata on the page, build the metadata with ScrollMetadata::pages or ScrollMetadata::cursors, and pass the page to inertia::scroll. The crate puts the items' path in mergeProps and the paging state in scrollProps.