investtools/extensible-duck

Request: Normalizr schema as a duck module

Closed this issue · 2 comments

It would be nice to be able to be able to write and access normalizr schemas inside of the Ducks. Any opinions here?

Look at this example:

// formDuck.js

import { schema, denormalize } from 'normalizr'

// ...

const formSchema = {
  questions: [ new schema.Entity('questions', {
    choices: [ new schema.Entity('choices') ],
    sectors: [ new schema.Entity('sectors') ],
  }) ]
}

// ...

export default createDuck({
  namespace: 'crm',
  store: 'form',
  path: '/forms',
  initialState: { questions: [] }
}).extend({
  creators: ({ types, options }, parent) => ({
    get: id => ({ ...parent.get(id), meta: { schema: { data: formSchema } } }),
    save: () => (dispatch, getState) => {
      const state = getState()[options.store]
      dispatch({
        type: types.PATCH,
        payload: server.patch(urljoin('/forms', state.obj.id), { form: denormalize(state.obj, formSchema, state) })
      })
    },
    // ...
  })
  // ...
})
// store.js
import normalizrMiddleware from 'redux-normalizr3-middleware'

const middleware = applyMiddleware(thunk, normalizrMiddleware(), ...)

// ...

Do you see a better approach?

No... I realized how unnecessary my request was after posting it. Thanks for posting this example though.