Reducer
Reducer is an object created using utReducer(). The created object is then connected to the layout component by the utCreateElement().
utReducer(name, reducer)
#name: string
name is an unique identifier for referring to the store object inside Redux Store. The convention is normally in <Module>/<Component> format.
Example
customer/search - Search view in Customer module
customer/create - Create form in Customer module
common/report-index - Report index page in Common module
reducer: object
Reducer object with init: and actions: properties.
Return
#Example
#const reducer = utReducer('get-started/helloworld', {
init: {
fields: {
name: {
label: 'Please enter your name'
},
...
},
<variable>: <value>,
...
},
actions: {
clear: (state, value) => state.fields.name.value = '',
...
}
})
init: {
fields: {
name: {
label: 'Please enter your name'
},
...
},
<variable>: <value>,
...
},
actions: {
clear: (state, value) => state.fields.name.value = '',
...
}
})
init: object
fields: object
A special fields object storing all field definitions.
Refer to Field for more details.
<variable>: any
Other variables in store which can be accessed in Layout components.
actions: object
Object contains actions in the format of
<action name>: ( _, <data object> ) => {}
( _, <data object> ) => {}
Function used to update the state of the reducer, where
_ - Reference to the state init: object defined above
merge(current reducer, new reducer)
#Helper function to deep merge the current reducer object with the new reducer object. Properties with the same name will be overridden by the new reducer.
Example
#const reducer = utReducer('crud/contact',
merge(_reducer, {
actions: {
delete: (_, rows) => {
const _rows = rows.map(t => t.rowIndex)
_.rows = _.rows.filter((t, i) => _rows.indexOf(i) < 0)
},
add: (_, row) => {
const _row = {}
each(row, (v, k) => _row[k] = v.value)
if(_row.id) {
const i = findIndex(_.rows, t => t.id === _row.id)
_.rows[i] = _row
} else {
_row.id = uniqueId()
_.rows.push(_row)
}
}
}
})
)
merge(_reducer, {
actions: {
delete: (_, rows) => {
const _rows = rows.map(t => t.rowIndex)
_.rows = _.rows.filter((t, i) => _rows.indexOf(i) < 0)
},
add: (_, row) => {
const _row = {}
each(row, (v, k) => _row[k] = v.value)
if(_row.id) {
const i = findIndex(_.rows, t => t.id === _row.id)
_.rows[i] = _row
} else {
_row.id = uniqueId()
_.rows.push(_row)
}
}
}
})
)