mobx-blocks (beta)
MobX classes designed for solving common problems encountered when building dashboard-style web applications. With 100% test coverage.
Collection
Given an async fetch function like:
type Product = { id: string }
type FetchFn = (params: Record<string, any>) => Promise<{ data: Product[] }>
We can pass it to a new Collection to let it manage the following:
Storing the data (boring on its own)
const products = new Collection({ fetchFn })
console.log(c.data)
Sorting
const products = new Collection({
fetchFn,
sortBy: "id" as "id" | "title",
})
products.sorting.sort("id")
products.sorting.toggleDirection()
products.sorting.setKey("title")
products.sorting.setAscending(true)
Filtering
const products = new Collection({
fetchFn,
initialFilters: { title: "Foo", valid: false } as ProductQueryParams,
})
products.filters.set("title", "Bar")
products.filters.get("title")
products.filters.merge({ title: "Bar" })
products.filters.replace({ title: "Bar" })
products.filters.reset()
Selection
const products = new Collection({ fetchFn })
await products.fetch()
products.selection.select(products.data.?[0])
products.selection.select(products.data.?[0])
products.selection.set(products.data)
Caching
import { Cache, Collection } from "mobx-blocks"
const productsCache = new Cache({ ttl: 3 })
const products = new Collection({ fetchFn, fetchOne, cache: productsCache })
await products.fetch({ sortBy: "title", ascending: false })
await products.fetch({ sortBy: "title", ascending: false })
await products.fetchOne("id")
await products.fetchOne("id", { useCache: false })
synchronizing query params to URL
const products = new Collection({
fetchFn,
sortBy: "title",
initialFilters: { foo: "foo", bar: "bar" },
syncParamsToUrl: true,
})
products.fetch()
TODO: initializing from query string
const products = new Collection({
fetchFn,
sortBy: null as null | "title",
initialFilters: { foo: "", bar: "" },
})
products.init({ queryString: "?sortBy=title&sortAscending=true&foo=foo&bar=bar" })
console.log(products.sorting.params)
console.log(products.filters.active)
Kitchen-sink example
Source
Types
Cache
[TODO] Manages caching of list/detail queries
[TODO] Manages pagination
Filters
[TODO] Manages filters
Selection
[TODO] Manages filters
Notifications
[TODO] Manages notifications
Modals
[TODO] Manages modals