prikhi/paginate/1.0.1

NOTE: this package is not compatible with Elm 0.19.1

Paginate

Paginate is used for querying & paginating API responses. Paginate will handle fetching new pages of data, and caches already fetched data.

TODO: Eventually:

  • examples
  • fetchRequest per-args cache?
    • toString on args to get a (Dict String (Dict PageNumber Chunk))?
  • custom page sizes(+ reorganize items when changed)

Model

type Paginated a b

The Paginated type is responsible for storing the fetched items, current page number, total count, and additional data to pass to the fetch command..

initial : Config a b -> b -> Int -> ( Paginated a b, Cmd (Msg a) )

Get an initial Pagination & Fetch Commands from a Config, list of Filters, & Page Number.

Config

type Config a b

The Config type is used to build a Fetch Request, given the Paginated's Request Data & a Page Number.

type alias FetchResponse a =
{ items : List a
, totalCount : Int
}

The result type of a Paginated Fetch Request. At the minimum, your API needs to return the items & a total count of all items.

makeConfig : (b -> Int -> Request (FetchResponse a)) -> Config a b

Make a Config from a function that takes a list of Parameters & a Page Number.

Retrieving Data

getCurrent : Paginated a b -> List a

Get the current list of items.

getPage : Paginated a b -> Int

Get the current page number.

getTotalPages : Paginated a b -> Int

Get the total number of pages.

getTotalItems : Paginated a b -> Int

Get the total item count.

getError : Paginated a b -> Maybe Error

Return the current page's fetch request's error if it has one.

getData : Paginated a b -> b

Return the Extra Request Data for the current Paginated.

Querying Status

isLoading : Paginated a b -> Bool

Is the current page's fetch request still loading?

hasNone : Paginated a b -> Bool

Did the current page load successfully but return no items?

hasPrevious : Paginated a b -> Bool

Are there page's before the current one?

hasNext : Paginated a b -> Bool

Are there page's after the current one?

Modifying Pagination

moveNext : Config a b -> Paginated a b -> ( Paginated a b, Cmd (Msg a) )

Move to the next page. TODO: re-implement as call to jumpTo?

movePrevious : Config a b -> Paginated a b -> ( Paginated a b, Cmd (Msg a) )

Move to the previous page. TODO: re-implement as call to jumpTo?

jumpTo : Config a b -> Paginated a b -> Int -> ( Paginated a b, Cmd (Msg a) )

Move to a specific page.

updateData : Config a b -> Paginated a b -> b -> ( Paginated a b, Cmd (Msg a) )

Replace the current Extra Request Data, jumping to page 1 & performing new fetch requests. Does nothing if the Data is equal to existing Data.

Updating / Messages

type Msg a

Wrap an Http Response for a Page, to be handled in the update function.

Config a b
-> Msg a
-> Paginated a b
-> ( Paginated a b, Cmd (Msg a) )

Update the Paginated Model on Fetch Completion.

If the Page Number of the Returned Model is Different than the Given Model, We Were On a Page That Didn't Exist, So We Changed to the Last Page.

You Should Handle this Special Case in your Update Function, so that You can Modify the Page URL.