Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/tungv/redux-yucks

declarative redux ducks with FP
https://github.com/tungv/redux-yucks

Last synced: about 2 months ago
JSON representation

declarative redux ducks with FP

Awesome Lists containing this project

README

        

# redux-yucks
declarative redux ducks with FP

# Proposal

Given a yaml file like this:

```yaml
module: myAwesomeModule
imports:
./some/other/file: ACTIONS

states:
currentStep: initial

selectors:
isAtTheInitialStepSelector: currentStepSelector | eq('initial')

actionCreators:
gotoInitialStep: constant('initial') | currentStepSetter
gotoAwesomeStep: constant('awesome') | currentStepSetter

epics:
updateStepWhenUpdateComplete:
- filter get('type') | eq(ACTIONS.AWESOME)
- filter get('payload.awesomeness') | gte(9000)
- mapTo currentStepSetter('success')
```

`redux-yucks` will take it as an input an returns the follow file

```js
import { combineReducers } from 'redux';
import { createAction, handleActions } from 'redux-actions';
import { combineEpics } from 'redux-observable';
import { ACTIONS } from './some/other/file';

import flow from 'lodash/fp/flow';
import get from 'lodash/fp/get';
import constant from 'lodash/fp/constant';
import eq from 'lodash/fp/eq';
import gte from 'lodash/fp/gte';
import 'rxjs/add/operator/filter';
import 'rxjs/add/operator/mapTo';

// helpers
const takePayload = (state, { payload }) => payload;

// currentStep
const ACTION_SETTER_currentStep = 'myAwesomeModule/action/SETTER_currentStep';
export const currentStepSetter = createAction(ACTION_SETTER_currentStep);
export const currentStepSelector = get('myAwesomeModule.currentStep');
const currentStep = handleActions({
[ACTION_SETTER_currentStep]: takePayload
}, 'initial');

// selectors
export const isAtTheInitialStepSelector = flow(currentStepSelector, eq('initial'));

// actionCreators
export const gotoInitialStep = flow(constant('initial'), currentStepSetter);
export const gotoAwesomeStep = flow(constant('awesome'), currentStepSetter);

// combined reducer
export const reducer = combineReducers({
currentStep,
});

// epics
const updateStepWhenUpdateComplete = actions$ =>
actions$
.filter(flow(get('type'), eq(ACTIONS.AWESOME)))
.filter(flow(get('payload.awesomeness'), gte(9000)))
.mapTo(currentStepSetter('success'));

export const epic = combineEpics({
updateStepWhenUpdateComplete,
});

export default { myAwesomeModule: reducer };
```