Usage
@xpfw/data
packages provide 4 hooks. One for each of the CRUD operations.
Click here to see a live demo of
@xpfw/data
Quick Start
If you want a full CRUD UI from a JSON-Schema out of the box you can use use @xpfw/data-bulma
.
For custom styling you can use the hooks in your own components!
The parameters are minimal: They all require an ExtendedJSONSchema
and optionally take a prefix. If it's an edit, show or delete you also need to supply an id. If it's a show you don't even need a form just the collection
name.
import {
BulmaCreate, BulmaEdit, BulmaDelete, BulmaShow, BulmaList
} from "@xpfw/data-bulma"
// replace with your own
const form = {
model: "testModel",
collection: "testCol",
sections: [{fields: [{
mapTo: "myString",
type: FieldType.Text,
validate: {required: {type: RequiredType.Always}}
}]}]
}
<BulmaCreate schema={schema} prefix="optionalPrefix" />
<BulmaEdit schema={schema} prefix="optionalPrefix" id="idOfEditedItem" />
<BulmaDelete schema={schema} prefix="optionalPrefix" id="idOfEditedItem" />
<BulmaShow schema={schema} id="idOfItem" />
<BulmaList schema={schema} prefix="optionalPrefix" />
Hooks
The @xpfw/data-bulma
package was built to provide a quick start. Most probably you will want to customize the components and let @xpfw/data
only focus on providing the data.
For every of the 4 CRUD operation's there is a React hook as well as for lists and authentication (login / registration / logout):
Create, Read, Update, Remove, List, Authentication
For complete code examples of wrapped components you can look at the implementations used for the demo site:
Create, Read, Update, Remove, List
Create
Example usage of the useCreate
Hook can be found here
Expects:
schema
=>ExtendedJSONSchema
conform JSON-Schema- (optional)
mapTo
=> use for FormStore path instead ofschema.title
- (optional)
prefix
=> prepend to FormStore path Provides the following properties: submitCreate
=> Call this and thecreate
call will be called.state
,loading
anderror
will update respectivelystate
=> Status of the creationloading
=> boolean indicating businesserror
=> null if none otherwise array of invalid fieldsuser
=> currently logged in user
Read
Example usage of the useGet
hook can be found here
Expects:
id
=> The ID of the requested recordcollection
=> The name of the collection to fetch the record from Provides the following properties:item
=> either null or the requested object fromcollection
andid
combinationloading
=> boolean indicating business
Edit
Example usage of the useEdit
hook can be found here
Expects:
id
=> The ID of the record to editschema
=>ExtendedJSONSchema
conform JSON-Schema. Thecollection
property is required if used in this hook!- (optional)
mapTo
=> use for FormStore path instead ofschema.title
- (optional)
prefix
=> prepend to FormStore path Provides the following properties: submitEdit
=> Call this and theedit
call will be called.state
,loading
anderror
will update respectivelystate
=> Status of the creationloading
=> boolean indicating businesserror
=> null if none otherwise array of invalid fieldsuser
=> currently logged in user
Delete
useRemove
Example usage of the useRemove
hook can be found here
Expects:
id
=> The ID of the record to removeschema
=>ExtendedJSONSchema
conform JSON-Schema. Thecollection
property is required if used in this hook! Provides the following properties:submitRemove
=> Call this and theremove
call will be called.state
,loading
anderror
will update respectivelystate
=> Status of the creationloading
=> boolean indicating businesserror
=> null if none otherwise array of invalid fields
List
Example usage of the useList
Hook can be found here
Expects:
schema
=>ExtendedJSONSchema
conform JSON-Schema- (optional)
mapTo
=> use for FormStore path instead ofschema.title
- (optional)
prefix
=> prepend to FormStore path - (optional)
options
=> optional advanced options Provides the following properties: nextPage
=> Increase page count, load content of next page and hence change content oflist
prevPage
=> Decrease page count, load content of previous page and hence change content oflist
list
=> Array of found items you can rendercurrentPage
=> null if none otherwise array of invalid fieldsmaxPage
=> null if none otherwise array of invalid fieldsshowNextPage
=> convenience prop to know whether there are more pages available or not based on comparison of currentPage and maxPageshowPrevPage
=> convenience prop to know whether there are earlier pages available or not based on comparison of currentPage and maxPage
Authentication
The useAuth
hook provides information about the current user.
It provides the following properties:
submitLogin
=> Call this and thelogin
will be called with the value of MailField and PwField.error
,user
andloading
will update respectivelysubmitLogout
=> Call this and thelogout
will be called with the value of MailField and PwField.error
,user
andloading
will update respectivelysubmitRegister
=> Call this and theregistration
will be called with the value of MailField and PwField.error
,user
andloading
will update respectivelyloggedIn
=> true if successfully logged inconnected
=> Whether an active connection to the server existsloading
=> whether user login/out/register related activites are ongoinguser
=> Currently logged in user. Null if not logged inerror
=> null if none otherwise array of invalid fields