View Controllers
The ModelShow
, ModelIndex
, ModelCreate
and ModelEdit
top level view components are driven by view controllers that provide the data to power a React context wi These controllers are created with hooks. The basic pattern for the components is:
The Base
version of the component will take the arguments for the controller as props and pass them to the controller hook. The Provider
version of the component will take the results of the controller hook and set them as the value for the view Context
. The context can then be access via a useModelViewContext
and will provide the results of the controller hook.
const MyCustomComponent = () => {
const { resource } = useModelIndexContext();
return <div>{resource.name}</div>
}
<ModelIndexBase defaultFilter={{category: 6}}>
<MyCustomComponent/>
<ModelIndexBase>
useModelShowController
useModelShowController takes an options object for configuration.
const { model, resource } = useModelShowController(options);
model
a string for the model name or an already existing model object (from useModel or getModel) (required)modelId
a value for the id of the object to fetch (omitted or null for singular models) (required)paths
the paths to render for the create view (default: all viewable paths except for the parent)- All options for useModelShow
and in addition to the useModelShow returns, useModelShowController returns:
modelId
the id of the model object being fetchedrenderPaths
the elementized paths for the show viewcreate
the return values of useModelCreateupdate
the return values of useModelUpdatedestroy
the return values of useModelDestroy
useModelShowContext
Returns the context for the model show view.
ModelShowContext
The model show context.
ModelShowProvider
Injects the results of the model show controller into the context.
useModelIndexController
useModelIndex takes an options object for configuration.
const { page, resources } = useModelIndex(options);
model
a string for the model name or an already existing model object (from useModel or getModel) (required)syncUrl
a boolean indicating whether to sync the url with the query parameters (default: true)defaultFiltersBaseOwner
a boolean indicating whether to include a filter by the base owner (default: true)defaultFilter
the default filterdefaultOrder
the default orderdefaultLimit
the default limitdefaultOffset
the default offsetdefaultSearch
the default searchqueryOptions
as per useModelIndexnetworkOptions
as per useModelIndex
and in addition to the useModelIndex returns, useModelIndex returns:
defaultState
the default state of the model index controller - before any URL parameters are appliedinitialState
the current state of the model index controller - after any URL parameters are appliedorder
the current order of the resourcessetOrder
a function to set the order of the resourcesfilter
the current filter of the resourcessetFilter
a function to set the filter of the resourcestotalFilters
the total number of dynamic filtersfullFilter
the current full filter of the resources (default plus dynamic filters)totalFullFilters
the total number of full filterssetDefaultFilter
a function to set the default filter of the resourcessearch
the current search of the resourcessetSearch
a function to set the search of the resourceslimit
the current limit of the resourcessetLimit
a function to set the limit of the resourcesoffset
the current offset of the resourcessetOffset
a function to set the offset of the resourcestotalPages
the total number of pages of resources base on current limitpage
the current page of the resourcessetPage
a function to set the page of the resourceshasNextPage
a boolean indicating whether there is a next page of resourceshasPrevPage
a boolean indicating whether there is a previous page of resourcesfirstPage
a function to set the page to the first page of resourceslastPage
a function to set the page to the last page of resourcescreate
the return values of useModelCreateupdate
the return values of useModelUpdatedestroy
the return values of useModelDestroy
useModelIndexContext
Returns the context for the model index view.
ModelIndexContext
The model index context.
ModelIndexProvider
Injects the results of the model index controller into the context.
useModelCreateController
useModelCreateController takes an options object for configuration.
const {
mutation,
showParent: { resource: blog },
} = useModelCreateController(options);
model
a string for the model name or an already existing model object (from useModel or getModel) (required)parentId
the id of the parent (required)disabled
a boolean indicating whether the form elements in the view should be disabled (default: false)extraDefaultValues
values to augment the default values for the create viewpaths
the paths to render for the create view (default: all creatable paths except for the parent)queryOptions
options to pass to the query for the parent mo
and in addition to the useModelCreate returns, useModelCreateController returns:
parentModel
a memoized version of the parent modelparentId
the id of the parent model object being fetchedresolver
the yup resolver for the model uses for form validationschema
the yup schema for the model uses for form validationmethods
the output ofuseForm
from react-hook-formshowParent
the return values of useModelShow for the parent object
useModelCreateContext
Returns the context for the model edit view.
ModelCreateContext
The model edit context.
ModelCreateProvider
Injects the results of the model edit controller into the context.
useModelEditController
useModelEditController takes an options object for configuration.
const {
mutation,
show: { resource: blog },
} = useModelEditController(options);
model
a string for the model name or an already existing model object (from useModel or getModel) (required)modelId
the id of the resource to be edited (required)disabled
a boolean indicating whether the form elements in the view should be disabled (default: false)extraDefaultValues
values to augment the default values for the create viewdebounceDelay
the delay in ms to debounce the mutation (default: 2000)paths
the paths to render for the create view (default: all creatable paths except for the parent)queryOptions
options to pass to the query for the model object (passed to useModelShow)
and in addition to the useModelUpdate returns, useModelEditController returns:
modelId
the id of the model object being fetchedresolver
the yup resolver for the model uses for form validationschema
the yup schema for the model uses for form validationmethods
the output ofuseForm
from react-hook-formdebouncedMutate
a debounced mutate function delayed bydebounceDelay
show
the return values of useModelShow
useModelEditContext
Returns the context for the model edit view.
ModelEditContext
The model edit context.
ModelEditProvider
Injects the results of the model edit controller into the context.