Skip to content

Instantly share code, notes, and snippets.

@vishin-pavel
Last active July 12, 2018 07:36
Show Gist options
  • Save vishin-pavel/4b797f899846c3f12b1e90ca0bdca2a3 to your computer and use it in GitHub Desktop.
Save vishin-pavel/4b797f899846c3f12b1e90ca0bdca2a3 to your computer and use it in GitHub Desktop.
PhpStorm redux duck template
#set ($UppaerCaseModuleName = $ModuleName.toUpperCase())
#set ($capitalizedModuleName = $ModuleName.substring(0,1).toUpperCase() + $ModuleName.substring(1))
import { appName } from '../../../application/config'
import { Record, List } from 'immutable'
import { createSelector } from 'reselect'
import { put, takeEvery, all, call } from 'redux-saga/effects'
/**
* Constants
*/
export const moduleName = '${ModuleName}s'
const prefix = `\${appName}/\${moduleName}`
/**
* Actions
*/
export const ADD_${UppaerCaseModuleName} = `\${prefix}/ADD_${UppaerCaseModuleName}`
export const ADD_${UppaerCaseModuleName}_SUCCESS = `\${prefix}/ADD_${UppaerCaseModuleName}_SUCCESS`
/**
* Reducers
*/
const ReducerState = Record({
entities: new List([])
})
const ${capitalizedModuleName}Record = Record({
id: null,
name: null,
})
export default function reducer (state = new ReducerState(), action) {
const {type, payload} = action
switch (type) {
case ADD_${UppaerCaseModuleName}:
return state.update('entities', (entities) =>
entities.push(new ${capitalizedModuleName}Record (payload.${ModuleName}))
)
}
}
/**
* Selectors
*/
export const stateSelector = (state) => state[moduleName]
export const ${ModuleName}Selector = createSelector(stateSelector, (state) =>
state.entities.valueSeq().toArray()
)
/**
* Action creators
*/
export function add${capitalizedModuleName} (${ModuleName}) {
return {
type: ADD_${UppaerCaseModuleName},
payload: {${ModuleName}}
}
}
/**
* Sagas
*/
export function* saga() {
yield add([
takeEvery(ADD_${UppaerCaseModuleName}, add${capitalizedModuleName}Saga)
])
}
export function* add${capitalizedModuleName}Saga (action) {
yield put(ADD_${UppaerCaseModuleName}_SUCCESS)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment