API Redux
API redux is a front end library that uses redux thunk to make api calls and store their result in redux. It supports Auth, Interface, event, history and user calls. If set up correctly, it makes api calls immediately accessible anywhere dispatch is accessible.
Installation
npm install --save @leverege/api-redux
Reducer Setup
Import the reducers that you are going to use:
- InterfaceReducer - reducer that will handle imagine interface queries
- AuthReducer - reducer that handles authentication
- HistoryReducer - reducer that handles queries for getting and updating imagine history
- EventReducer - reducer that handles queries for getting and updating imagine events
- UsersReducer - reducer that handles queries for getting and updating imagine Users
import { combineReducers } from 'redux'
import { InterfaceReducer, AuthReducer, HistoryReducer, EventReducer, UsersReducer } from '@leverege/api-redux'
...
const reducers = combineReducers( {
...
interface : InterfaceReducer
auth : AuthReducer
history : HistoryReducer
event : EventReducer
users : UsersReducer
} )
export default reducers
Incorporate api-redux into your react component
import { Interface } from 'leverege/api-redux'
class MyScreen extends React.Component {
constructor( props ) {
super( props )
const { dispatch } = props
this.items = Interface.create( system, 'myType' ).getList( interface )
dispatch( items.list() )
}
render() {
const { model } = this.props
const items = this.items.getList( model )
return (
<div>
{items.map( item => item.name )}
</div>
)
}
}
export default connect(
state => ( {
model : state.interface
} )
)( MyScreen );
Auth
Auth has several methods all relating to authenticating the user and managing their password ( these need to be dispatched )
- verify( [ targetLocation ] ) - gets and verifies the bearer token
- login( username, password, [ projectId ] ) - logs in the user
- logout() - logs out the user
- forgotPassword( username, projectId ) - sends an email to the user's email address with instructions to reset their password
- changePassword( oldPassword, newPassword ) - changes the current logged in user's password
- resetPassword( username, password, confirm, token, projectId ) - uses an api generated token to allow a user to change their password
In addition to several methods to check the current state
- isLoggedIn( state ) - returns a boolean representing if there is a user logged in
- isLoading( state ) - returns a boolean representing if a Auth query is loading
- isVerifying( state ) - returns a boolean representing if a verify query is loading
- isError( state ) - returns a boolean representing if there was an Auth error
- getErrorMessage( state ) - returns the last error message
- getUsername( state ) - returns the current user's username
- getUserId( state ) - returns the current user's imagine userId
- getProfile( state ) - returns the current user's profile
Users
Users has several methods all relating to maintaining and giving permissions to users ( these need to be dispatched; almost all the methods are for admins, but some allow for the user to make changes to themselves )
- list( opts ) - gets all the current users for the project
- create( user ) - creates a new project user
- get( userId, opts ) - gets a user
- remove( userId ) - removes a user
- update( userId, values, opts ) - updates the user with the values
- forcePWReset( userId, projectId ) - sends an email to the user to reset their password
Interface
Interface is used to interact with devices and groups on the system and corresponds to the Interface queries. it is possible to chain together objects and groups to get a desired device.
create
create( systemId, blueprintAlias ) - creates an Interface object with several methods
list( options ) - makes an api call to get all the devices in the system with the blueprintAlias
create( device, options ) - makes an api call to create a device
delete( id, options ) - makes an api call to delete a device
model( options ) - makes an api call to get the model
obj( id ) - creates an instance of the Obj class with the id
getList( state, opts ) - gets the list from the state
getModel( state, opts ) - gets the model from the state
isLoading( state, opts ) - returns a boolean based on if there is a query being made
isError( state ) - returns a boolean based on if there was an error for a query
isDone( state ) - returns a boolean based on if a query is finished
Object
Obj( parentPath, deviceId, attribute ) - creates an instance of the Obj class
- created by
Interface.create( 'mySystem', 'myBP' ).obj( 'myDeviceId' )
which will correctly manage the path for you
This class is used to interact with the api for a specific device
- get( options ) - makes an api call to get the device
- history( query, options ) - makes an api call to get the history for the device. query is an object with a queryName and query object, refer to history documentation for how to structure the query
- create( device, options ) - creates a child device
- update( obj, options ) - updates the current device
- delete( options ) - deletes the current device
- model( options ) - gets the model for the current device
- obj( id ) - new instance of the Obj class pointed at the new device
- grp( attribute ) - new instance of the Grp class, attribute should be a relationship attribute
- users() - gets all the users permissioned on the device
- getObject( state ) - gets the device from the state after an api call has been made
- getTable( state, opts ) - gets the table for the device from the state after an api call
- getModel( state, opts ) - gets the model from the state after an api call
- getHistory( state, queryName ) - gets the history query result corresponding to the queryName
- isTable( state ) - boolean on whether the api result is a table
- isLoading( state ) - boolean on whether an api call is being made
- isError( state ) - boolean on whether an api call returned an error
- isDone( state ) - boolean on whether an api call is done
Group
Grp( parentPath, deviceId, attribute ) - creates an instance of the Grp class
- created by
Interface.create( 'mySystem', 'myBP' ).obj( 'myDeviceId' ).grp( 'myChildDevices' )
which will correctly manage the path for you
A group represents a group of devices often by their relationship to a parent device
- list( options ) - api call to get the list of the group of devices
- create( obj, options ) - api call to create a new device for the group
- get( id, options ) - api call to get a specific device from the group
- update( id, data, options ) - api call to update a specific device from the group
- delete( id, options ) - deletes a device
- model( options ) - gets the model for the devices
- obj( id ) - Obj class Instance
- getList( state, opts ) - gets the list from the state
- isTable( state, opts ) - returns a boolean based on if the api call result is a table
- getObject( state, id, opts ) - returns a device from the state
- getModel( state ) - returns the model from the state
- isLoading( state ) - returns if an api call is loading
- isError( state ) - return if an api call made an error
- isDone( state ) - returns if an api call is finished
Filters
You can install custom filter functions by calling
Interface.setFilter( 'myFilterType', ( filter, items ) => {
return items.filter( item => item.whatever == filter.myOption )
} )