jsonforms icon indicating copy to clipboard operation
jsonforms copied to clipboard

React middleware support

Open sdirix opened this issue 4 years ago • 5 comments

Description Sometimes general modifications of JSON Forms behavior would be useful. For example to listen on update events and to modify errors and data before they are set.

Solution Similar to the middleware concept in Redux, the JsonForms component could accept a middleware function.

Conceptually the API could look like this

const loggerMiddleware = (previousState, dispatch, action) => {
  console.log('dispatching', action)
  // here you could modify the action or dispatch multiple ones
  const nextState = dispatch(previousState, action)
  // here you could modify the resulting state
  console.log('next state', nextState)
  return nextState;
}

On JSON Forms side this can be implemented by handling the core state via useState instead of useReducer.

Alternatives

  • Instead of adding yet another parameter to the JsonForms component the middleware could also be provided by another context, similar to the JsonFormsStyleContext.
  • Instead of the JsonForms component only the JsonFormsStateProvider component is enhanced. In that case the advanced use case of adding a middleware can be done by manually creating the JsonFormsStateProvider instead of using the JsonForms component.

Describe for which setup you like to have the improvement Framework: [React]

Could also be implemented for Angular

sdirix avatar Dec 17 '20 07:12 sdirix

Description Sometimes general modifications of JSON Forms behavior would be useful. For example to listen on update events and to modify errors and data before they are set.

Solution Similar to the middleware concept in Redux, the JsonForms component could accept a middleware function.

Conceptually the API could look like this

const loggerMiddleware = (previousState, dispatch, action) => {
  console.log('dispatching', action)
  // here you could modify the action or dispatch multiple ones
  const nextState = dispatch(previousState, action)
  // here you could modify the resulting state
  console.log('next state', nextState)
  return nextState;
}

On JSON Forms side this can be implemented by handling the core state via useState instead of useReducer.

Alternatives

  • Instead of adding yet another parameter to the JsonForms component the middleware could also be provided by another context, similar to the JsonFormsStyleContext.
  • Instead of the JsonForms component only the JsonFormsStateProvider component is enhanced. In that case the advanced use case of adding a middleware can be done by manually creating the JsonFormsStateProvider instead of using the JsonForms component.

Describe for which setup you like to have the improvement Framework: [React]

Could also be implemented for Angular

Could Someone please provide an example of implementing middleware in JSONForms, possibly in standalone JSONForms React version.

manju2791 avatar Jul 19 '21 13:07 manju2791

Hi @manju2791, this feature is not yet implemented. I linked it in #1469 because if this middleware approach was implemented, #1469 would already be solved. It's probably preferable over implementing a special solution for #1469.

sdirix avatar Jul 19 '21 15:07 sdirix

Coming here form this post https://jsonforms.discourse.group/t/showing-server-side-back-end-errors-on-the-form-in-react-standalone-jsonforms/292

Any updates? : )

Thanks a lot for this awesome work.

My ideal setup would be

  1. Disabled frontend validations. Not adding ajv validations at all.
  2. Using ajv validations on my node server and when I submit data from a JSONForm return an errors key that I can put back in the form.

I'm considering doing it overriding the provider while the middleware solution is developed.

andresgutgon avatar Dec 02 '21 10:12 andresgutgon

Hi @andresgutgon, no update yet. We planed this issue for version 3.x but are still busy with the 3.0 release.

Coming here form this post https://jsonforms.discourse.group/t/showing-server-side-back-end-errors-on-the-form-in-react-standalone-jsonforms/292

To clarify the post in the discussion board: Using a middleware support would be one way to support the use case of showing external errors, however I think it would be best solved via explicit support, e.g. by adding an errors prop to the root component which are mixed in with, or replace, the errors determined within JSON Forms. This is basically option 3 of the discussed post.

sdirix avatar Dec 02 '21 11:12 sdirix

I think it would be best solved via explicit support, e.g. by adding an errors prop to the root component which are mixed in with, or replace, the errors determined within JSON Forms

I like it, but this is not possible at the moment right? I mean without overriding default provider.

andresgutgon avatar Dec 02 '21 11:12 andresgutgon

Implemented with https://github.com/eclipsesource/jsonforms/pull/2220

sdirix avatar Jan 10 '24 16:01 sdirix