-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optimize storeUpdater
Extensibility
#163
Comments
storeUpdaterCreate
ExtensibilitystoreUpdater
Extensibility
With Can be used to individually overwrite:
import { UIStoreActionSet, UIStoreAction, UIStoreUpdaterData } from '@ui-schema/ui-schema/UIStoreActions'
import { scopeUpdaterMapDefault, createStoreUpdater, ScopeUpdaterMapType } from '@ui-schema/ui-schema/storeUpdater'
import { onChangeHandler, UIStoreType, UIStoreUpdaterFn } from '@ui-schema/ui-schema/UIStore'
import React from 'react'
// new custom action:
export interface CustomStoreActionElementDelete extends UIStoreAction {
type: 'element-delete'
}
// all actions supported by custom reducer:
export type CustomStoreActions =
UIStoreActionSet | CustomStoreActionElementDelete
// this map controls the `setter`/`getter` of the `storeKeys` position for each `scope`
const customScopeUpdaterMap: ScopeUpdaterMapType = {
...scopeUpdaterMapDefault,
value: {
// experimental, does not use `getter` and `setter` for that scope
noStore: true,
},
}
// the reducers must return the new data for the `storeKeys` position - except when `noStore` is on for the scope
// todo: typing/return type, especially in relation to `noStore` need to be optimized
export const storeActionReducers = <D extends UIStoreUpdaterData = UIStoreUpdaterData>(action: CustomStoreActions): UIStoreUpdaterFn<D> | D => {
switch(action.type) {
case 'element-delete':
// todo: implement custom logic
return {value: undefined} as D
case 'set':
// todo: implement custom logic
return action.data as D
default:
// @ts-ignore
throw new Error('storeActionReducers unsupported action: ' + (action.type))
}
}
// bind the custom reducers and scope-setter/getter to the internal onchange dispatching
// todo: optimize typing for actions, somehow `extends` is required for IDE formatting there - but `extends` enforces wrong duck typing
// @ts-ignore
export const customStoreUpdater = createStoreUpdater<UIStoreType, CustomStoreActions>(storeActionReducers, customScopeUpdaterMap)
// binding the custom updater to create a new `onChange`
export const useCustomOnChange = (
{
setStore,
}: {
setStore: React.Dispatch<React.SetStateAction<UIStoreType>>
},
) => {
const onChange: onChangeHandler<CustomStoreActions> = React.useCallback((actions) => {
setStore(customStoreUpdater(actions))
}, [setStore])
return onChange
} I've built it with and around: Reusable |
…pings #163 and normalize/optimize the GenericList widget html-structure and a bit of styling, very small sizing/gutter adjustments and better support for e,g. footer
The general structure of
storeUpdaterCreate
storeUpdater
with the optionsactionHandler
/scopeUpdaterMap
should be optimized to be easier extensible.Currently some logic is hard wired inside
scopeUpdaterMap
, e.g.deleteOnEmpty
in thescopeUpdaterValues
and deletingvalid
whenundefined
in thescopeUpdaterValidity
. These should be moved to a chain of functions, which can be added to thescopeUpdaterMap
.The
storeActionHandler
currently contains all store-action implementations, the reason why thestoreUpdater
historically needed to receive theoldStore
, but together with thetype=set
action -> now not all store-actions need theoldStore
- depending how the state is implemented. Thus thestoreActionHandler
should be split up and thestoreUpdater
should be more modular in general. e.g. allowing to "handle theset
action forvalue
scope without getting theoldStore
.".The text was updated successfully, but these errors were encountered: