This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const reducerJedi = createClassReducer(ReducerJedi) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-create-reducer' | |
const createClassReducer = (ReducerClass) => { | |
const reducerClass = new ReducerClass() | |
const methodsWithActionTypes = getReducerClassMethodsWthActionTypes( | |
reducerClass, | |
) | |
const reducerMap = getReducerMap(methodsWithActionTypes) | |
const initialState = reducerClass.initialState | |
const reducer = createReducer(initialState, reducerMap) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getReducerMap = (methodsWithActionTypes) => | |
methodsWithActionTypes.reduce((reducerMap, { method, actionType }) => { | |
reducerMap[actionType] = method | |
return reducerMap | |
}, {}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const getReducerClassMethodsWthActionTypes = (instance) => { | |
// Get method names from class' prototype | |
const proto = Object.getPrototypeOf(instance) | |
const methodNames = Object.getOwnPropertyNames(proto).filter( | |
(name) => name !== 'constructor', | |
) | |
// We want to get back a collection with action types and corresponding reducers | |
const res = [] | |
methodNames.forEach((methodName) => { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const METADATA_KEY_ACTION = 'reducer-class-action-metadata' | |
export const Action = (...actionTypes) => (target, propertyKey, descriptor) => { | |
Reflect.defineMetadata(METADATA_KEY_ACTION, actionTypes, target, propertyKey) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
class ReducerJedi { | |
initialState = { | |
loading: false, | |
data: [], | |
error: undefined, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createReducer } from 'redux-create-reducer' | |
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
const reducerJediInitialState = { | |
loading: false, | |
data: [], | |
error: undefined, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
const reducerJediInitialState = { | |
loading: false, | |
data: [], | |
error: undefined, | |
} | |
const reducerJediMap = { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const actionTypeJediCreateInit = 'jedi-app/jedi-create-init' | |
const actionTypeJediCreateSuccess = 'jedi-app/jedi-create-success' | |
const actionTypeJediCreateError = 'jedi-app/jedi-create-error' | |
const reducerJediInitialState = { | |
loading: false, | |
// List of our jedi | |
data: [], | |
error: undefined, | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ActionStandard { | |
get static type () { | |
return `prefix/${this.name}` | |
} | |
constructor(payload) { | |
return { | |
type: this.constructor.type, | |
payload, | |
error: payload instanceof Error | |
} |